In [5]:
import sys
def simple_def_decorator(F):
decorator_name = sys._getframe().f_code.co_name
function_name = F.__name__
print "{0} for {1}".format(decorator_name, function_name)
return F
@simple_def_decorator
def func1():
pass
func1()
In [9]:
def simple_call_decorator(F):
decorator_name = sys._getframe().f_code.co_name
function_name = F.__name__
print "def time: {0} for {1}".format(decorator_name, function_name)
def wrapper():
msg = "call time: {0}"
print msg.format(F.__name__)
F()
return wrapper
@simple_call_decorator
def func2():
print "{0} called".format(sys._getframe().f_code.co_name)
func2()
In [16]:
def simple_invocation_callable_decorator():
decorator_name = sys._getframe().f_code.co_name
print "def time: {0}()".format(decorator_name)
def simple_call_decorator(F):
decorator_name = sys._getframe().f_code.co_name
function_name = F.__name__
print "def time: {0} for {1}".format(decorator_name, function_name)
def wrapper():
msg = "call time: {0}"
print msg.format(F.__name__)
F()
return wrapper
return simple_call_decorator
@simple_invocation_callable_decorator()
def func3():
print "{0} called".format(sys._getframe().f_code.co_name)
func3()
In [26]:
def parameterized_decorator(debug=False):
decorator_name = sys._getframe().f_code.co_name
print "def time: {0}()".format(decorator_name)
#print "debug={0}".format(debug)
def simple_call_decorator(F):
decorator_name = sys._getframe().f_code.co_name
function_name = F.__name__
print "def time: {0} for {1}".format(decorator_name, function_name)
#print "debug={0}".format(debug)
def wrapper():
msg = "call time: {0}"
print msg.format(F.__name__),
print "debug={0}".format(debug)
F()
return wrapper
return simple_call_decorator
@parameterized_decorator(debug=True)
def func3():
print "{0} called".format(sys._getframe().f_code.co_name)
func3()
In [28]:
def parameterized_args_passing_decorator(debug=False):
decorator_name = sys._getframe().f_code.co_name
print "def time: {0}()".format(decorator_name)
#print "debug={0}".format(debug)
def simple_call_decorator(F):
decorator_name = sys._getframe().f_code.co_name
function_name = F.__name__
print "def time: {0} for {1}".format(decorator_name, function_name)
#print "debug={0}".format(debug)
def wrapper(arg1):
msg = "call time: {0}"
print msg.format(F.__name__),
print "debug={0}".format(debug)
F(arg1)
return wrapper
return simple_call_decorator
@parameterized_args_passing_decorator(debug=True)
def func4(arg1):
print "{0} called with {1}".format(sys._getframe().f_code.co_name, arg1)
func4("zzz")
In [ ]: