How to convert int to Enum in python?
Asked Answered
I

6

174

Using the new Enum feature (via backport enum34) with python 2.7.6.

Given the following definition, how can I convert an int to the corresponding Enum value?

from enum import Enum

class Fruit(Enum):
    Apple = 4
    Orange = 5
    Pear = 6

I know I can hand craft a series of if-statements to do the conversion but is there an easy pythonic way to convert? Basically, I'd like a function ConvertIntToFruit(int) that returns an enum value.

My use case is I have a csv file of records where I'm reading each record into an object. One of the file fields is an integer field that represents an enumeration. As I'm populating the object I'd like to convert that integer field from the file into the corresponding Enum value in the object.

Izawa answered 30/5, 2014 at 9:50 Comment(0)
H
297

You 'call' the Enum class:

Fruit(5)

to turn 5 into Fruit.Orange:

>>> from enum import Enum
>>> class Fruit(Enum):
...     Apple = 4
...     Orange = 5
...     Pear = 6
... 
>>> Fruit(5)
<Fruit.Orange: 5>

From the Programmatic access to enumeration members and their attributes section of the documentation:

Sometimes it’s useful to access members in enumerations programmatically (i.e. situations where Color.red won’t do because the exact color is not known at program-writing time). Enum allows such access:

>>> Color(1)
<Color.red: 1>
>>> Color(3)
<Color.blue: 3>

In a related note: to map a string value containing the name of an enum member, use subscription:

>>> s = 'Apple'
>>> Fruit[s]
<Fruit.Apple: 4>
Hake answered 30/5, 2014 at 10:0 Comment(5)
Thanks just got this as I was re-reading the documentation. Wasn't clear to me on my first skim through. Also I just tried using a string and nice to see that it works when the enumeration value is a string as well and I'm assuming any arbitrary object value.Izawa
Very good, so conversion is by value. Is there an analogue for by name other than through a list comprehension to filter through the enum list and take the match on the name attributes?Laniferous
@Laniferous do follow the documentation link in my answer, it is explicitly covered there. Use item access: Fruit['Orange'].Hake
Bingo, very awesome! I searched the page for convert,coerce, and cast but didn't hit anything. Looks like the magic was access enum members by name, use item access. Perfecto, thanks so much, going to clean up my code with this syntax.Laniferous
Even cooler is that I can use the stuff like "foobar" in Fruit.__members__ to test for membership and even pull off Fruit.get( 'foobar', Fruit.Orange ) to get that default Enum flavor. My code is looking a lot more simplified removing all these accessory lookup scaffolding I laid down earlier.Laniferous
C
3

I think it is in simple words is to convert the int value into Enum by calling EnumType(int_value), after that access the name of the Enum object:

my_fruit_from_int = Fruit(5) #convert to int
fruit_name = my_fruit_from_int.name #get the name
print(fruit_name) #Orange will be printed here

Or as a function:

def convert_int_to_fruit(int_value):
    try:
        my_fruit_from_int = Fruit(int_value)
        return my_fruit_from_int.name
    except:
        return None
Chervonets answered 31/3, 2019 at 7:55 Comment(0)
M
1

I wanted something similar so that I could access either part of the value pair from a single reference. The vanilla version:

#!/usr/bin/env python3


from enum import IntEnum


class EnumDemo(IntEnum):
    ENUM_ZERO       = 0
    ENUM_ONE        = 1
    ENUM_TWO        = 2
    ENUM_THREE      = 3
    ENUM_INVALID    = 4


#endclass.


print('Passes')
print('1) %d'%(EnumDemo['ENUM_TWO']))
print('2) %s'%(EnumDemo['ENUM_TWO']))
print('3) %s'%(EnumDemo.ENUM_TWO.name))
print('4) %d'%(EnumDemo.ENUM_TWO))
print()


print('Fails')
print('1) %d'%(EnumDemo.ENUM_TWOa))

The failure throws an exception as would be expected.

A more robust version:

#!/usr/bin/env python3


class EnumDemo():


    enumeration =   (
                        'ENUM_ZERO',    # 0.
                        'ENUM_ONE',     # 1.
                        'ENUM_TWO',     # 2.
                        'ENUM_THREE',   # 3.
                        'ENUM_INVALID'  # 4.
                    )


    def name(self, val):
        try:

            name = self.enumeration[val]
        except IndexError:

            # Always return last tuple.
            name = self.enumeration[len(self.enumeration) - 1]

        return name


    def number(self, val):
        try:

            index = self.enumeration.index(val)
        except (TypeError, ValueError):

            # Always return last tuple.
            index = (len(self.enumeration) - 1)

        return index


#endclass.


print('Passes')
print('1) %d'%(EnumDemo().number('ENUM_TWO')))
print('2) %s'%(EnumDemo().number('ENUM_TWO')))
print('3) %s'%(EnumDemo().name(1)))
print('4) %s'%(EnumDemo().enumeration[1]))
print()
print('Fails')
print('1) %d'%(EnumDemo().number('ENUM_THREEa')))
print('2) %s'%(EnumDemo().number('ENUM_THREEa')))
print('3) %s'%(EnumDemo().name(11)))
print('4) %s'%(EnumDemo().enumeration[-1]))

When not used correctly this avoids creating an exception and, instead, passes back a fault indication. A more Pythonic way to do this would be to pass back "None" but my particular application uses the text directly.

Materi answered 13/9, 2019 at 14:11 Comment(1)
Why not just define the __int__ and __str__ methods on an Enum instance?Zoniazoning
R
1
class Status(IntEnum):
    UPLOADED = 1
    DOWNLOADED = 5
    SEGMENTED = 10    
    DIRECTED = 15
    READYTODEEP = 20

to get the enum;

statusId = 5
Status(statusId)

to get the enum's string value;

statusId = 5
print(Status(statusId).name)
Roderick answered 17/1, 2023 at 11:49 Comment(0)
K
1

You can get the Enum name string like this:

Fruit(5).name

You get the Enum int value like this:

Fruit(5).value

And you can get Enum object like this:

Fruit(5)

Test your class:

from enum import Enum

class Fruit(Enum):
    Apple = 4
    Orange = 5
    Pear = 6

Like this:

>>> Fruit(5)
<Fruit.Orange: 5>
>>> Fruit(5).name
'Orange'
>>> Fruit(5).value
5

>>> Fruit['Apple']
<Fruit.Apple: 4>
>>> Fruit['Apple'].value
4
>>> Fruit['Apple'].name
'Apple'

Notice you access it like a function when you want to convert from int

AND you access it with square brackets when you want to convert from a string

Ketty answered 21/4, 2023 at 11:15 Comment(0)
G
0

Old question, but wanted to share. when receiving an Enum inside of a variable, you can utilize the "name" property, so whatever variable you're receiving, just variable.name will show the string mapped to the enumeration.

Gaylordgaylussac answered 9/4 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.