从多个值中获取枚举名称python [英] Get Enum name from multiple values python
问题描述
我正在尝试获取给定多个值之一的枚举的名称:
class DType(Enum):float32 = ["f", 8]double64 = ["d", 9]
当我尝试获得一个值时,它可以工作:
print DType["float32"].value[1] # 打印 8打印 DType["float32"].value[0] # 打印 f
但是当我尝试从给定值中获取名称时,只会出现错误:
print DataType(8).name打印数据类型(f").名称
<块引用>
raise ValueError("%s is not a valid %s" % (value, cls.name))
ValueError: 8 is not a valid DataType
ValueError: f 不是有效的数据类型
有没有办法做到这一点?还是我使用了错误的数据结构?
最简单的方法是使用 aenum
library1,看起来像这样:
from aenum import MultiValueEnum类 DType(MultiValueEnum):float32 = "f", 8double64 = "d", 9
并在使用中:
<预><代码>>>>DType("f")<DType.float32: 'f'>>>>D型(9)<DType.double64: 'd'>如您所见,列出的第一个值是规范值,并显示在 repr()
中.
如果您希望显示所有可能的值,或者需要使用 stdlib Enum
(Python 3.4+),则 此处找到的答案 是您想要的基础(也适用于 aenum
):
class DType(Enum):float32 = "f", 8double64 = "d", 9def __new__(cls, *values):obj = object.__new__(cls)# 第一个值是规范值obj._value_ = 值[0]对于 values[1:] 中的 other_value:cls._value2member_map_[other_value] = objobj._all_values = 值返回对象def __repr__(self):返回 '<%s.%s: %s>'% (self.__class__.__name__,self._name_,', '.join([repr(v) for v in self._all_values]),)
并在使用中:
<预><代码>>>>DType("f")<DType.float32: 'f', 8>>>>数据类型(9)<DType.float32: 'f', 9><小时>
1 披露:我是 Python stdlib 的作者Enum
,enum34
向后移植a> 和 高级枚举 (aenum
) 库.>
I'm trying to get the name of a enum given one of its multiple values:
class DType(Enum):
float32 = ["f", 8]
double64 = ["d", 9]
when I try to get one value giving the name it works:
print DType["float32"].value[1] # prints 8
print DType["float32"].value[0] # prints f
but when I try to get the name out of a given value only errors will come:
print DataType(8).name
print DataType("f").name
raise ValueError("%s is not a valid %s" % (value, cls.name))
ValueError: 8 is not a valid DataType
ValueError: f is not a valid DataType
Is there a way to make this? or am I using the wrong data structure?
The easiest way is to use the aenum
library1, which would look like this:
from aenum import MultiValueEnum
class DType(MultiValueEnum):
float32 = "f", 8
double64 = "d", 9
and in use:
>>> DType("f")
<DType.float32: 'f'>
>>> DType(9)
<DType.double64: 'd'>
As you can see, the first value listed is the canonical value, and shows up in the repr()
.
If you want all the possible values to show up, or need to use the stdlib Enum
(Python 3.4+), then the answer found here is the basis of what you want (and will also work with aenum
):
class DType(Enum):
float32 = "f", 8
double64 = "d", 9
def __new__(cls, *values):
obj = object.__new__(cls)
# first value is canonical value
obj._value_ = values[0]
for other_value in values[1:]:
cls._value2member_map_[other_value] = obj
obj._all_values = values
return obj
def __repr__(self):
return '<%s.%s: %s>' % (
self.__class__.__name__,
self._name_,
', '.join([repr(v) for v in self._all_values]),
)
and in use:
>>> DType("f")
<DType.float32: 'f', 8>
>>> Dtype(9)
<DType.float32: 'f', 9>
1 Disclosure: I am the author of the Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) library.
这篇关于从多个值中获取枚举名称python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!