Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String-based enum in Python

Tags:

To encapsulate a list of states I am using enum module:

from enum import Enum

class MyEnum(Enum):
    state1='state1'
    state2 = 'state2'

state = MyEnum.state1
MyEnum['state1'] == state  # here it works
'state1' == state  # here it does not throw but returns False (fail!)

However, the issue is that I need to seamlessly use the values as strings in many contexts in my script, like:

select_query1 = select(...).where(Process.status == str(MyEnum.state1))  # works but ugly

select_query2 = select(...).where(Process.status == MyEnum.state1)  # throws exeption

How to do it avoiding calling additional type conversion (str(state) above) or the underlying value (state.value)?

like image 987
sophros Avatar asked Oct 29 '19 13:10

sophros


People also ask

Can Python enums be strings?

Enumerations are created using classes. Enums have names and values associated with them. Properties of enum: Enums can be displayed as string or repr.

Can enum be using with string?

In a string enum, each member has to be constant-initialized with a string literal, or with another string enum member. While string enums don't have auto-incrementing behavior, string enums have the benefit that they “serialize” well.

How do I convert string to enum?

Use the Enum. IsDefined() method to check if a given string name or integer value is defined in a specified enumeration. Thus, the conversion of String to Enum can be implemented using the Enum. Parse ( ) and Enum.


1 Answers

It seems that it is enough to inherit from str class at the same time as Enum:

class MyEnum(str, Enum):
    state1 = 'state1'
    state2 = 'state2'

The tricky part is that the order of classes in the inheritance chain is important as this:

class MyEnum(Enum, str):
    state1 = 'state1'
    state2 = 'state2'

throws:

TypeError: new enumerations should be created as `EnumName([mixin_type, ...] [data_type,] enum_type)`

With the correct class the following operations on MyEnum are fine:

print('This is the state value: ' + state)

As a side note, it seems that the special inheritance trick is not needed for formatted strings which work even for Enum inheritance only:

msg = f'This is the state value: {state}'  # works without inheriting from str
like image 161
sophros Avatar answered Oct 01 '22 17:10

sophros