python decorator
4114 ワード
非常に良い入門文:
http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html
http://coolshell.cn/articles/11265.html
http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html
http://coolshell.cn/articles/11265.html
#!/usr/bin/python
import time
import functools
def timeit_0(func):
start = time.clock()
func()
end =time.clock()
print 'used:', end - start
def timeit_1(func):
def wrapper():
start = time.clock()
func()
end =time.clock()
print 'used:', end - start
return wrapper
def foo():
for i in range(0, 100):
print 'in foo()',i
@timeit_1
def foo_1():
for i in range(0, 100):
print 'in foo()',i
"step one--------------------------------------------"
print "timeit_0","#"*10
timeit_0(foo)
"step two--------------------------------------------"
print "timeit_1","#"*10
bar=timeit_1(foo)
bar()
"step three-------------------------------------------"
print "foo 1 with @","#"*10
foo_1()
def timeit(func):
def wrapper(*args, **kvargs):
start = time.clock()
func(*args, **kvargs)
end =time.clock()
print 'used:', end - start
return wrapper
@timeit
def foo(a,b,c,d):
for i in range(0, a):
print 'in foo()',i
for i in range(0, b):
print 'in foo()',i
for i in range(0, c):
print 'in foo()',i
"step four--------------------------------------------"
print "with *args **kvargs"
foo(10,9,8,7)
print foo.__name__
print """------------------nomally decorate----------------------"""
def dec_0(fun):
def hello():
if fun.__doc__==None:
print fun.__name__,"do not has doc"
else:
print fun.__name__,fun.__doc__
return hello
@dec_0
def hello_world_0():
'hello world'
print "hello"
hello_world_0()
print "#"*5
print hello_world_0.__name__
print "#"*5
print """------------------with arg decorate----------------------"""
def dec_1(sql):
print sql
def test(fun):
def hello():
if fun.__doc__==None:
print fun.__name__,"do not has doc"
else:
print fun.__name__,fun.__doc__
return hello
return test
@dec_1(sql="test")
def hello_world_1():
'hello world'
print "hello"
hello_world_1()
print "#"*5
print hello_world_1.__name__
print "#"*5
print """--------------------with wraps -----------------"""
from functools import wraps
def dec_2(fun):
@wraps(fun)
def hello():
if fun.__doc__==None:
print fun.__name__,"do not has doc"
else:
print fun.__name__,fun.__doc__
return hello
@dec_2
def hello_world_2():
'hello world'
print "hello"
hello_world_2()
print "#"*5
print hello_world_2.__name__
print "#"*5
print """------------------with class-------------------"""
class myDecorator(object):
def __init__(self, fn):
print "inside myDecorator.__init__()"
self.fn = fn
def __call__(self):
self.fn()
print "inside myDecorator.__call__()"
@myDecorator
def aFunction():
print "inside aFunction()"
aFunction()
import time
def logger(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
ts = time.time()
result = fn(*args, **kwargs)
te = time.time()
print "function = {0}".format(fn.__name__)
print " arguments = {0} {1}".format(args, kwargs)
print " return = {0}".format(result)
print " time = %.6f sec" % (te-ts)
return result
return wrapper
@logger
def multipy(x, y):
return x * y
@logger
def sum_num(n):
s = 0
for i in xrange(n+1):
s += i
return s
print multipy(2, 10)
print sum_num(100)
print sum_num(10000000)