Source
named_enum.meta
Module for the rewriting EnumMeta class. It contains 2 classes: NamedEnumMeta, _NamedEnumDict.
- class NamedEnumMeta(name: str, bases: Tuple, namespace: _NamedEnumDict)[source]
Extends the EnumMeta class for three purposes:
1. uses the _NamedEnumDict as the data type of the namespace parameter for __new__ function, such that we can use the namedtuple as the data type of the value of each enumeration item.
2. provides extra functions, which is independent of the variable _field_names_, such as names, values, as_dict, as_list, as_set, as_tuple, as_ordereddict, describe, gen. The aim is extending the Enum class for complicated use cases in software development.
3. provides functions for each field name defined in class variable _field_names_ in the NamedEnum class and its subclasses, for example:
assuming ‘key’ is included in _field_names_, then the functions for this field name are: keys, from_key, has_key.
- _fields() Tuple [source]
Returns the defined field names as a tuple for the enumeration class.
Note
If the variable _field_names_ is None or empty value, then returns an empty tuple.
- Returns:
tuple of field names.
- Return type:
Tuple
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> TripleEnum._fields() ('first', 'second', 'third') >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> Triangle._fields() ('first', 'second', 'third')
- as_dict() Dict [source]
Converts the enumeration to a dict, in which the key is the name of the enumeration item and value is its value.
- Returns:
a dictionary containing name-value-pairs of the enumeration.
- Return type:
Dict
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.as_dict() {} >>> Triangle.as_dict() {'EQUILATERAL': NamedTuple(first=6, second=6, third=6), 'RIGHT': NamedTuple(first=3, second=4, third=5)}
- as_list() List [source]
Converts the enumerations to a list, in which each item is a tuple of the enumeration item’s name and value.
- Returns:
a list containing name-value-pairs of the enumeration.
- Return type:
List
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.as_list() [] >>> Triangle.as_list() [('EQUILATERAL', NamedTuple(first=6, second=6, third=6)), ('RIGHT', NamedTuple(first=3, second=4, third=5))]
- as_ordereddict() OrderedDict [source]
Converts the enumerations to an OrderedDict, in which each item is a tuple of the enumeration item’s name and value.
- Returns:
- an OrderedDict containing name-value-pairs of the
enumeration.
- Return type:
OrderedDict
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.as_ordereddict() OrderedDict() >>> Triangle.as_ordereddict() OrderedDict([('EQUILATERAL', NamedTuple(first=6, second=6, third=6)), ('RIGHT', NamedTuple(first=3, second=4, third=5))])
- as_set() Set [source]
Converts the enumerations to a set, in which each item is a tuple of the enumeration item’s name and value.
- Returns:
a set containing name-value-pairs of the enumeration.
- Return type:
Set
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.as_set() == set() True >>> isinstance(Triangle.as_set(), set) True >>> dict(Triangle.as_set()) == Triangle.as_dict() True
- as_tuple() Tuple [source]
Converts the enumerations to a tuple, in which each item is a tuple of the enumeration item’s name and value.
- Returns:
a tuple containing name-value-pairs of the enumeration.
- Return type:
Tuple
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.as_tuple() () >>> Triangle.as_tuple() (('EQUILATERAL', NamedTuple(first=6, second=6, third=6)), ('RIGHT', NamedTuple(first=3, second=4, third=5)))
- describe() None [source]
Prints in the console a table showing the content of the enumeration.
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.describe() Class: TripleEnum Name | First | Second | Third ----------------------------- >>> Triangle.describe() Class: Triangle Name | First | Second | Third ------------------------------------ EQUILATERAL | 6 | 6 | 6 RIGHT | 3 | 4 | 5
- gen(name_value_pair: bool | None = True) Generator [source]
Returns a generator of pairs consisting of each enumeration item’s name and value, if name_value_pair is True; otherwise a generator of the enumeration items.
- Parameters:
name_value_pair (Optional[bool]) – controls the return result. If true, returns the generator of name-value pair; if False, returns the generator of the enumeration items. Default value is True.
- Returns:
a generator which iterates all the enumeration items.
- Return type:
Generator
Examples
>>> from types import GeneratorType >>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> isinstance(TripleEnum.gen(), GeneratorType) True >>> list(TripleEnum.gen()) [] >>> isinstance(TripleEnum.gen(name_value_pair=False), GeneratorType) True >>> list(TripleEnum.gen(name_value_pair=False)) [] >>> isinstance(Triangle.gen(), GeneratorType) True >>> list(Triangle.gen()) [('EQUILATERAL', NamedTuple(first=6, second=6, third=6)), ('RIGHT', NamedTuple(first=3, second=4, third=5))] >>> isinstance(Triangle.gen(name_value_pair=False), GeneratorType) True >>> list(Triangle.gen(name_value_pair=False)) [<Triangle.EQUILATERAL: NamedTuple(first=6, second=6, third=6)>, <Triangle.RIGHT: NamedTuple(first=3, second=4, third=5)>]
- names(as_tuple: bool | None = True) Tuple | Generator [source]
Returns the names of all the enumeration items as a tuple, if parameter as_tuple is True; otherwise returns a generator.
- Parameters:
as_tuple (bool) – returns a tuple if True; otherwise returns a generator.
- Returns:
names of all the enumeration items inside the class in a specific form.
- Return type:
Union[Tuple, Generator]
Examples
>>> from types import GeneratorType >>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.names() () >>> isinstance(TripleEnum.names(as_tuple=False), GeneratorType) True >>> list(TripleEnum.names(as_tuple=False)) [] >>> Triangle.names() ('EQUILATERAL', 'RIGHT') >>> isinstance(Triangle.names(as_tuple=False), GeneratorType) True >>> list(Triangle.names(as_tuple=False)) ['EQUILATERAL', 'RIGHT']
- values(as_tuple: bool | None = True) Tuple | Generator [source]
Returns the values of all the enumeration items as a tuple, if parameter as_tuple is True, otherwise returns a generator.
- Parameters:
as_tuple (Optional[bool]) – returns a tuple if True; otherwise returns a generator. Default value is True.
- Returns:
values of all the enumeration items inside the class in a specific form.
- Return type:
Union[Tuple, Generator]
Examples
>>> from types import GeneratorType >>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.values() () >>> isinstance(TripleEnum.values(as_tuple=False), GeneratorType) True >>> list(TripleEnum.values(as_tuple=False)) [] >>> Triangle.values() (NamedTuple(first=6, second=6, third=6), NamedTuple(first=3, second=4, third=5)) >>> isinstance(Triangle.values(as_tuple=False), GeneratorType) True >>> list(Triangle.values(as_tuple=False)) [NamedTuple(first=6, second=6, third=6), NamedTuple(first=3, second=4, third=5)]
- class NamedEnumMeta(name: str, bases: Tuple, namespace: _NamedEnumDict)[source]
Extends the EnumMeta class for three purposes:
1. uses the _NamedEnumDict as the data type of the namespace parameter for __new__ function, such that we can use the namedtuple as the data type of the value of each enumeration item.
2. provides extra functions, which is independent of the variable _field_names_, such as names, values, as_dict, as_list, as_set, as_tuple, as_ordereddict, describe, gen. The aim is extending the Enum class for complicated use cases in software development.
3. provides functions for each field name defined in class variable _field_names_ in the NamedEnum class and its subclasses, for example:
assuming ‘key’ is included in _field_names_, then the functions for this field name are: keys, from_key, has_key.
- as_dict() Dict [source]
Converts the enumeration to a dict, in which the key is the name of the enumeration item and value is its value.
- Returns:
a dictionary containing name-value-pairs of the enumeration.
- Return type:
Dict
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.as_dict() {} >>> Triangle.as_dict() {'EQUILATERAL': NamedTuple(first=6, second=6, third=6), 'RIGHT': NamedTuple(first=3, second=4, third=5)}
- as_list() List [source]
Converts the enumerations to a list, in which each item is a tuple of the enumeration item’s name and value.
- Returns:
a list containing name-value-pairs of the enumeration.
- Return type:
List
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.as_list() [] >>> Triangle.as_list() [('EQUILATERAL', NamedTuple(first=6, second=6, third=6)), ('RIGHT', NamedTuple(first=3, second=4, third=5))]
- as_ordereddict() OrderedDict [source]
Converts the enumerations to an OrderedDict, in which each item is a tuple of the enumeration item’s name and value.
- Returns:
- an OrderedDict containing name-value-pairs of the
enumeration.
- Return type:
OrderedDict
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.as_ordereddict() OrderedDict() >>> Triangle.as_ordereddict() OrderedDict([('EQUILATERAL', NamedTuple(first=6, second=6, third=6)), ('RIGHT', NamedTuple(first=3, second=4, third=5))])
- as_set() Set [source]
Converts the enumerations to a set, in which each item is a tuple of the enumeration item’s name and value.
- Returns:
a set containing name-value-pairs of the enumeration.
- Return type:
Set
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.as_set() == set() True >>> isinstance(Triangle.as_set(), set) True >>> dict(Triangle.as_set()) == Triangle.as_dict() True
- as_tuple() Tuple [source]
Converts the enumerations to a tuple, in which each item is a tuple of the enumeration item’s name and value.
- Returns:
a tuple containing name-value-pairs of the enumeration.
- Return type:
Tuple
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.as_tuple() () >>> Triangle.as_tuple() (('EQUILATERAL', NamedTuple(first=6, second=6, third=6)), ('RIGHT', NamedTuple(first=3, second=4, third=5)))
- describe() None [source]
Prints in the console a table showing the content of the enumeration.
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.describe() Class: TripleEnum Name | First | Second | Third ----------------------------- >>> Triangle.describe() Class: Triangle Name | First | Second | Third ------------------------------------ EQUILATERAL | 6 | 6 | 6 RIGHT | 3 | 4 | 5
- gen(name_value_pair: bool | None = True) Generator [source]
Returns a generator of pairs consisting of each enumeration item’s name and value, if name_value_pair is True; otherwise a generator of the enumeration items.
- Parameters:
name_value_pair (Optional[bool]) – controls the return result. If true, returns the generator of name-value pair; if False, returns the generator of the enumeration items. Default value is True.
- Returns:
a generator which iterates all the enumeration items.
- Return type:
Generator
Examples
>>> from types import GeneratorType >>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> isinstance(TripleEnum.gen(), GeneratorType) True >>> list(TripleEnum.gen()) [] >>> isinstance(TripleEnum.gen(name_value_pair=False), GeneratorType) True >>> list(TripleEnum.gen(name_value_pair=False)) [] >>> isinstance(Triangle.gen(), GeneratorType) True >>> list(Triangle.gen()) [('EQUILATERAL', NamedTuple(first=6, second=6, third=6)), ('RIGHT', NamedTuple(first=3, second=4, third=5))] >>> isinstance(Triangle.gen(name_value_pair=False), GeneratorType) True >>> list(Triangle.gen(name_value_pair=False)) [<Triangle.EQUILATERAL: NamedTuple(first=6, second=6, third=6)>, <Triangle.RIGHT: NamedTuple(first=3, second=4, third=5)>]
- names(as_tuple: bool | None = True) Tuple | Generator [source]
Returns the names of all the enumeration items as a tuple, if parameter as_tuple is True; otherwise returns a generator.
- Parameters:
as_tuple (bool) – returns a tuple if True; otherwise returns a generator.
- Returns:
names of all the enumeration items inside the class in a specific form.
- Return type:
Union[Tuple, Generator]
Examples
>>> from types import GeneratorType >>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.names() () >>> isinstance(TripleEnum.names(as_tuple=False), GeneratorType) True >>> list(TripleEnum.names(as_tuple=False)) [] >>> Triangle.names() ('EQUILATERAL', 'RIGHT') >>> isinstance(Triangle.names(as_tuple=False), GeneratorType) True >>> list(Triangle.names(as_tuple=False)) ['EQUILATERAL', 'RIGHT']
- values(as_tuple: bool | None = True) Tuple | Generator [source]
Returns the values of all the enumeration items as a tuple, if parameter as_tuple is True, otherwise returns a generator.
- Parameters:
as_tuple (Optional[bool]) – returns a tuple if True; otherwise returns a generator. Default value is True.
- Returns:
values of all the enumeration items inside the class in a specific form.
- Return type:
Union[Tuple, Generator]
Examples
>>> from types import GeneratorType >>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> TripleEnum.values() () >>> isinstance(TripleEnum.values(as_tuple=False), GeneratorType) True >>> list(TripleEnum.values(as_tuple=False)) [] >>> Triangle.values() (NamedTuple(first=6, second=6, third=6), NamedTuple(first=3, second=4, third=5)) >>> isinstance(Triangle.values(as_tuple=False), GeneratorType) True >>> list(Triangle.values(as_tuple=False)) [NamedTuple(first=6, second=6, third=6), NamedTuple(first=3, second=4, third=5)]
named_enum.enum
Module for the classes extending the default Enum class. It contains four classes: ExtendedEnum, ExtendedEnum, LabeledEnum, PairEnum and one function namedenum.
- class NamedEnum(value)[source]
Through the value of variable _field_names_ to control its subclass for different use cases:
1. value of _field_names_ is None or empty. In this case, its subclass works like an extended Enum class with extra function: names, values, as_dict, as_list, as_set, as_tuple, as_ordereddict, describe.
2. value of _field_names_ is neither None or empty. In this case, its subclass keeps the extra functions mentioned in 1, and gives each element in the enumeration item’s value a name and provides functions for each attribute/field, like: <field_name>s, from_<field_name>, has_<field_name>.
Instead of the setting the attributes to the enumeration instance, it uses the function __getattr__ to achieve it.
Examples
>>> class TripleEnum(NamedEnum): ... _field_names_ = ("first", "second", "third") >>> class Triangle(TripleEnum): ... EQUILATERAL = (6, 6, 6) ... RIGHT = (3, 4, 5) >>> Triangle._fields() ('first', 'second', 'third') >>> Triangle.names() ('EQUILATERAL', 'RIGHT') >>> Triangle.values() (NamedTuple(first=6, second=6, third=6), NamedTuple(first=3, second=4, third=5)) >>> Triangle.describe() Class: Triangle Name | First | Second | Third ------------------------------------ EQUILATERAL | 6 | 6 | 6 RIGHT | 3 | 4 | 5 >>> Triangle.as_dict() {'EQUILATERAL': NamedTuple(first=6, second=6, third=6), 'RIGHT': NamedTuple(first=3, second=4, third=5)} >>> Triangle.as_list() [('EQUILATERAL', NamedTuple(first=6, second=6, third=6)), ('RIGHT', NamedTuple(first=3, second=4, third=5))] >>> Triangle.as_set() == {('RIGHT', Triangle._tuple_cls(first=3, second=4, third=5)), ... ('EQUILATERAL', Triangle._tuple_cls(first=6, second=6, third=6))} True >>> Triangle.as_tuple() (('EQUILATERAL', NamedTuple(first=6, second=6, third=6)), ('RIGHT', NamedTuple(first=3, second=4, third=5))) >>> Triangle.as_ordereddict() OrderedDict([('EQUILATERAL', NamedTuple(first=6, second=6, third=6)), ('RIGHT', NamedTuple(first=3, second=4, third=5))]) >>> Triangle.firsts() (6, 3) >>> Triangle.seconds() (6, 4) >>> Triangle.thirds() (6, 5) >>> Triangle.from_first(6) (<Triangle.EQUILATERAL: NamedTuple(first=6, second=6, third=6)>,) >>> Triangle.from_first(66) () >>> Triangle.from_second(6) (<Triangle.EQUILATERAL: NamedTuple(first=6, second=6, third=6)>,) >>> Triangle.from_second(66) () >>> Triangle.from_third(6) (<Triangle.EQUILATERAL: NamedTuple(first=6, second=6, third=6)>,) >>> Triangle.from_third(66) () >>> Triangle.has_first(6) True >>> Triangle.has_first(66) False >>> Triangle.has_second(6) True >>> Triangle.has_second(66) False >>> Triangle.has_third(6) True >>> Triangle.has_third(66) False >>> Triangle.RIGHT <Triangle.RIGHT: NamedTuple(first=3, second=4, third=5)> >>> Triangle.RIGHT.first 3 >>> Triangle.RIGHT.second 4 >>> Triangle.RIGHT.third 5 >>> Triangle.RIGHT.name 'RIGHT' >>> Triangle.RIGHT.value NamedTuple(first=3, second=4, third=5) >>> print(Triangle.RIGHT) Triangle.RIGHT: NamedTuple(first=3, second=4, third=5)
- _field_names_: Tuple[str] | None = None
The place to define the field names of the enumeration class. It accepts the same format as the parameter field_names in function namedtuple from collections package. It’s used in the NamedEnumMeta class’s __new__ function to generate the corresponding functions for each field. If it’s value is None or empty, then the enumeration class behaves like a normal Enum class, but with some extended functions to simplify the usages of enumerations.
Attention: this variable should not be used to get the field_names, to do so you can use the class method _fields. Because it also accept the comma separated string.
- class ExtendedEnum(value)[source]
An alias for the class NamedEnum.
The goal is explicit directly providing the users an Enum class with extra functions.
Examples
>>> from types import GeneratorType >>> class TVCouple(ExtendedEnum): ... GALLAGHERS = ("FRANK", "MONICA") ... MIKE_AND_MOLLY = ("Mike", "Molly") >>> TVCouple.names() ('GALLAGHERS', 'MIKE_AND_MOLLY') >>> isinstance(TVCouple.names(as_tuple=False), GeneratorType) True >>> list(TVCouple.names(as_tuple=False)) ['GALLAGHERS', 'MIKE_AND_MOLLY'] >>> TVCouple.values() (('FRANK', 'MONICA'), ('Mike', 'Molly')) >>> isinstance(TVCouple.values(as_tuple=False), GeneratorType) True >>> list(TVCouple.values(as_tuple=False)) [('FRANK', 'MONICA'), ('Mike', 'Molly')] >>> TVCouple.describe() Class: TVCouple Name | Value ------------------------------------ GALLAGHERS | ('FRANK', 'MONICA') MIKE_AND_MOLLY | ('Mike', 'Molly') >>> isinstance(TVCouple.gen(), GeneratorType) True >>> tuple(TVCouple.gen()) (('GALLAGHERS', ('FRANK', 'MONICA')), ('MIKE_AND_MOLLY', ('Mike', 'Molly'))) >>> isinstance(TVCouple.gen(name_value_pair=False), GeneratorType) True >>> tuple(TVCouple.gen(name_value_pair=False)) (<TVCouple.GALLAGHERS: ('FRANK', 'MONICA')>, <TVCouple.MIKE_AND_MOLLY: ('Mike', 'Molly')>) >>> TVCouple.as_dict() {'GALLAGHERS': ('FRANK', 'MONICA'), 'MIKE_AND_MOLLY': ('Mike', 'Molly')} >>> isinstance(TVCouple.as_set(), set) True >>> sorted(list(TVCouple.as_set())) [('GALLAGHERS', ('FRANK', 'MONICA')), ('MIKE_AND_MOLLY', ('Mike', 'Molly'))] >>> TVCouple.as_tuple() (('GALLAGHERS', ('FRANK', 'MONICA')), ('MIKE_AND_MOLLY', ('Mike', 'Molly'))) >>> TVCouple.as_list() [('GALLAGHERS', ('FRANK', 'MONICA')), ('MIKE_AND_MOLLY', ('Mike', 'Molly'))] >>> TVCouple.as_ordereddict() OrderedDict([('GALLAGHERS', ('FRANK', 'MONICA')), ('MIKE_AND_MOLLY', ('Mike', 'Molly'))])
- class LabeledEnum(value)[source]
An enumeration class with two attributes key and label.
It can be used in the Django project as the choices of a field in model or form.
Examples
>>> from types import GeneratorType >>> class NBALegendary(LabeledEnum): ... JOHNSON = ("Johnson", "Magic Johnson") ... Jordan = ("Jordan", "Air Jordan") >>> NBALegendary.names() ('JOHNSON', 'Jordan') >>> isinstance(NBALegendary.names(as_tuple=False), GeneratorType) True >>> list(NBALegendary.names(as_tuple=False)) ['JOHNSON', 'Jordan'] >>> NBALegendary.values() (NamedTuple(key='Johnson', label='Magic Johnson'), NamedTuple(key='Jordan', label='Air Jordan')) >>> isinstance(NBALegendary.values(as_tuple=False), GeneratorType) True >>> list(NBALegendary.values(as_tuple=False)) [NamedTuple(key='Johnson', label='Magic Johnson'), NamedTuple(key='Jordan', label='Air Jordan')] >>> NBALegendary.describe() Class: NBALegendary Name | Key | Label --------------------------------- JOHNSON | Johnson | Magic Johnson Jordan | Jordan | Air Jordan >>> isinstance(NBALegendary.gen(), GeneratorType) True >>> tuple(NBALegendary.gen()) (('JOHNSON', NamedTuple(key='Johnson', label='Magic Johnson')), ('Jordan', NamedTuple(key='Jordan', label='Air Jordan'))) >>> isinstance(NBALegendary.gen(name_value_pair=False), GeneratorType) True >>> tuple(NBALegendary.gen(name_value_pair=False)) (<NBALegendary.JOHNSON: NamedTuple(key='Johnson', label='Magic Johnson')>, <NBALegendary.Jordan: NamedTuple(key='Jordan', label='Air Jordan')>) >>> NBALegendary.as_dict() {'JOHNSON': NamedTuple(key='Johnson', label='Magic Johnson'), 'Jordan': NamedTuple(key='Jordan', label='Air Jordan')} >>> isinstance(NBALegendary.as_set(), set) True >>> sorted(list(NBALegendary.as_set())) [('JOHNSON', NamedTuple(key='Johnson', label='Magic Johnson')), ('Jordan', NamedTuple(key='Jordan', label='Air Jordan'))] >>> NBALegendary.as_tuple() (('JOHNSON', NamedTuple(key='Johnson', label='Magic Johnson')), ('Jordan', NamedTuple(key='Jordan', label='Air Jordan'))) >>> NBALegendary.as_list() [('JOHNSON', NamedTuple(key='Johnson', label='Magic Johnson')), ('Jordan', NamedTuple(key='Jordan', label='Air Jordan'))] >>> NBALegendary.as_ordereddict() OrderedDict([('JOHNSON', NamedTuple(key='Johnson', label='Magic Johnson')), ('Jordan', NamedTuple(key='Jordan', label='Air Jordan'))]) >>> NBALegendary.keys() ('Johnson', 'Jordan') >>> NBALegendary.labels() ('Magic Johnson', 'Air Jordan') >>> isinstance(NBALegendary.keys(as_tuple=False), GeneratorType) True >>> list(NBALegendary.keys(as_tuple=False)) ['Johnson', 'Jordan'] >>> isinstance(NBALegendary.labels(as_tuple=False), GeneratorType) True >>> list(NBALegendary.labels(as_tuple=False)) ['Magic Johnson', 'Air Jordan'] >>> NBALegendary.from_key('Johnson') (<NBALegendary.JOHNSON: NamedTuple(key='Johnson', label='Magic Johnson')>,) >>> NBALegendary.from_key('Jordan') (<NBALegendary.Jordan: NamedTuple(key='Jordan', label='Air Jordan')>,) >>> NBALegendary.from_label('Magic Johnson') (<NBALegendary.JOHNSON: NamedTuple(key='Johnson', label='Magic Johnson')>,) >>> NBALegendary.from_label('Air Jordan') (<NBALegendary.Jordan: NamedTuple(key='Jordan', label='Air Jordan')>,) >>> isinstance(NBALegendary.from_key('Johnson', as_tuple=False), GeneratorType) True >>> list(NBALegendary.from_key('Johnson', as_tuple=False)) [<NBALegendary.JOHNSON: NamedTuple(key='Johnson', label='Magic Johnson')>] >>> isinstance(NBALegendary.from_key('Jordan', as_tuple=False), GeneratorType) True >>> list(NBALegendary.from_key('Jordan', as_tuple=False)) [<NBALegendary.Jordan: NamedTuple(key='Jordan', label='Air Jordan')>] >>> isinstance(NBALegendary.from_label('Magic Johnson', as_tuple=False), GeneratorType) True >>> list(NBALegendary.from_label('Magic Johnson', as_tuple=False)) [<NBALegendary.JOHNSON: NamedTuple(key='Johnson', label='Magic Johnson')>] >>> isinstance(NBALegendary.from_label('Air Jordan', as_tuple=False), GeneratorType) True >>> list(NBALegendary.from_label('Air Jordan', as_tuple=False)) [<NBALegendary.Jordan: NamedTuple(key='Jordan', label='Air Jordan')>] >>> NBALegendary.has_key('Johnson') True >>> NBALegendary.has_key('John') False >>> NBALegendary.has_key('Jordan') True >>> NBALegendary.has_key('George') False >>> NBALegendary.has_label('Magic Johnson') True >>> NBALegendary.has_label('King James') False >>> NBALegendary.has_label('Air Jordan') True >>> NBALegendary.has_label('The Black Mamba') False
- _field_names_: Tuple[str] | None = ('key', 'label')
key, label
- Type:
Each enumeration of LabeledEnum has two attributes
- from_key(as_tuple: bool | None = True) Tuple | Generator
Returns a tuple of the defined enumeration items regarding to the given field_value of field key, if as_tuple is True; otherwise returns a generator.
- from_label(as_tuple: bool | None = True) Tuple | Generator
Returns a tuple of the defined enumeration items regarding to the given field_value of field label, if as_tuple is True; otherwise returns a generator.
- has_key() bool
Returns a boolean value which indicates if there is at least one enumeration item in which the value of the field key matches the given field_value.
- has_label() bool
Returns a boolean value which indicates if there is at least one enumeration item in which the value of the field label matches the given field_value.
- class PairEnum(value)[source]
Enumeration with two attributes first, second, the idea comes from the C++’s pair container.
Examples
>>> from types import GeneratorType >>> class Pair(PairEnum): ... TOM_AND_JERRY = ("Tom", "Jerry") ... BULLS = ("Micheal", "Pippen") >>> Pair.names() ('TOM_AND_JERRY', 'BULLS') >>> isinstance(Pair.names(as_tuple=False), GeneratorType) True >>> list(Pair.names(as_tuple=False)) ['TOM_AND_JERRY', 'BULLS'] >>> Pair.values() (NamedTuple(first='Tom', second='Jerry'), NamedTuple(first='Micheal', second='Pippen')) >>> isinstance(Pair.values(as_tuple=False), GeneratorType) True >>> list(Pair.values(as_tuple=False)) [NamedTuple(first='Tom', second='Jerry'), NamedTuple(first='Micheal', second='Pippen')] >>> Pair.describe() Class: Pair Name | First | Second -------------------------------- TOM_AND_JERRY | Tom | Jerry BULLS | Micheal | Pippen >>> isinstance(Pair.gen(), GeneratorType) True >>> tuple(Pair.gen()) (('TOM_AND_JERRY', NamedTuple(first='Tom', second='Jerry')), ('BULLS', NamedTuple(first='Micheal', second='Pippen'))) >>> isinstance(Pair.gen(name_value_pair=False), GeneratorType) True >>> tuple(Pair.gen(name_value_pair=False)) (<Pair.TOM_AND_JERRY: NamedTuple(first='Tom', second='Jerry')>, <Pair.BULLS: NamedTuple(first='Micheal', second='Pippen')>) >>> Pair.as_dict() {'TOM_AND_JERRY': NamedTuple(first='Tom', second='Jerry'), 'BULLS': NamedTuple(first='Micheal', second='Pippen')} >>> isinstance(Pair.as_set(), set) True >>> sorted(list(Pair.as_set())) [('BULLS', NamedTuple(first='Micheal', second='Pippen')), ('TOM_AND_JERRY', NamedTuple(first='Tom', second='Jerry'))] >>> Pair.as_tuple() (('TOM_AND_JERRY', NamedTuple(first='Tom', second='Jerry')), ('BULLS', NamedTuple(first='Micheal', second='Pippen'))) >>> Pair.as_list() [('TOM_AND_JERRY', NamedTuple(first='Tom', second='Jerry')), ('BULLS', NamedTuple(first='Micheal', second='Pippen'))] >>> Pair.as_ordereddict() OrderedDict([('TOM_AND_JERRY', NamedTuple(first='Tom', second='Jerry')), ('BULLS', NamedTuple(first='Micheal', second='Pippen'))]) >>> Pair.firsts() ('Tom', 'Micheal') >>> Pair.seconds() ('Jerry', 'Pippen') >>> isinstance(Pair.firsts(as_tuple=False), GeneratorType) True >>> list(Pair.firsts(as_tuple=False)) ['Tom', 'Micheal'] >>> isinstance(Pair.seconds(as_tuple=False), GeneratorType) True >>> list(Pair.seconds(as_tuple=False)) ['Jerry', 'Pippen'] >>> Pair.from_first("Tom") (<Pair.TOM_AND_JERRY: NamedTuple(first='Tom', second='Jerry')>,) >>> Pair.from_first("Micheal") (<Pair.BULLS: NamedTuple(first='Micheal', second='Pippen')>,) >>> Pair.from_second("Jerry") (<Pair.TOM_AND_JERRY: NamedTuple(first='Tom', second='Jerry')>,) >>> Pair.from_second("Pippen") (<Pair.BULLS: NamedTuple(first='Micheal', second='Pippen')>,) >>> isinstance(Pair.from_first("Tom", as_tuple=False), GeneratorType) True >>> list(Pair.from_first("Tom", as_tuple=False)) [<Pair.TOM_AND_JERRY: NamedTuple(first='Tom', second='Jerry')>] >>> isinstance(Pair.from_first("Micheal", as_tuple=False), GeneratorType) True >>> list(Pair.from_first("Micheal", as_tuple=False)) [<Pair.BULLS: NamedTuple(first='Micheal', second='Pippen')>] >>> isinstance(Pair.from_second("Jerry", as_tuple=False), GeneratorType) True >>> list(Pair.from_second("Jerry", as_tuple=False)) [<Pair.TOM_AND_JERRY: NamedTuple(first='Tom', second='Jerry')>] >>> isinstance(Pair.from_second("Pippen", as_tuple=False), GeneratorType) True >>> list(Pair.from_second("Pippen", as_tuple=False)) [<Pair.BULLS: NamedTuple(first='Micheal', second='Pippen')>] >>> Pair.has_first('Tom') True >>> Pair.has_first('Tommy') False >>> Pair.has_first('Micheal') True >>> Pair.has_first('Mike') False >>> Pair.has_second('Jerry') True >>> Pair.has_second('Jeremy') False >>> Pair.has_second('Pippen') True >>> Pair.has_second('Pepe') False
- _field_names_: Tuple[str] | None = ('first', 'second')
first, second
- Type:
Each enumeration of PairEnum has two attributes
- firsts() Tuple | Generator
Collective method to return the values of the attribute first from all the enumeration items.
- from_first(as_tuple: bool | None = True) Tuple | Generator
Returns a tuple of the defined enumeration items regarding to the given field_value of field first, if as_tuple is True; otherwise returns a generator.
- namedenum(typename: str, field_names: Sequence | None = None, *, verbose: bool | None = False, module: str | None = None) object [source]
Creates an named enum class with the given typename as class name and field_names as the _field_names_ in named enum class. The implementation is similar to the namedtuple function.
- Parameters:
- Returns:
subclass of NamedEnum
- Return type:
Examples
>>> TripleEnum = namedenum("TripleEnum", ("first", "second", "third")) >>> TripleEnum <named enum 'TripleEnum'>