必做题:
1.编写函数,(函数执行的时间用time.sleep(n)模拟)
import time
def count_time():
start = time.time()
time.sleep(2)
print('我是输出的内容!我要花费零点几毫秒的时间\n')
stop = time.time()
print('函数一共运行时间为:', stop - start)
count_time()
2.编写装饰器,为函数加上统计时间的功能
import time
def count_time(func):
def inner(*args, **kwargs):
start = time.time()
func(*args, **kwargs)
stop = time.time()
print('函数一共运行时间为:', stop - start)
return inner
@count_time
def foo():
time.sleep(2)
print('我是原封不动的原函数输出的内容!我要花费零点几毫秒的时间')
foo()
3.编写装饰器,为函数加上认证的功能
import time
def identify(func):
inp_usr = input('请输入用户名:').strip()
inp_pwd = input('请输入密码:').strip()
def dealwith():
if not (inp_usr or inp_pwd):
print("输入不能为空")
with open(r'userinfo.txt', mode='at', encoding='UTF-8') as w_register, \
open(r'userinfo.txt', mode='rt', encoding='UTF-8') as r_register:
res = r_register.read()
if inp_usr not in res:
w_register.write(f'{inp_usr}:{inp_pwd}\n')
print('注册成功!')
else:
print('该用户已存在,注册失败!')
return dealwith
@identify
def register(name):
time.sleep(1)
print('用户{}注册成功!'.format(name))
register()
4.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码
注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式
import time
login_user = {'name': None}
def identify(func):
def wrapper(*args, **kwargs):
if login_user['name']:
return func(*args, **kwargs)
inp_usr = input('请输入用户名:').strip()
inp_pwd = input('请输入密码:').strip()
with open(r'userinfo.txt', mode='rt', encoding='UTF-8') as f:
for line in f:
line = line.strip()
userinfo = eval(line)
if inp_usr == userinfo['name']:
if inp_pwd == userinfo['password']:
print('登录成功')
login_user['name'] = inp_usr
login_user['start_time'] = time.time()
return func(*args, **kwargs)
else:
print('密码错误!')
break
else:
print('该用户名不存在!')
return wrapper
@identify
def index1():
time.sleep(1)
print('欢迎用户{}来到主页!'.format(login_user['name']))
@identify
def index2():
time.sleep(1)
print('欢迎用户{}再一次来到主页!'.format(login_user['name']))
index1()
index2()
5.编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录
import time
login_user = {'name': None, 'start_time': 0}
def identify(func):
def wrapper(*args, **kwargs):
if login_user['name']:
spend_time = time.time() - login_user.get('start_time')
if spend_time < 5:
return func(*args, **kwargs)
else:
print("超时,请重新登录")
login_user['name'] = None
login_user['start_time'] = 0
inp_usr = input('请输入用户名:').strip()
inp_pwd = input('请输入密码:').strip()
with open(r'userinfo.txt', mode='rt', encoding='UTF-8') as f:
for line in f:
line = line.strip()
userinfo = eval(line)
if inp_usr == userinfo['name']:
if inp_pwd == userinfo['password']:
print('登录成功')
login_user['name'] = inp_usr
login_user['start_time'] = time.time()
return func(*args, **kwargs)
else:
print('密码错误!')
break
else:
print('该用户名不存在!')
return wrapper
@identify
def index1():
time.sleep(1)
print('欢迎用户{}来到主页!'.format(login_user['name']))
@identify
def index2():
time.sleep(1)
print('欢迎用户{}再一次来到主页!'.format(login_user['name']))
index1()
index2()
选做题:
思考题(选做),叠加多个装饰器,加载顺序与运行顺序,可以将上述实现的装饰器叠加起来自己验证一下
# @deco1 # index=deco1(deco2.wrapper的内存地址)
# @deco2 # deco2.wrapper的内存地址=deco2(deco3.wrapper的内存地址)
# @deco3 # deco3.wrapper的内存地址=deco3(index)
# def index():
# pass