Package: acrilib
Table of ContentsOverviewProgramming IdomsthreadedSingleton and NamedSingletonSequenceTimedSizedRotatingHandlerDecoratorsData TypesMediatorsetup toolsChange HistoryOverviewacrilibis a python library providing useful programming patterns and tools.acrilibstarted 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:programming idioms that are repeatedly used by programmers.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 atsupport@acrisel.comProgramming Idomsthreadeddecorator 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.examplefromacrisimportthreaded,RetriveAsycValuefromtimeimportsleepclassThreadedExample(object):@threadeddefproc(self,id_,num,stall):s=numwhilenum>0:print("%s:%s"%(id_,s))num-=1s+=stallsleep(stall)print("%s:%s"%(id_,s))returnsexample outputprint("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('%scallback 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('%scallback result:%s'%(te2_callback.name,te2_callback.result))will produce:startingworkersTE1:3TE2:3collectingresultsjoiningt1TE1:4TE2:4TE1:5TE2:5TE1:6TE2:6joinedt1te1callbackresult:6te1syncResult:6te2syncResult:6te2callbackresult:6Singleton and NamedSingletonmeta class that creates singleton footprint of classes inheriting from it.Singleton examplefromacrisimportSingletonclassSequence(Singleton):step_id=0def__call__(self):step_id=self.step_idself.step_id+=1returnstep_idexample outputA=Sequence()print('A',A())print('A',A())B=Sequence()print('B',B())will produce:A0A1B2NamedSingleton examplefromacrisimportSingletonclassSequence(NamedSingleton):step_id=0def__init__(self,name=''):self.name=namedef__call__(self,):step_id=self.step_idself.step_id+=1returnstep_idexample outputA=Sequence('A')print(A.name,A())print(A.name,A())B=Sequence('B')print(B.name,B())will produce:A0A1B0Sequencemeta class to produce sequences.  Sequence allows creating different sequences using name tags.examplefromacrisimportSequenceA=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())example outputA0A1B0A2A3B1TimedSizedRotatingHandlerTBDDecoratorsUseful decorators for production and debug.traced_methodlogs 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)]256Data Typesvaries derivative of Python data typesMergeChainedDictSimilar 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}MediatorClass interface to generator allowing query of has_next()ExamplefromacrisimportMediatordefyrange(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))Example Output0True01True12True23True34True45True56True67True78False89False9Traceback(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)StopIterationsetup toolsMethods to use in standard python environmentChange HistoryVersion 1.0Initial publication to open source