def demo():
print('demo执行')
demo()
@dazzling(n=10) # 输出10个=
def demo():
print('demo执行')
demo()
@auto_retry(times=3) # 最多重试3次, 函数会执行4次
def demo():
raise Exception('自定义错误')
demo()
@record_time(keep=4) # 保留4位小数
def demo():
print('哈哈哈')
time.sleep(1)
demo()
# 耀眼显示, 默认输出100个=
def dazzling(n=100):
def outer(func):
def inner(*args, **kwargs):
print('\n')
print('=' * n)
result = func(*args, **kwargs)
print('=' * n)
print('\n')
return result
return inner
return outer
# 自动重试, 默认重试2次
def auto_retry(times=2):
def outer(func):
def inner(*args, **kwargs):
for i in range(times + 1):
try:
return func(*args, **kwargs)
except Exception as e:
print('ERROR {}执行错误 {}'.format(func.__name__, e))
return inner
return outer
# 记录消耗的时间, 默认保留小数点后两位
def record_time(keep=2):
def outer(func):
def inner(*args, **kwargs):
t1 = time.time()
result = func(*args, **kwargs)
print(f'耗时{time.time() - t1:.{keep}f}秒')
return result
return inner
return outer