enumagic

Python enums that work like magic.

class enumagic.IterEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: 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(cls, bases, classdict, *, boundary=None, _simple=False, **kwds)[source]

Bases: EnumType

Iterable enum metaclass.

__contains__(item: Any) bool[source]

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]][source]

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(value: Any)[source]

Bases: 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

__index__() int[source]

Return the instance as an index.

Returns:

int – The index of the instance.

Examples

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

Return the instance as an integer.

Returns:

int – The index of the instance.

Examples

>>> int(MappingExample.A)
0
class enumagic.MappingMeta(cls, bases, classdict, *, boundary=None, _simple=False, **kwds)[source]

Bases: EnumType

Mapping enum metaclass.

__call__(value: Any) _ET[source]

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]][source]

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')]
property indices: tuple[int, ...]

Get the indices of the enum.

Type

tuple of int

Examples

>>> MappingExample.indices
(0, 1)
property items: dict[str, int]

Get a mapping of label/index pairs.

Type

dict of str to int

Examples

>>> MappingExample.items
{'Alice': 0, 'Bob': 1}
property labels: tuple[str, ...]

Get the labels of the enum.

Type

tuple of str

Examples

>>> MappingExample.labels
('Alice', 'Bob')
class enumagic.StrEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: str, Enum

Enum class that be used as a string.

Examples

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

alias of enum.EnumMeta

__format__(format_spec)

Return a formatted version of the string as described by format_spec.

__repr__()

Return repr(self).

__str__() str[source]

Return the instance as a string.

Returns:

str – The value of the instance.

Examples

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