在python中用户友好地实现enum

easy-enum的Python项目详细描述


Build StatusCoverage StatusPyPI StatusPython Version

python的文档化Enum类型的用户友好实现 语言。

安装

$ pip install easy_enum

要从shell中的master branch execute安装最新版本 以下命令:

$ pip install -U https://github.com/molejar/pyEnum/archive/master.zip

如果要进行开发,请从以下源安装pyenum:

$ git clone https://github.com/molejar/pyEnum.git
$ cd pyEnum
$ pip install -U -e .

运行这些命令可能会遇到权限问题。这里有一个 很少有解决方法:

  1. 使用sudo运行以全局安装pyenum和依赖项
  2. 指定--user选项以本地安装到您的家庭中 目录(如果没有,则将“~/.local/bin”导出到路径变量中)。
  3. virtualenv本地到 特定项目工作集。

用法

下面的示例演示如何在 代码:

fromeasy_enumimportEnumclassTestEnum(Enum):# attribute with no description, the name will be 'FIRST_ITEM' and empty string as descriptionFIRST_ITEM=1# attribute with descriptionSECOND_ITEM=(2,'Description for second item')# attribute with description and custom string nameTHIRD_ITEM=(3,'third','Description for third item')# attribute with custom string name (the description must be specified as empty string)FOURTH_ITEM=(4,'fourth','')# Read attributes value and nameprint(TestEnum.SECOND_ITEM)# 2print(TestEnum['FIRST_ITEM'])# 1print(TestEnum[1])# 'FIRST_ITEM'print(TestEnum[3])# 'third'print(TestEnum['third'])# 3# Use get method with default value if want skip exceptionprint(TestEnum.get(8))# Noneprint(TestEnum.get('eight'))# Noneprint(TestEnum.get(8,'eight'))# 'eight'# Check if exist attribute with specific valueprint(1inTestEnum)# Trueprint(8inTestEnum)# False# Check if exist attribute with specific nameprint('first'inTestEnum)# Falseprint('third'inTestEnum)# True# Get attribute description (as parameter use attribute name or value)print(TestEnum.desc(1))# ''print(TestEnum.desc(2))# 'Description for second item'print(TestEnum.desc('third'))# 'Description for third item'# Get count of all attributesprint(len(TestEnum))# 4# Get list with all attributes namenames=[item[0]foriteminTestEnum]print(names)# ['FIRST_ITEM', 'SECOND_ITEM', 'third', 'fourth']# Get list with all attributes valuevalues=[item[1]foriteminTestEnum]print(values)# [1, 2, 3, 4]# Read all itemsforname,value,descinTestEnum:print('{} = {} ({})'.format(name,value,desc))

欢迎加入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中的模型属性值