命名枚举

named-enum的Python项目详细描述


https://img.shields.io/pypi/l/named-enum.svgDocumentation Statushttps://travis-ci.com/KnightConan/named_enum.svg?branch=masterhttps://ci.appveyor.com/api/projects/status/fi9ayfyo1w4oi2w8/branch/master?svg=truehttps://img.shields.io/pypi/v/named-enum.svghttps://img.shields.io/pypi/status/named-enum.svgPython 3.6 | Python 3.7https://img.shields.io/pypi/format/named-enum.svghttps://codecov.io/gh/KnightConan/named_enum/branch/master/graph/badge.svgUpdateshttps://pepy.tech/badge/named-enumhttps://img.shields.io/badge/saythanks.io-☼-1EAEDB.svg

简介

这个包提供了几个枚举类,它使用各种功能扩展了默认的enum类。对于每个枚举类,其枚举项的值是由namedtuplecollections包生成的自定义元组类型。

这个软件包的灵感来自Cristian Alfonso González Mora

如果你喜欢,请把它放在github里。

安装

pip install named-enum

运行试验

python setup.py test

快速启动

枚举创建

创建枚举有两种方法。

  • 使用提供的枚举类ExtendedEnumLabeledEnumPairEnum声明枚举。

    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")
  • 自定义您自己的枚举类并使用它定义枚举。

    1. 创建新的枚举类
    • 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")
    1. 在最后一步中使用自定义枚举类创建枚举。

      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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JavaBeanio如何将抽象类或接口映射为记录或段   java Jboss 4.2.2到Jboss 7.1.1的迁移问题   如果Java运行时高于给定版本,则强制Maven失败   java在部署时持久化实体   java如何使用jdatechooser从mysql数据库中保存和检索空日期   java Google Drive SDK如何获取文件所在的文件夹?   java使用spring mvc mybatis从oracle db获取失败用户登录结果的数量   数组如何在java中拆分数字文件?   创建对象期间出现安卓 Java空指针异常   java 安卓supportv4。jar在Eclipse中未正确导入   java如何在javafx中创建这种类型的按钮   关于Spring集成和原型范围的java之谜   java正则表达式:在2个标记之间提取DNA信息   使用getText()时出现java空指针异常。toString()。修剪();   java如何从spring控制器获取angularjs中的模型属性值