1

I would like to create dynamic enums in python loaded from a SQL Table. The output of SQL will be a list of tuplets, which with I want to fill the attributes of the enum.

Lets say I receive this list:

lst = [('PROCESS_0', 0, "value", 123, False), ('PROCESS_1',1,"anothervalue", 456, True)]

I now want to fill the values in the enum below:

class Jobs(IntEnum):
    def __new__(cls, value: int, label: str, heartbeat: int = 60, heartbeat_required: bool = False):
        obj = int.__new__(cls, value)
        obj._value_ = value
        obj.label = label
        obj.heartbeat = heartbeat
        obj.heartbeat_required = heartbeat_required
        return obj

The first variable in the tuple should be the variable name of the enum, I have solved this with:

locals()['Test'] = (0, '', 789, False)

But this only works for single values, it seems that I can not run a for loop within enum. When using a for loop like this:

 for i in lst:
    locals()[i[0]] = (i[1], i[2], i[3])

Python sends this error TypeError: Attempted to reuse key: 'i' which propably comes from enums only having constants.

Is there any (possibly elegant) solution for this?

Many thanks in advance!

3
  • 1
    I don't get any error when running your code on Python3. Commented Jun 7, 2022 at 10:22
  • I am running code example 3 and 4 within the Enum class. My current python version is 3.8.5. Commented Jun 7, 2022 at 10:25
  • If I understand your use case, I would go for a dict of namedtuples (or even dataclasses if you want to be able to change the values of the attributes) Commented Jun 7, 2022 at 10:25

2 Answers 2

1

You need to use _ignore_ = "i". Something like:

class Jobs(IntEnum):

    _ignore_ = "i"

    def __new__(cls, value, label, heartbeat=60, heartbeat_required=False):
        obj = int.__new__(cls, value)
        obj._value_ = value
        obj.label = label
        obj.heartbeat = heartbeat
        obj.heartbeat_required = heartbeat_required
        return obj

    for i in lst:
        locals()[i[0]] = i[1:]
Sign up to request clarification or add additional context in comments.

Comments

0

Check the example at https://docs.python.org/3/howto/enum.html#timeperiod

Note that the _ignore_ can be avoided in favor of dict comprehension

from datetime import timedelta
class Period(timedelta, Enum):
    "different lengths of time"
    vars().update({ f"day_{i}": i for i in range(367) })

Then you can access all possible enum values via Period.__members__

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.