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对象数组   不支持java PageNotFound请求方法“GET”   java JDBC未选择任何数据库   java正在获取ssl。SSLHandshakeException在使用带有标头的REST客户端时发生,但在使用PostMan时可以正常工作   java测试与junit 5的接口   多线程Java Publisher服务器聊天程序   java编译错误已尝试修复,但没有成功   在TIBCO上安装java Https证书   java如何解析一个困难的问题。txt文件?   java如何使用ApachePOI3.6和ApachePOIOXML3读取pptx文件的内容。15罐?   java使用Gradle运行特定于Android的检测测试   人工智能回溯Java   用java制作螺旋桨动画   spring hibernate查询中的java日期   java读取HDFS小型分区?