Python动态枚举

发布于 2025-02-04 21:32:04 字数 898 浏览 0 评论 0原文

我想在从SQL表中加载的Python中创建动态枚举。 SQL的输出将是tuplets的列表,我想填写枚举的属性。

假设我收到此列表:

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

我现在想填写以下枚举中的值:

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

元组中的第一个变量应该是枚举的变量名称,我已经对此进行了解决:

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

但是这仅适用于单个值,似乎我无法在枚举内运行循环。当使用这样的循环时:

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

python发送此错误 typeError:尝试重复使用密钥:'i'',它可能来自只有常数的枚举。

有没有(可能是优雅的)解决方案?

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过度放纵 2025-02-11 21:32:04

您需要使用_ignore_ =“ I”。像:

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:]

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:]
放飞的风筝 2025-02-11 21:32:04

https://docs.pypython.org/3/howto/ enum.html#timeperiod

请注意,_ignore _可以避免使用

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

ofiper ecipt.__ _____ ,可以避免使用dict Gracements。

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__

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文