import logging
from time import time as t
import time
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
is_debug = True
def count_time(is_debug):
def handle_func(func):
def handle_args(*args, **kwargs):
if is_debug:
begin = t()
func(*args, **kwargs)
print 'args2','kwargs2',args,kwargs
logging.debug( "[" + func.__name__ + "] -> " + str(t() - begin) )
else:
func(*args, **kwargs)
print 'handle_args', handle_args
print type(handle_args)
return handle_args
return handle_func
def pr():
for i in range(1,1000):
i = i * 2
time.sleep(0.01)
print "hello world",t()
def test():
pr()
print 'test'
@
count_time(is_debug)
def test2():
pr()
print 'test2'
@
count_time(False)
def test3():
pr()
print 'test3'
if __name__ == "__main__":
test()
test2()
test3()
结果
C:\Python27\python.exe D:/python/pyworkspcae/testdeco/
timedeco.pyhello world 1500518211.89
test
hello world 1500518221.92
DEBUG:root:[test2] -> 10.0350000858
test2
args2 kwargs2 () {}
hello world 1500518232.03
test3
handle_args <function handle_args at 0x00000000029C6518>
<type 'function'>
Process finished with exit code 0
DEBUG 输出这条信息顺序看不懂
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
https://tanronggui.xyz/t/376611
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.