命名枚举
named-enum的Python项目详细描述
简介
这个包提供了几个枚举类,它使用各种功能扩展了默认的enum类。对于每个枚举类,其枚举项的值是由namedtuple从collections包生成的自定义元组类型。
这个软件包的灵感来自Cristian Alfonso González Mora。
安装
pip install named-enum
运行试验
python setup.py test
快速启动
枚举创建
创建枚举有两种方法。
使用提供的枚举类ExtendedEnum,LabeledEnum,PairEnum声明枚举。
fromnamed_enumimportExtendedEnum,LabeledEnum,PairEnumclassTVCouple(ExtendedEnum):GALLAGHERS=("FRANK","MONICA")MIKE_AND_MOLLY=("Mike","Molly")classNBALegendary(LabeledEnum):JOHNSON=("Johnson","Magic Johnson")JORDAN=("Jordan","Air Jordan")classPair(PairEnum):TOM_AND_JERRY=("Tom","Jerry")BULLS=("Micheal","Pippen")
自定义您自己的枚举类并使用它定义枚举。
- 创建新的枚举类
inherit from class ^{tt4}$
fromnamed_enumimportNamedEnumclassTripleEnum(NamedEnum):"""using a sequence of strings to define the field names"""_field_names_=("first","second","third")
use function ^{tt5}$
fromnamed_enumimportnamedenum# using a sequence of strings to define the field namesTripleEnum=namedenum("TripleEnum",("first","second","third"))# using a comma/space separated string to define the field namesTripleEnum=namedenum("LabelEnum","key, label")
在最后一步中使用自定义枚举类创建枚举。
classAnimationFamily(TripleEnum):SIMPSONS=("Homer","Bart","Marge")DUCKS=("Huey","Dewey","Louie")
用法
- names(as_tuple=True)
as_tuple=True:以元组的形式返回所有枚举项的名称。
>>>AnimationFamily.names()('SIMPSONS','DUCKS')
as_tuple=False:返回所有枚举项名称的生成器。
>>>fromtypesimportGeneratorType>>>isinstance(AnimationFamily.names(as_tuple=False),GeneratorType)True
- values(as_tuple=True)
as_tuple=True:以元组的形式返回所有枚举项的值。
# TripleEnum>>>AnimationFamily.values()(NamedTuple(first='Homer',second='Bart',third='Marge'),NamedTuple(first='Huey',second='Dewey',third='Louie'))# ExtendedEnum>>>TVCouple.values()(('FRANK','MONICA'),('Mike','Molly'))
as_tuple=False:返回所有枚举项的值的生成器。
>>>importtypes>>>isinstance(AnimationFamily.values(as_tuple=False),GeneratorType)True
- describe()
将枚举显示为表。
# TripleEnum>>>AnimationFamily.describe()Class:AnimationFamilyName|First|Second|Third---------------------------------SIMPSONS|Homer|Bart|MargeDUCKS|Huey|Dewey|Louie<BLANKLINE># ExtendedEnum>>>TVCouple.describe()Class:TVCoupleName|Value------------------------------------GALLAGHERS|('FRANK','MONICA')MIKE_AND_MOLLY|('Mike','Molly')<BLANKLINE>
- gen(name_value_pair=True)
name_value_pair=True:返回由每个枚举项的名称-值对组成的生成器
# TripleEnum>>>tuple(AnimationFamily.gen())(('SIMPSONS',NamedTuple(first='Homer',second='Bart',third='Marge')),('DUCKS',NamedTuple(first='Huey',second='Dewey',third='Louie')))# ExtendedEnum>>>tuple(TVCouple.gen())(('GALLAGHERS',('FRANK','MONICA')),('MIKE_AND_MOLLY',('Mike','Molly')))
name_value_pair=False:返回枚举项的生成器
# TripleEnum>>>tuple(AnimationFamily.gen(name_value_pair=False))(<AnimationFamily.SIMPSONS:NamedTuple(first='Homer',second='Bart',third='Marge')>,<AnimationFamily.DUCKS:NamedTuple(first='Huey',second='Dewey',third='Louie')>)# ExtendedEnum>>>tuple(TVCouple.gen(name_value_pair=False))(<TVCouple.GALLAGHERS:('FRANK','MONICA')>,<TVCouple.MIKE_AND_MOLLY:('Mike','Molly')>)
- as_dict()
返回字典,其中键是枚举项的名称,值是项的值
# TripleEnum>>>AnimationFamily.as_dict(){'SIMPSONS':NamedTuple(first='Homer',second='Bart',third='Marge'),'DUCKS':NamedTuple(first='Huey',second='Dewey',third='Louie')}# ExtendedEnum>>>TVCouple.as_dict(){'GALLAGHERS':('FRANK','MONICA'),'MIKE_AND_MOLLY':('Mike','Molly')}
- as_set()
返回一组元组,其中包含枚举项的名称和值
# TripleEnum>>>AnimationFamily.as_set(){('SIMPSONS',NamedTuple(first='Homer',second='Bart',third='Marge')),('DUCKS',NamedTuple(first='Huey',second='Dewey',third='Louie'))}# ExtendedEnum>>>TVCouple.as_set(){('GALLAGHERS',('FRANK','MONICA')),('MIKE_AND_MOLLY',('Mike','Molly'))}
- as_tuple()
返回包含枚举项的名称和值的元组的元组
# TripleEnum>>>AnimationFamily.as_tuple()(('SIMPSONS',NamedTuple(first='Homer',second='Bart',third='Marge')),('DUCKS',NamedTuple(first='Huey',second='Dewey',third='Louie')))# ExtendedEnum>>>TVCouple.as_tuple()(('GALLAGHERS',('FRANK','MONICA')),('MIKE_AND_MOLLY',('Mike','Molly')))
- as_list()
返回包含枚举项的名称和值的元组列表
# TripleEnum>>>AnimationFamily.as_list()[('SIMPSONS',NamedTuple(first='Homer',second='Bart',third='Marge')),('DUCKS',NamedTuple(first='Huey',second='Dewey',third='Louie'))]# ExtendedEnum>>>TVCouple.as_list()[('GALLAGHERS',('FRANK','MONICA')),('MIKE_AND_MOLLY',('Mike','Molly'))]
- as_ordereddict()
返回一个有序的dict,其中键是枚举项的名称,值是项的值
# TripleEnum>>>AnimationFamily.as_ordereddict()OrderedDict([('SIMPSONS',NamedTuple(first='Homer',second='Bart',third='Marge')),('DUCKS',NamedTuple(first='Huey',second='Dewey',third='Louie'))])# ExtendedEnum>>>TVCouple.as_ordereddict()OrderedDict([('GALLAGHERS',('FRANK','MONICA')),('MIKE_AND_MOLLY',('Mike','Molly'))])
如果使用_field_names_变量定义枚举类,则为其中的每个字段名生成3个相应的函数并将其分配给枚举类:
- ^{tt22}$
^{tt7}$: returns a tuple containing all corresponding values of the field in enumeration items
# TripleEnum>>>AnimationFamily.firsts()('Homer','Huey')>>>AnimationFamily.seconds()('Bart','Dewey')>>>AnimationFamily.thirds()('Marge','Louie')# LabeledEnum>>>NBALegendary.keys()('Johnson','Jordan')>>>NBALegendary.labels()('Magic Johnson','Air Jordan')^{tt8}$: returns a generator of all corresponding values of the field in enumeration items
# TripleEnum>>>isinstance(AnimationFamily.firsts(as_tuple=False),GeneratorType)True
- ^{tt25}$
^{tt7}$: returns a tuple containing all enumeration items which has the given ^{tt27}$ in corresponding field
# TripleEnum>>>AnimationFamily.from_first('Homer')(<AnimationFamily.SIMPSONS:NamedTuple(first='Homer',second='Bart',third='Marge')>,)>>>AnimationFamily.from_second('Dewey')(<AnimationFamily.DUCKS:NamedTuple(first='Huey',second='Dewey',third='Louie')>,)>>>AnimationFamily.from_third('Marge')(<AnimationFamily.SIMPSONS:NamedTuple(first='Homer',second='Bart',third='Marge')>,)# LabeledEnum>>>NBALegendary.from_key('Johnson')(<NBALegendary.JOHNSON:NamedTuple(key='Johnson',label='Magic Johnson')>,)>>>NBALegendary.from_label('Air Jordan')(<NBALegendary.Jordan:NamedTuple(key='Jordan',label='Air Jordan')>,)^{tt8}$: returns a generator of all enumeration items which has the given ^{tt27}$ in corresponding field
# TripleEnum>>>isinstance(AnimationFamily.from_first('Homer',as_tuple=False),GeneratorType)True
- ^{tt30}$
returns a boolean value to indicate whether there is at least one enumeration item has the given ^{tt27}$ in corresponding field
# TripleEnum>>>AnimationFamily.has_first('Homer')True>>>AnimationFamily.has_first('Holmes')False>>>AnimationFamily.has_second('Dewey')True>>>AnimationFamily.has_second('David')False>>>AnimationFamily.has_third('Louie')True>>>AnimationFamily.has_third('Louis')False# LabeledEnum>>>NBALegendary.has_key('Johnson')True>>>NBALegendary.has_key('John')False>>>NBALegendary.has_label('Air Jordan')True>>>NBALegendary.has_label('The Black Mamba')False
文档
有关此项目的文档可在 Read the Docs。