acrilib是python中常用的编程模式的python库

acrilib的Python项目详细描述


Overview

acrilib is a python library providing useful programming patterns and tools. acrilib started as Acrisel’s internal idioms and utilities for programmers. The main key is that this library is completely independent. It does not use any external packages beside what provided by Python.

It includes:
  1. programming idioms that are repeatedly used by programmers.
  2. helpers functions for logging and other utilities.

We decided to contribute this library to Python community as a token of appreciation to what this community enables us.

We hope that you will find this library useful and helpful as we find it.

If you have comments or insights, please don’t hesitate to contact us at support@acrisel.com

Programming Idoms

threaded

decorator for methods that can be executed as a thread. RetriveAsycValue callable class used in the example below provide means to access results. One can provide their own callable to pass results.

示例

fromacrisimportthreaded,RetriveAsycValuefromtimeimportsleepclassThreadedExample(object):@threadeddefproc(self,id_,num,stall):s=numwhilenum>0:print("%s: %s"%(id_,s))num-=1s+=stallsleep(stall)print("%s: %s"%(id_,s))returns

示例输出

print("starting workers")te1=ThreadedExample().proc('TE1',3,1)te2=ThreadedExample().proc('TE2',3,1)print("collecting results")te1_callback=RetriveAsycValue('te1')te1.addCallback(te1_callback)te2_callback=RetriveAsycValue('te2')te2.addCallback(te2_callback)print('joining t1')te1.join()print('joined t1')print('%s callback result: %s'%(te1_callback.name,te1_callback.result))result=te1.syncResult()print('te1 syncResult : %s'%result)result=te2.syncResult()print('te2 syncResult : %s'%result)print('%s callback result: %s'%(te2_callback.name,te2_callback.result))

will produce:

startingworkersTE1:3TE2:3collectingresultsjoiningt1TE1:4TE2:4TE1:5TE2:5TE1:6TE2:6joinedt1te1callbackresult:6te1syncResult:6te2syncResult:6te2callbackresult:6

Singleton and NamedSingleton

meta class that creates singleton footprint of classes inheriting from it.

单例
fromacrisimportSingletonclassSequence(Singleton):step_id=0def__call__(self):step_id=self.step_idself.step_id+=1returnstep_id

示例输出

A=Sequence()print('A',A())print('A',A())B=Sequence()print('B',B())

will produce:

A0A1B2

命名singleton示例
fromacrisimportSingletonclassSequence(NamedSingleton):step_id=0def__init__(self,name=''):self.name=namedef__call__(self,):step_id=self.step_idself.step_id+=1returnstep_id

示例输出

A=Sequence('A')print(A.name,A())print(A.name,A())B=Sequence('B')print(B.name,B())

will produce:

A0A1B0

Sequence

meta class to produce sequences. Sequence allows creating different sequences using name tags.

示例

fromacrisimportSequenceA=Sequence('A')print('A',A())print('A',A())B=Sequence('B')print('B',B())A=Sequence('A')print('A',A())print('A',A())B=Sequence('B')print('B',B())

示例输出

A0A1B0A2A3B1

Decorators

Useful decorators for production and debug.

追踪法

logs entry and exit of function or method.

fromacrisimporttraced_methodtraced=traced_method(print,print_args=True,print_result=True)classOper(object):def__init__(self,value):self.value=valuedef__repr__(self):returnstr(self.value)@traceddefmul(self,value):self.value*=valuereturnself@traceddefadd(self,value):self.value+=valuereturnselfo=Oper(3)print(o.add(2).mul(5).add(7).mul(8))

would result with the following output:

[add][entering][args:(2)][kwargs:{}][trace_methods.py.Oper(39)][add][exiting][timespan:0:00:00.000056][result:5][trace_methods.py.Oper(39)][mul][entering][args:(5)][kwargs:{}][trace_methods.py.Oper(34)][mul][exiting][timespan:0:00:00.000010][result:25][trace_methods.py.Oper(34)][add][entering][args:(7)][kwargs:{}][trace_methods.py.Oper(39)][add][exiting][timespan:0:00:00.000007][result:32][trace_methods.py.Oper(39)][mul][entering][args:(8)][kwargs:{}][trace_methods.py.Oper(34)][mul][exiting][timespan:0:00:00.000008][result:256][trace_methods.py.Oper(34)]256

Data Types

varies derivative of Python data types

合并链信息技术

Similar to ChainedDict, but merged the keys and is actually derivative of dict.

a={1:11,2:22}b={3:33,4:44}c={1:55,4:66}d=MergedChainedDict(c,b,a)print(d)

Will output:

{1:55,2:22,3:33,4:66}

Mediator

Class interface to generator allowing query of has_next()

示例

fromacrisimportMediatordefyrange(n):i=0whilei<n:yieldii+=1n=10m=Mediator(yrange(n))foriinrange(n):print(i,m.has_next(3),next(m))print(i,m.has_next(),next(m))

示例输出

0True01True12True23True34True45True56True67True78False89False9Traceback(mostrecentcalllast):File"/private/var/acrisel/sand/acris/acris/acris/example/mediator.py",line19,in<module>print(i,m.has_next(),next(m))File"/private/var/acrisel/sand/acris/acris/acris/acris/mediator.py",line38,in__next__value=next(self.generator)StopIteration

setup tools

在标准python环境中使用的方法

Change History

1.0版
  1. Initial publication to open source

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

推荐PyPI第三方库


热门话题
java为什么javaassist仅在项目中使用lambda时加载Entitymanager时抛出无效的常量类型:18   java如何识别用户是否在Alexa中首次启动技能?   java maven:如何防止插件更新   java StringBuilder将null追加为“null”   在java中,我可以在画布上绘制画布吗?   java如何在JRadioButton上垂直对齐文本和图像?   java“类是对象的集合”。这个定义是对的还是错的?   java如何用其他字符替换字符串中的1个或多个字符?   Java的HashSet<Double>及其子集的hashcode的唯一性   对象ArrayList的java并发修改错误   多线程Java线程:让EDT函数等待长时间运行的函数离开EDT   java如何重写方法,将一个实例变量和一个局部变量相加,从而生成一个数据类型为Double的新变量?