spock
6/10/2017 - 8:01 PM

Basic decorator function

Basic decorator function

from functools import wraps

def my_decorator(f):
    @wraps(f)
    def wrapper(*args, **kwds):
        print('Calling decorated function')
        return f(*args, **kwds)
    return wrapper
def decorator(func):
  """ use via decorator: @timeit """
  def func_wrapper(*args, **kw):
    ts = time()  # things to do before
    result = func(*args, **kw)
    te = time()   # things to do after
    print 'func:%r args:[%r, %r] took: %2.4f sec' % \
    (f.__name__, args, kw, te-ts)
    return result
  return func_wrapper