# 一:编写函数,(函数执行的时间用time.sleep(n)模拟)
# import time
#
# def wrapper(f):
# def inner(*args, **kwargs):
# start_time = time.time()
# ret = f(*args, **kwargs)
# time.sleep(1)
# stop_time = time.time()
# print('运行时间为%s' % (stop_time - start_time))
# return ret
#
# return inner
# 二:编写装饰器,为函数加上统计时间的功能
# import time
#
#
# def wrapper(f):
# def inner(*args, **kwargs):
# start_time = time.time()
# ret = f(*args, **kwargs)
# stop_time = time.time()
# print('运行时间为%s' % (stop_time - start_time))
# return ret
#
# return inner
#
#
# l = []
#
#
# @wrapper
# def test():
# time.sleep(0.5)
# for i in range(100):
# l.append(i)
# print(l)
#
#
# test()
# 三:编写装饰器,为函数加上认证的功能
# def wrapper(f):
# def inner(*args,**kwargs):
# name = input('账号')
# pasd = input("密码")
# if name == 'chen' and pasd == 'chen':
# msg = f(*args,**kwargs)
# return msg
# else:
# print('error')
# return inner
# @wrapper
# def amt():
# print('可选界面')
# amt()
# 四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),
# 要求登录成功一次,后续的函数都无需再输入用户名和密码
# 注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式
# import time
# username = None
#
# def wrapper(func):
# def inner(*args,**kwargs):
# if username == None:
# login()
# func1()
# else:
# msg = func(*args, **kwargs)
# return msg
# return inner
#
# def login():
# global username
# tag = 1
# while tag <= 3:
# name = input('账号')
# pasd = input('密码')
# with open('db.txt', 'r', encoding='utf-8') as f:
# for line in f:
# if name in line:
# user = line.strip('
').split(':')
# if name == user[0] and pasd == user[1]:
# print('登录成功')
# username = 1
#
# tag = 4
# else:
# tag += 1
#
# @wrapper
# def func1():
# time.sleep(1)
# print('chen')
# @wrapper
# def func2():
# print('shao')
#
# func1()
# func2()
# 五:编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录
import time
username = 1
def wrapper(func):
def inner(*args, **kwargs):
read_time()
if username == None:
login()
else:
msg = func(*args, **kwargs)
return msg
return inner
tag = 1
def login():
global username,tag
while tag < 3:
name = input('账号').strip()
pasd = input('密码').strip()
with open('db.txt', 'r', encoding='utf-8') as f:
for line in f:
if name in line:
user = line.strip('
').split(':')
if name == user[0] and pasd == user[1]:
print('登录成功')
username = 1
tag = 4
doc()
else:
tag += 1
def doc():
ret = time.time()
ret = int(ret) + 100
with open('time.txt','w',errors='utf-8') as f:
f.write(str(ret))
def read_time():
global username
now_time = time.time()
now_time = int(now_time)
with open('time.txt','r',encoding='utf-8') as f:
war_time = f.read()
if now_time > int(war_time):
username = None
else:
username = 1
@wrapper
def fu():
pass
@wrapper
def func1():
time.sleep(1)
print('chen')
@wrapper
def func2():
print('shao')
fu()
func1()
func2()
"""
六:编写装饰器,为多个函数加上记录日志的功能:函数一旦运行则按照下述格式记录日志
函数开始执行的时间 函数名 返回值
2020-06-18 12:13:38 index 456
2020-06-18 12:13:39 home 123
提示:
统计时间time.strftime('%Y-%m-%d %H:%M:%S')
函数名func.__name__
"""
姗姗来迟
import time
def wrapper(func):
def inner(*args,**kwargs):
t = time.strftime('%Y-%m-%d %H:%M:%S')
ret = func(*args,**kwargs)
with open('log.txt','a',encoding='utf-8') as f:
f.write("%s %s %s
"%(t,func.__name__,ret))
return ret
return inner
@wrapper
def fun():
print('chen')
return 22
fun()