Advertisement
Guest User

Untitled

a guest
Dec 9th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.83 KB | None | 0 0
  1. import numpy as np
  2. import collections as cl
  3. import time
  4.  
  5. #decorator
  6. def accepts(*decargs,**deckwargs):
  7.     def acceptdecorator(func):
  8.         typeName = lambda x: str(x)[str(x).find("'")+1:str(x).rfind("'")]
  9.         def wrappedfunc(*args,**kwargs):
  10.             match,typesreqstr,typesgivenstr = True,"",""
  11.             for decarg,arg in zip(decargs,args):
  12.                 typesreqstr += typeName(decarg)+", "
  13.                 typesgivenstr += typeName(type(arg))+", "
  14.                 if decarg!=type(arg):
  15.                     match=False
  16.             for key in kwargs:
  17.                 if key in deckwargs:
  18.                     typesreqstr += ("%s = %s, ")%(key,typeName(deckwargs[key]))
  19.                     typesgivenstr += ("%s = %s, ")%(key,typeName(type(kwargs[key])))
  20.                     if deckwargs[key]!=type(kwargs[key]): match=False
  21.             typesreqstr = typesreqstr[:-2]
  22.             typesgivenstr = typesgivenstr[:-2]
  23.             if not match:
  24.                 message = ("\nArgument types were:\n\t%s\nRequired types were:\n\t%s\n")%(typesgivenstr,typesreqstr)
  25.                 raise Exception(message)
  26.             return func(*args,**kwargs)
  27.         return wrappedfunc
  28.     return acceptdecorator
  29.  
  30. #decorator
  31. def returns(*dargs):
  32.     def returnsdecorator(func):
  33.         def typeName(x):
  34.             x = str(x)
  35.             if x == "None": return x
  36.             return x[x.find("'")+1:x.rfind("'")]
  37.         def wrappedfunc(*args,**kwargs):
  38.             outs = func(*args,**kwargs)
  39.             swap = False
  40.             try: len(outs)
  41.             except Exception as e: outs,swap = [outs],True
  42.             match,typesreqstr,typesgivenstr = True,"",""
  43.             if len(outs) != len(dargs): match = False
  44.             if match: match = all([type(out)==darg for out,darg in zip(outs,dargs)])
  45.             for out in outs: typesgivenstr += typeName(type(out))+", "
  46.             for darg in dargs: typesreqstr += typeName(darg)+", "
  47.             typesreqstr = typesreqstr[:-2]
  48.             typesgivenstr = typesgivenstr[:-2]
  49.             if not match:
  50.                 message = (
  51.                 "\nReturned types were:\n\t%s\nRequired types were:\n\t%s\n")%(typesgivenstr,typesreqstr)
  52.                 raise Exception(message)
  53.             return outs if not swap else outs[0]
  54.         return wrappedfunc
  55.     return returnsdecorator
  56.  
  57.  
  58. #decorator
  59. def timeinfo(func):
  60.     def wrappedfunc(*args,**kwargs):
  61.         print "\nfunc: ", func.__name__
  62.         if len(args):
  63.             print "args:"
  64.             print "\n\t".join([str(arg) for  arg in args])
  65.         if len(kwargs):
  66.             print "kwargs:"
  67.             print "\n\t".join( [ ("%s: %s")%(key,val) for key,val in zip(kwargs.keys(),kwargs.values())])
  68.         start = time.time()
  69.         out = func(*args, **kwargs)
  70.         print "time: ",time.time()-start," s\n"
  71.         return out
  72.     return wrappedfunc
  73.  
  74.  
  75. #decorator
  76. def memoize(func):
  77.     cache = dict()
  78.     def wrapped(key = object()):
  79.         if key not in cache:
  80.             cache.update({key:func(key)})
  81.         return cache[key]
  82.     return wrapped
  83.  
  84. #decorator
  85. def statistics(*statfuncs):
  86.     def statdecorator(func):
  87.         results = cl.deque([])
  88.         def wrappedfunc(*args, **kwargs):
  89.             output = func(*args,**kwargs)
  90.             results.append(output)
  91.             return output,[statfunc(results) for statfunc in statfuncs]
  92.         return wrappedfunc
  93.     return statdecorator
  94.  
  95. #decorator
  96. def filehandling(func):
  97.     import os
  98.     def wrappedfunc(*args,**kwargs):
  99.         if "filename" in kwargs.keys():
  100.             filename = kwargs["filename"]
  101.             if not os.path.exists(filename):
  102.                 file(filename,"w").close()
  103.         return func(*args,**kwargs)
  104.     return wrappedfunc
  105.  
  106. #decorator
  107. def decryptinput(decryptfunc):
  108.     def decryptdecorator(func):
  109.         def wrappedfunc(inputval):
  110.             return func(decryptfunc(inputval))
  111.         return wrappedfunc
  112.     return decryptdecorator
  113.  
  114. #decorator
  115. def encryptoutput(encryptfunc):
  116.     def encryptdecorator(func):
  117.         def wrappedfunc(*args,**kwargs):
  118.             funcout = func(*args,**kwargs)
  119.             return encryptfunc(funcout)
  120.         return wrappedfunc
  121.     return encryptdecorator
  122.  
  123. #decorator
  124. def outtofile(filename = "/tmp/pylog"):
  125.     def outdecorator(func):
  126.         def wrappedfunc(*args,**kwargs):
  127.             output = func(*args,**kwargs)
  128.             handle = file(filename,"a")
  129.             handle.write(("%s\t\t%s\n")%(func.__name__,str(output)))
  130.             handle.close()
  131.             return output
  132.         return wrappedfunc
  133.     return outdecorator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement