我正在尝试使用 type(name, base, dict) 动态创建一个 Enum 类。
from enum import Enum
class FriendlyEnum(Enum):
def hello(self):
print(self.name + ' says hello!')
Run Code Online (Sandbox Code Playgroud)
正常的方法工作得很好:
class MyEnum(FriendlyEnum):
foo = 1
bar = 2
MyEnum.foo.hello()
# ->
foo says hello!
Run Code Online (Sandbox Code Playgroud)
但如果我动态尝试:
MyEnum = type('MyEnum',
(FriendlyEnum,),
{'foo':1,'bar':2}
)
MyEnum.foo.hello()
# ->
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/enum.py", line 146, in __new__
enum_members = {k: classdict[k] for k in classdict._member_names}
AttributeError: 'dict' object has no attribute '_member_names'
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
它对于测试父 Enum 类的方法、动态创建具有不同属性和相同继承方法的新 Enum 类可能很有用。
我在 Circleci 上使用 docker-compose 来启动一个 postgres 容器,其中安装了一堆 sql 文件/docker-entrypoint-initdb.d/。我正在使用 ubuntu 机器映像:ubuntu-2004:202010-01,但收到错误:
popen failure: Cannot allocate memory
initdb: error: program "postgres" is needed by initdb but was not found in the same directory as "/usr/lib/postgresql/15/bin/initdb"
Run Code Online (Sandbox Code Playgroud)
它一直在醒来,但在某个时候它停止了这样做。对我可以尝试什么有什么建议吗?我已经尝试过一些更大的resource_type(即从中型到大型)和其他一些 ubuntu 机器映像。