2

As I am using Python 2.6 I am trying to create custom Enum class. What I need is to store string with the enum object. Something like:

class Enum(object):
    def __init__(self, value):
        self.value = value[0]
        self.msg = value[1]

class Parameters(Enum):
    SERVER_LISTEN_ADDRESS = (0, "http://blabla.com")
    SERVER_LISTEN_PORT = (1, "7001")
    SERVER_SSL_LISTEN_PORT = (2, "7002")
    (...) # many parameters more 
    SERVER_NAME = (X, "name")

The problem is that my 'Parameters' class is not iterable... From another file I need to be able to iterate among PARAMs:

import Parameters

for param in Parameters:
    # do sth

Unfortunately, for my solution I have error: for param in Parameters: TypeError: 'type' object is not iterable.

Konrad
  • 630
  • 2
  • 11
  • 27

3 Answers3

4

Use the backported enum module (documentation):

from enum import Enum


class Parameters(Enum):
    PARAM1 = (0, "some msg 1")
    PARAM2 = (1, "some msg 2")

for i in Parameters:
    print(i, '=', i.value)

which prints:

Parameters.PARAM1 = (0, 'some msg 1')

Parameters.PARAM2 = (1, 'some msg 2')

Community
  • 1
  • 1
Joschua
  • 5,816
  • 5
  • 33
  • 44
  • Hey Joschua! But I'm using Python 2.6 – Konrad Sep 28 '15 at 10:03
  • Hi, I think it's also available for Python 2.6. I'm curious though, why do you use Python 2.6 instead of Python 2.7. Aren't they compatible (i.e. you could replace the older version with the newer one without any ramifications)? btw. Are you on Windows or Linux? – Joschua Sep 28 '15 at 10:04
  • Unfortunately I cannot change the environment that I'm working on :< It needs to be compatible with many machines in our network and there I have only Python 2.6. I am on Linux – Konrad Sep 28 '15 at 10:27
  • It's backported down to 2.4, so this would be the best solution. – Gareth Latty Sep 28 '15 at 11:19
  • @Konrad Then you should be able to install it with `sudo pip install enum34`. And [here](http://stackoverflow.com/questions/24294467/how-to-install-pip-for-python-2-6)'s how to install pip if it isn't. – Joschua Sep 28 '15 at 13:57
1
for param in Parameters:
   #

Above will not work because Parameters is class and it is not iterable.

class Parameters(Enum):
    PARAM1 = (0, "some msg 1")
    PARAM2 = (1, "some msg 2")

PARAM1 and PARAM2 are class variables in above case.


We have to do something like this:

class Enum(object):
    def __init__(self, value):
        self.value = value[0]
        self.msg = value[1]

class Parameters(Enum):
    def _init__(self, value):
        super.__init__(value)

PARAM1 = (0, "some msg 1")
PARAM2 = (1, "some msg 2")
for param in [PARAM1, PARAM2]:
    print Parameters(param)

[Edit 1]:

Create number of objects by for loop with range function

code:

for i in range(100):
    param = (i, "some mesg %s"%i)
    print Parameters(param)

[Edit 2]:

Get values from the User by raw_input function and type conversion from the string to integer

Demo:

>>> no = raw_input("Enter Number of objects you want to create(Give only number): ")
Enter Number of objects you want to create(Give only number): 33
>>> no
'33'
>>> type(no)
<type 'str'>
>>> no_int = int(no)
>>> type(no_int)
<type 'int'>

Note:

use raw_input() in Python 2.x

use input() in Python 3.x

[Edit 3]: Hardcode values by Instance variables method.

Define class in hardcoded_values.py file

class Hardcodes():
    def __init__(self,):
        self.para1 = (1, "some msg 1")
        self.para2 = (2, "some msg 2")
        self.para3 = (3, "some msg 3")
        self.para4 = (4, "some msg 4")

Import Hardcodes class in test.py file

#- Create Object(instance) 
from hardcoded_values import Hardcodes
obj = Hardcodes()
hardcode_values = obj.__dict__
for i in hardcode_values.iteritems():
    print i

Output:

$ python test.py 

('para3', (3, 'some msg 3'))
('para2', (2, 'some msg 2'))
('para1', (1, 'some msg 1'))
('para4', (4, 'some msg 4'))
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
  • Thanks! Is there any way to make: 'for param in [PARAM1, PARAM2]' independent from PARAMs names? – Konrad Sep 28 '15 at 07:32
  • means I am not getting. `PARAM1` and `PARAM2` are variables of `tuple` types. You means you have to generate more variable like `PARAM1` ? – Vivek Sable Sep 28 '15 at 08:32
  • I mean that I can have a lot of PARAMs (f.g PARAM1 - ... - PARAM101) and I would like to be able to iterate on every PARAM without knowing its name – Konrad Sep 28 '15 at 08:54
  • hmm. but in your answer you assume that there is 100 parameters. I just gave an example. It's not totally independent... – Konrad Sep 28 '15 at 09:48
  • you can change this value according to your logic. e.g. we can take this file from the user by `raw_input() - Python2.x` and `input()-Python 3.x` – Vivek Sable Sep 28 '15 at 09:52
  • Many variables are in class?? means. Can you give or explain us input and output of your programme, so we can suggest you different solutions. – Vivek Sable Sep 28 '15 at 10:05
  • Vivek, I have updated my question. I don't want to generate enums, I just want to create static class with some parameters and to be able to refer to that – Konrad Sep 28 '15 at 10:17
  • ok, means these values are hard coded in code and more values will add in futures . correct? we want to refer these values(i.e. Variables of respective values) in other py files. correct? – Vivek Sable Sep 28 '15 at 10:27
  • Exactly! In other words, I need to be able to iterate for all the params that are in Parameters class without knowing their name... Sorry for the confusion – Konrad Sep 28 '15 at 10:29
  • added answer by class instance variable method – Vivek Sable Sep 28 '15 at 11:09
0

Check out the myo.utils.enum.Enumeration class: https://github.com/NiklasRosenstein/myo-python/blob/9335663d00df762b29e0c0f12550d8d73aa097b6/myo/utils/enum.py

Niklas R
  • 16,299
  • 28
  • 108
  • 203