Get Enum name from multiple values python

随声附和 提交于 2019-12-05 18:18:33

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/43202777/get-enum-name-from-multiple-values-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!