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:
bool
–True
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
ofstr
,object
– The next tuple where the first element is thename
of the enum instance and the second element is thevalue
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:
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
- 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
ofint
,str
– The next tuple where the first element is theindex
of the enum instance and the second element is thelabel
of the enum instance.
Examples
>>> list(MappingExample) [(0, 'Alice'), (1, 'Bob')]
- property indices: tuple[int, ...]¶
Get the indices of the enum.
Examples
>>> MappingExample.indices (0, 1)
- class enumagic.StrEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
-
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).