1. 记录程序运行次数
import logging
counte= 0
def count(f):
def wrapper(*args,**kwargs):
global counte
counte += 1
result = f(*args,**kwargs)
logging.warning("%s processed %s times!"%(f.__name__,counte))
return result
return wrapper
@count
def hello(s):
print(s)
hello("well")
hello("Hello!")
hello("Word!")
2. 记录程序运行时间
import time,logging
def timmer(func):
def wrapper(*args,**kwargs):
print('programe %s strat running...'%func.__qualname__)
start = time.time()
try:
result = func(*args,**kwargs)
end = time.time()
runtime = end - start
print('running %s s'%runtime)
return result
except:logging.error('programe running err!')
finally: print('programe end running!\n')
return wrapper