Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Enum name from multiple values python

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?

like image 280
Ruvalcaba Avatar asked Apr 04 '17 09:04

Ruvalcaba


People also ask

Can enum have multiple values python?

Enums can't have multiple value per name.

What is Auto in enum Python?

auto() method, we can get the assigned integer value automatically by just using enum. auto() method. Syntax : enum.auto() Automatically assign the integer value to the values of enum class attributes.


1 Answers

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.

like image 142
Ethan Furman Avatar answered Oct 17 '22 05:10

Ethan Furman