Python dynamic enum

Question:

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!

Asked By: AGI_Max

||

Answers:

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:]
Answered By: Ethan Furman

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__

Answered By: Yang Bo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.