tri.token提供了丰富的枚举功能

tri.token的Python项目详细描述


https://travis-ci.org/TriOptima/tri.token.svg?branch=masterhttps://codecov.io/github/TriOptima/tri.token/coverage.svg?branch=master

三.代币

token提供了丰富的枚举功能。tri.token枚举结构使用:

  • tokenattribute:支持动态值的可重写属性定义。
  • token:保存tokenattributes对象。
  • 令牌容器:保存令牌对象。

换言之:令牌是具有令牌实例成员的枚举。令牌实例在令牌容器中声明。

基本用法

fromtri_tokenimportToken,TokenAttribute,TokenContainer,PRESENTclassTaste(Token):name=TokenAttribute()display_name=TokenAttribute(value=lambda**kwargs:kwargs['name'].upper()+'!!')opinion=TokenAttribute()classTastes(TokenContainer):vanilla=Taste()pecan_nut=Taste(display_name="pecan nutz",opinion="Tasty")# A TokenContainer is a collection of Token objects.assertTastes.vanillainTastes# The order of Token objects in a TokenContainer is by order of declaration.assertlist(Tastes)==[Tastes.vanilla,Tastes.pecan_nut]assertlist(Tastes)!=[Tastes.pecan_nut,Tastes.vanilla]# Magic for 'name' TokenAttribute. It is set automatically from the token declaration within it's container.assertTastes.vanilla.name=="vanilla"# A TokenAttribute will have a None value if not set during Token instantiation.assertTastes.vanilla.opinionisNone# A TokenAttribute can have a dynamic value, derived from the invocation to the callable# set as 'value' in the TokenAttribute definition# (see declaration of 'display_name' TokenAttribute further up in the code).# The real value of the token attribute will be the return value of an invocation to said callable.# The invocation will receive the values of all other token attributes passed as keyword arguments.assertTastes.vanilla.display_name=="VANILLA!!"# TokenAttribute dynamic value behavior is overridden/not used if value is set explicitly during Token instantiation.assertTastes.pecan_nut.display_name=="pecan nutz"# A TokenContainer can be rendered as csv, excel, rst etcassert"""\
+--------------+---------+
| display_name | opinion |
+==============+=========+
|  VANILLA!!   |         |
+--------------+---------+
|  pecan nutz  |  Tasty  |
+--------------+---------+\
"""==Tastes.to_rst(['display_name','opinion'])

可选令牌属性

# A TokenAttribute may be declared as having optional dynamic values.# That is, we want these dynamic attributes to be evaluated sometimes, but not always.# In the example below, we want some superheroes to have homes, but not others.SUPERHERO_HOMES={'superman':'Fortress of Solitude','batman':'Batcave'}classSuperhero(Token):name=TokenAttribute()home=TokenAttribute(optional_value=lambdaname,**_:SUPERHERO_HOMES[name])# The PRESENT special value is used during Token instantiation to decide what# optional token attributes should be evaluated.classSuperheroes(TokenContainer):batman=Superhero(home=PRESENT)hawkman=Superhero()wonder_woman=Superhero(home='Themyscira')# Batman has a home, but poor Hawkman does not.assertSuperheroes.batman.home=='Batcave'assertSuperheroes.hawkman.homeisNone# Just as with dynamic attributes, the logic for TokenAttribute optional dynamic values is overriddenifvalueissetexplicitlyduringTokeninstantiation.assertSuperheroes.wonder_woman.home='Themyscira'# As a shortcut, PRESENT for specific optional token attributes may be assigned to# variables, and used in declarations, for enhanced readability.# This is useful when one has tokens with many attributes declared using dynamic values,# but we don't want all of them to be evaluated in all tokens.home=PRESENT('home')classSuperheroes(TokenContainer):batman=Superhero(home)hawkman=Superhero()# Again, Batman has a home, but poor Hawkman does not.assertSuperheroes.batman.home=='Batcave'assertSuperheroes.hawkman.homeisNone

tokenattribute继承

classFooToken(Token):foo=TokenAttribute(value=lambda**kwargs:'foo_value')classBarToken(Token):bar=TokenAttribute()classFieToken(FooToken,BarToken):fie=TokenAttribute()classFooBarFieTokenContainer(TokenContainer):t=FieToken(fie=3)assertdict(FooBarFieTokenContainer.t)=={'foo':'foo_value','bar':None,'name':'t','fie':3}

tokenattribute容器继承

classMyToken(Token):name=TokenAttribute()stuff=TokenAttribute()classMyTokens(TokenContainer):foo=MyToken(stuff='Hello')bar=MyToken(stuff='World')assertMyTokens.fooinMyTokensclassMoreTokens(MyTokens):boink=MyToken(stuff='Other Stuff')assertMyTokens.fooinMoreTokensassertlist(MoreTokens)==[MyTokens.foo,MyTokens.bar,MoreTokens.boink]assertMoreTokens.fooisMyTokens.foo

有关更多tri.token示例,请查看安装目录中tests/test_tokens.py的内容。

运行测试

你需要安装毒物,然后只需进行测试。

许可证

bsd

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

推荐PyPI第三方库


热门话题
Java类之间并发性不一致的HashMap   插件如何在JavaSwing中使用UIManager和Classloader从外部jar安装外观?   java JasperReports:找不到子报表   在项目中找不到java生成的Javadoc文件   java BigDecimal。multiply()和divide()方法返回十六进制数。为什么?   java统计出现次数并从字符串中删除重复项   调用运算符时发生java NullPointerException   Spring和Hibernate之间的java配置错误   JavaZK将用户重定向回上一页   Javasocket为传出连接指定特定的网络接口   如果拖动到某个区域外,java Make按钮操作将被取消   如何在Eclipse for selenium 3.141.59中添加Java文档链接   java从匹配条件的数组中获取所有索引   docker未连接到RemoteWebDriver的java Gitlab ci selenium测试   java重写run方法   utf 8如何使用java解码UTF8编码的字符串?   java如何从eclipse调试部署在tomcat上的web应用程序?   将字母字符与前面没有百分号的Java正则表达式匹配