英文:
How to initialize named tuple in python enum
问题
以下是您提供的代码部分的翻译:
我有一个Enum,它被用作SqlALchemy中的列数据类型。但我需要更多的Enum属性,以使它能够在代码的其他功能中访问。
这是我目前创建的内容:
class ServerHealth(Enum):
"""服务器健康状态。"""
HealthStatus = namedtuple("HealthStatus", ["name", "low_bound", "high_bound"])
High = HealthStatus(name="High", low_bound=5, high_bound=24)
Fair = HealthStatus(name="Fair", low_bound=3, high_bound=5)
Low = HealthStatus(name="Low", low_bound=0, high_bound=3)
@DynamicClassAttribute
def value(self):
return self.name
这也在SqlAlchemy模型的某处使用,所以我需要保持它不会破坏-
class Server(Base):
server_health = Column(ENUM(ServerHealth), nullable=True)
我需要将namedtuple的"name"作为数据库中的列值。据我理解,value属性由SqlAlchemy使用,所以我已经重写了它们,value按预期工作,即。
>>>ServerHealth.High.value
"High"
但当我尝试访问low_bound和high_bound时,它会出错
>>>ServerHealth.High.low_bound
AttributeError Traceback (most recent call last)
Input In [77], in <cell line: 1>()
----> 1 ServerHealth.High.low_bound
AttributeError: 'ServerHealth'对象没有属性'low_bound'
现在,我应该重写哪个函数以使其正常工作而不破坏数据库初始化?
英文:
I have a Enum which is used as a column datatype in SqlALchemy. But I need more properties of the Enum to make it accessible for the code's other functionality.
This is what I have created till now:
class ServerHealth(Enum):
"""Status of Server health."""
HealthStatus = namedtuple("HealthStatus", ["name", "low_bound", "high_bound"])
High = HealthStatus(name="High", low_bound=5, high_bound=24)
Fair = HealthStatus(name="Fair", low_bound=3, high_bound=5)
Low = HealthStatus(name="Low", low_bound=0, high_bound=3)
@DynamicClassAttribute
def value(self):
return self.name
This is used somewhere in sqlalchemy model as well, so I need to keep it the way it doesn't break-
class Server(Base):
server_health = Column(ENUM(ServerHealth), nullable=True)
I need the "name" of namedtuple as the columns value in DB. As per my understanding the value attribute is used by sqlalchemy, so I have overwritten them and the value works as expected i.e.
>>>ServerHealth.High.value
"High"
but when I try to access low_bound and high_bound it breaks
>>>ServerHealth.High.low_bound
AttributeError Traceback (most recent call last)
Input In [77], in <cell line: 1>()
----> 1 ServerHealth.High.low_bound
AttributeError: 'ServerHealth' object has no attribute 'low_bound'
Now what function should I overwrite in order to get that working without breaking db initialization.
答案1
得分: 1
这是我会这样做的:
class ServerHealth(Enum):
"""
服务器健康状态。
"""
def __new__(cls, name, low_bound, high_bound):
member = object.__new__(cls)
member._value_ = name
member.low_bound = low_bound
member.high_bound = high_bound
return member
HIGH = "High", 5, 24
FAIR = "Fair", 3, 5
LOW = "Low", 0, 3
在使用中:
>>> ServerHealth.High.low_bound
5
披露:我是Python标准库Enum
、enum34
后端和高级枚举(aenum
)库的作者。
英文:
This is how I would do it:
class ServerHealth(Enum):
"""
Status of Server health.
"""
def __new__(cls, name, low_bound, high_bound):
member = object.__new__(cls)
member._value_ = name
member.low_bound = low_bound
member.high_bound = high_bound
return member
#
HIGH = "High", 5, 24
FAIR = "Fair", 3, 5
LOW = "Low", 0, 3
and in use:
>>> ServerHealth.High.low_bound
5
Disclosure: I am the author of the Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) library.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论