import datetime,time
defloog(fn):defwrapper(*args,**kwargs):print("{} program start".format(fn.__name__))
start = datetime.datetime.now()
cc = fn(*args,**kwargs)
delta =(datetime.datetime.now()-start).total_seconds()print("{} program end it took {} seconds".format(fn.__name__,delta))return wrapper
@loog # add = loog(add)defadd(x, y):"""I am a function ..."""
time.sleep(1)return x + y
add(5,8)print("function name is {},function doc is {}".format(add.__name__,add.__doc__))
调用函数进行属性覆盖
import datetime,time,functools
defcopysrc(src):def_copy(dest):
dest.__name__ = src.__name__
dest.__doc__ = src.__doc__
return dest
return _copy
defloog(fn):defwrapper(*args,**kwargs):print("{} program start".format(fn.__name__))
start = datetime.datetime.now()
cc = fn(*args,**kwargs)
delta =(datetime.datetime.now()-start).total_seconds()print("{} program end it took {} seconds".format(fn.__name__,delta))
copysrc(fn)(wrapper)#调用函数copysrc进行属性的覆盖return wrapper
@loog # add = loog(add)defadd(x, y):"""I am a function ..."""
time.sleep(1)return x + y
add(5,8)print("function name is {},function doc is {}".format(add.__name__,add.__doc__))
装饰器装饰装饰器
import datetime,time,functools
defcopysrc(src):def_copy(dest):
dest.__name__ = src.__name__
dest.__doc__ = src.__doc__
return dest
return _copy
defloog(fn):
@copysrc(fn)# wrapper = copysrc(fn)(wrapper)defwrapper(*args,**kwargs):print("{} program start".format(fn.__name__))
start = datetime.datetime.now()
cc = fn(*args,**kwargs)
delta =(datetime.datetime.now()-start).total_seconds()print("{} program end it took {} seconds".format(fn.__name__,delta))return wrapper
@loog # add = loog(add)defadd(x, y):"""I am a function ..."""
time.sleep(1)return x + y
add(5,8)print("function name is {},function doc is {}".format(add.__name__,add.__doc__))
用functools 模块进行装饰
import datetime,time,functools
# def copysrc(src):# def _copy(dest):# dest.__name__ = src.__name__# dest.__doc__ = src.__doc__# return dest# return _copydefloog(fn):
@functools.wraps(fn)# 等效于 wrapper = copysrc(fn)(wrapper) # 调用wraps函数进行装饰defwrapper(*args,**kwargs):"""My name is wrapper"""print("{} program start".format(fn.__name__))
start = datetime.datetime.now()
cc = fn(*args,**kwargs)
delta =(datetime.datetime.now()-start).total_seconds()print("{} program end it took {} seconds".format(fn.__name__,delta))return cc
return wrapper
@loog # add = loog(add)defadd(x, y):"""I am a function ..."""
time.sleep(1)return x + y
add(5,8)print("function name is {},function doc is {}".format(add.__name__, add.__doc__))
用functools 模块进行装饰
import datetime,time,functools
# def copysrc(src):# def _copy(dest):# dest.__name__ = src.__name__# dest.__doc__ = src.__doc__# return dest# return _copydeftest(flag):defloog(fn):
@functools.wraps(fn)# 等效于 wrapper = copysrc(fn)(wrapper)defwrapper(*args,**kwargs):"""My name is wrapper"""print("{} program start".format(fn.__name__))print("args = {},kwargs = {}".format(args,kwargs))
start = datetime.datetime.now()
cc = fn(*args,**kwargs)
delta =(datetime.datetime.now()-start).total_seconds()print("{} program end it took {} seconds".format(fn.__name__,delta))print("{} too slow".format(fn.__name__)if delta > flag else"{} OK ,is fast".format(fn.__name__))return cc
return wrapper
return loog
@test(2)# add = loog(add)defadd(x, y,z=2):"""I am a function ..."""
time.sleep(1)return x + y
add(5,8,z=15)print("function name is {},function doc is {}".format(add.__name__,add.__doc__))
将功能提取,用做通用函数,方便其他函数调用
import datetime, time, functools
# def copysrc(src):# def _copy(dest):# dest.__name__ = src.__name__# dest.__doc__ = src.__doc__# return dest# return _copydeffla(de,f,fla):#将下面一条语句做成函数提取出来,用做通用函数return"{} too slow".format(f.__name__)if de > fla else"{} OK ,is fast".format(f.__name__)deftest(flag):defloog(fn):
@functools.wraps(fn)# 等效于 wrapper = copysrc(fn)(wrapper)defwrapper(*args,**kwargs):"""My name is wrapper"""print("{} program start".format(fn.__name__))print("args = {},kwargs = {}".format(args,kwargs))
start = datetime.datetime.now()
cc = fn(*args,**kwargs)print(cc)
delta =(datetime.datetime.now()-start).total_seconds()print("{} program end it took {} seconds".format(fn.__name__, delta))
fla(delta, fn, flag)return cc
return wrapper
return loog
@test(2)# add = loog(add)defadd(x, y, z=2):"""I am a function ..."""
time.sleep(1)return x + y + z
add(5,8, z=15)print("function name is {},function doc is {}".format(add.__name__, add.__doc__))