enumagic

Python enums that work like magic.

class enumagic.IterEnum

Bases: enum.Enum

Enum class that can be used as an iterable.

__class__

alias of enumagic.IterMeta

Examples

>>> class IterExample(IterEnum):
...     A = 'Alice'
...     B = 'Bob'
>>> dict(IterExample)
{'A': 'Alice', 'B': 'Bob'}
class enumagic.IterMeta

Bases: enum.EnumMeta

Iterable enum metaclass.

__contains__(item: Any) → bool

Check whether the enum contains a certain item

Parameters:item (Any) – A string or enum instance.
Returns:boolTrue if the enum has a member that matches the given item, False otherwise.

Examples

>>> 'B' in IterExample
True
>>> 'C' in IterExample
False
__iter__() → Iterator[Tuple[str, _VT]]

Iterate over the entries of the enum.

Yields:tuple of str, object – The next tuple where the first element is the name of the enum instance and the second element is the value of the enum instance.

Examples

>>> it = iter(IterExample)
>>> next(it)
('A', 'Alice')
class enumagic.MappingEnum(index: int, label: str)

Bases: enum.Enum

Enum class which maps labels to indices.

Variables:
  • index (int) – An integer that will be used as the index.
  • label (str) – A string that will be used as the label.

Examples

>>> class MappingExample(MappingEnum):
...     A = 0, 'Alice'
...     B = 1, 'Bob'
>>> '%d, %s' % (MappingExample.B.index, Example.B.label)
'1, Bob'
__class__

alias of enumagic.MappingMeta

__str__() → str

Return the instance as a string.

Returns:str – The label of the instance.

Examples

>>> str(MappingExample.A)
'Alice'
__index__() → int

Return the instance as an index.

Returns:int – The index of the instance.

Examples

>>> test = ['first', 'second']
>>> test[MappingExample.B]
'second'
__int__() → int

Return the instance as an integer.

Returns:int – The index of the instance.

Examples

>>> int(MappingExample.A)
0
class enumagic.MappingMeta

Bases: enum.EnumMeta

Mapping enum metaclass.

__call__(value: Any) → _ET

Get an enum instance from the given value.

Parameters:value (Any) – The value to look for in the members of the enum.
Returns:Enum – An enum instance that corresponds to the value.
Raises:ValueError – If the given value is invalid.

Examples

>>> MappingExample(0)
<MappingExample.A: (0, 'Alice')>
>>> MappingExample('Bob')
<MappingExample.B: (1, 'Bob')>
__iter__() → Iterator[Tuple[int, str]]

Iterate over the values of the enum.

Yields:tuple of int, str – The next tuple where the first element is the index of the enum instance and the second element is the label of the enum instance.

Examples

>>> list(MappingExample)
[(0, 'Alice'), (1, 'Bob')]
indices

Get the indices of the enum.

Type
tuple of int

Examples

>>> MappingExample.indices
(0, 1)
items

Get a mapping of label/index pairs.

Type
dict of str to int

Examples

>>> MappingExample.items
{'Alice': 0, 'Bob': 1}
labels

Get the labels of the enum.

Type
tuple of str

Examples

>>> MappingExample.labels
('Alice', 'Bob')
class enumagic.StrEnum

Bases: str, enum.Enum

Enum class that be used as a string.

Examples

>>> class StrExample(StrEnum):
...     A = 'Alice'
>>> StrExample.A.upper()
'ALICE'
__class__

alias of enum.EnumMeta

__str__() → str

Return the instance as a string.

Returns:str – The value of the instance.

Examples

>>> str(StrExample.A)
'Alice'