Source Code¶
Module for the classes extending the default Enum class. It contains 5 classes: ExtendedEnumMeta, ExtendedEnum, ExtendedEnum, LabeledEnum, PairEnum and one method namedenum.
-
class
NamedEnumMeta
[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
()[source]¶ Returns the defined field names as a tuple for the enumeration class.
If the variable _field_names_ is None or empty value, then returns an empty tuple.
Returns: tuple of field names Return type: tuple >>> 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
()[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 >>> 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
()[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 >>> 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
()[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 >>> 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
()[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 >>> 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
()[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 >>> 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
()[source]¶ Prints in the console a table showing the content of the enumeration.
>>> 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 ----------------------------- <BLANKLINE> >>> Triangle.describe() Class: Triangle Name | First | Second | Third ------------------------------------ EQUILATERAL | 6 | 6 | 6 RIGHT | 3 | 4 | 5 <BLANKLINE>
-
gen
(name_value_pair=True)[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 (bool) – controls the return result. If true, returns the generator of name-value pair; if False, returns the generator of the enumeration items. Returns: a generator which iterates all the enumeration items Return type: Generator >>> 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=True)[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: tuple, Generator >>> 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=True)[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 (bool) – returns a tuple if True; otherwise returns a generator Returns: values of all the enumeration items inside the class in a specific form Return type: tuple, Generator >>> 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
NamedEnum
[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.
>>> 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 <BLANKLINE> >>> 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_
= 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
[source]¶ An alias for the class NamedEnum.
The goal is explicit directly providing the users an Enum class with extra functions.
>>> 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') <BLANKLINE> >>> 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
[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.
>>> 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 <BLANKLINE> >>> 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_
= ('key', 'label')¶ Each enumeration of LabeledEnum has two attributes: key, label
-
keys
(as_tuple=True)¶ Collective method to return the values of the attribute key from all the enumeration items.
Parameters: as_tuple (bool) – returns a tuple of the values if True; otherwise returns a generator Returns: corresponding values of the field key in all enumeration items Return type: tuple, Generator >>> NBALegendary.keys() ('Johnson', 'Jordan') >>> isinstance(NBALegendary.keys(as_tuple=False), GeneratorType) True >>> list(NBALegendary.keys(as_tuple=False)) ['Johnson', 'Jordan']
-
labels
(as_tuple=True)¶ Collective method to return the values of the attribute label from all the enumeration items.
Parameters: as_tuple (bool) – returns a tuple of the values if True; otherwise returns a generator Returns: corresponding values of the field label in all enumeration items Return type: tuple, Generator >>> NBALegendary.labels() ('Magic Johnson', 'Air Jordan') >>> isinstance(NBALegendary.labels(as_tuple=False), GeneratorType) True >>> list(NBALegendary.labels(as_tuple=False)) ['Magic Johnson', 'Air Jordan']
-
from_key
(field_value, as_tuple=True)¶ 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.
Parameters: Returns: collection of enumeration items whose has the given field_value as value of key
Return type: >>> NBALegendary.from_key('Johnson') (<NBALegendary.JOHNSON: NamedTuple(key='Johnson', label='Magic Johnson')>,) >>> NBALegendary.from_key('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')>]
-
from_label
(field_value, as_tuple=True)¶ 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.
Parameters: Returns: collection of enumeration items whose has the given field_value as value of label
Return type: >>> 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_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')>]
-
has_key
(field_value)¶ 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.
Parameters: field_value (int, str, object, ..) – value of the field key to search for Returns: True, if has at least one matching; otherwise False. Return type: bool >>> NBALegendary.has_key('Johnson') True >>> NBALegendary.has_key('John') False >>> NBALegendary.has_key('Jordan') True >>> NBALegendary.has_key('George') False
-
has_label
(field_value)¶ 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.
Parameters: field_value (int, str, object, ..) – value of the field label to search for Returns: True, if has at least one matching; otherwise False. Return type: bool >>> 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
-
-
class
PairEnum
[source]¶ Enumeration with two attributes first, second, the idea comes from the C++’s pair container.
>>> 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 <BLANKLINE> >>> 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_
= ('first', 'second')¶ Each enumeration of PairEnum has two attributes: first, second
-
firsts
(as_tuple=True)¶ Collective method to return the values of the attribute first from all the enumeration items.
Parameters: as_tuple (bool) – returns a tuple of the values if True; otherwise returns a generator Returns: corresponding values of the field first in all enumeration items Return type: tuple, Generator >>> Pair.firsts() ('Tom', 'Micheal') >>> isinstance(Pair.firsts(as_tuple=False), GeneratorType) True >>> list(Pair.firsts(as_tuple=False)) ['Tom', 'Micheal']
-
seconds
(as_tuple=True)¶ Collective method to return the values of the attribute second from all the enumeration items.
Parameters: as_tuple (bool) – returns a tuple of the values if True; otherwise returns a generator Returns: corresponding values of the field second in all enumeration items Return type: tuple, Generator >>> Pair.seconds() ('Jerry', 'Pippen') >>> isinstance(Pair.seconds(as_tuple=False), GeneratorType) True >>> list(Pair.seconds(as_tuple=False)) ['Jerry', 'Pippen']
-
from_first
(field_value, as_tuple=True)¶ 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.
Parameters: Returns: collection of enumeration items whose has the given field_value as value of first
Return type: >>> Pair.from_first("Tom") (<Pair.TOM_AND_JERRY: NamedTuple(first='Tom', second='Jerry')>,) >>> Pair.from_first("Micheal") (<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')>]
-
from_second
(field_value, as_tuple=True)¶ Returns a tuple of the defined enumeration items regarding to the given field_value of field second, if as_tuple is True; otherwise returns a generator.
Parameters: Returns: collection of enumeration items whose has the given field_value as value of second
Return type: >>> 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_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')>]
-
has_first
(field_value)¶ Returns a boolean value which indicates if there is at least one enumeration item in which the value of the field first matches the given field_value.
Parameters: field_value (int, str, object, ..) – value of the field first to search for Returns: True, if has at least one matching; otherwise False. Return type: bool >>> Pair.has_first('Tom') True >>> Pair.has_first('Tommy') False >>> Pair.has_first('Micheal') True >>> Pair.has_first('Mike') False
-
has_second
(field_value)¶ Returns a boolean value which indicates if there is at least one enumeration item in which the value of the field second matches the given field_value.
Parameters: field_value (int, str, object, ..) – value of the field second to search for Returns: True, if has at least one matching; otherwise False. Return type: bool >>> Pair.has_second('Jerry') True >>> Pair.has_second('Jeremy') False >>> Pair.has_second('Pippen') True >>> Pair.has_second('Pepe') False
-
-
namedenum
(typename, field_names=None, *, verbose=False, module=None)[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: >>> TripleEnum = namedenum("TripleEnum", ("first", "second", "third")) >>> TripleEnum <named enum 'TripleEnum'>