enumagic¶
Python enums that work like magic.
-
class
enumagic.
IterEnum
[source]¶ 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
[source]¶ Bases:
enum.EnumMeta
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.Raises: TypeError
– If the item is not a string or enum instance.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
(index: int, label: str)[source]¶ Bases:
enum.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
-
__str__
() → str[source]¶ Return the instance as a string.
Returns: str
– The label of the instance.Examples
>>> str(MappingExample.A) 'Alice'
-
-
class
enumagic.
MappingMeta
[source]¶ Bases:
enum.EnumMeta
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')]
-
items
¶ Get a mapping of
label
/index
pairs.Examples
>>> MappingExample.items {'Alice': 0, 'Bob': 1}
-
enumagic.django¶
Special enums for Django.
-
class
enumagic.django.
ChoiceEnum
¶ -
Enum class that can be used as Django field choices.
Examples
>>> from django.db.models import CharField, Model >>> class ColorChoice(ChoiceEnum): ... RED = '#F00' ... GREEN = '#0F0' ... BLUE = '#00F' >>> class Color(Model): ... color = CharField(choices=ColorChoice) >>> example = Color(color=ColorChoice.RED) >>> example.get_color_display() '#F00'
-
do_not_call_in_templates
= True¶ Prevent the Django template system from calling the enum.
-
__class__
¶ alias of
enumagic.django.ChoiceMeta
-
-
class
enumagic.django.
ChoiceMeta
¶ Bases:
enumagic.IterMeta
Choice enum metaclass.