enumagic¶
Python enums that work like magic.
- class enumagic.IterEnum(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
EnumEnum class that can be used as an iterable.
- __class__ = <class 'enumagic._iter.IterMeta'>¶
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:
EnumTypeIterable 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–Trueif the enum has a member that matches the given item,Falseotherwise.
Examples
>>> 'B' in IterExample True >>> 'C' in IterExample False
- __iter__() Iterator[tuple[str, _VT]][source]¶
Iterate over the entries of the enum.
- Yields:
tupleofstr,object– The next tuple where the first element is thenameof the enum instance and the second element is thevalueof the enum instance.
Examples
>>> it = iter(IterExample) >>> next(it) ('A', 'Alice')
- __firstlineno__ = 28¶
- __static_attributes__ = ()¶
- class enumagic.MappingEnum(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
EnumEnum 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__ = <class 'enumagic._mapping.MappingMeta'>¶
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'
- class enumagic.MappingMeta(cls, bases, classdict, *, boundary=None, _simple=False, **kwds)[source]¶
Bases:
EnumTypeMapping 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:
tupleofint,str– The next tuple where the first element is theindexof the enum instance and the second element is thelabelof the enum instance.
Examples
>>> list(MappingExample) [(0, 'Alice'), (1, 'Bob')]
- __firstlineno__ = 27¶
- __static_attributes__ = ()¶
- property indices: tuple[int, ...]¶
Get the indices of the enum.
Examples
>>> MappingExample.indices (0, 1)
- class enumagic.StrEnum(new_class_name, /, names, *, 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__ = <class 'enum.EnumType'>¶
alias of
enum.EnumMeta
- __format__(format_spec)¶
Return a formatted version of the string as described by format_spec.
- __repr__()¶
Return repr(self).