一:编写函数,(函数执行的时间用time.sleep(n)模拟)
import time def timmer(func): start=time.time() func() end=time.time() print("程序执行时间{}".format(end-start))
二:编写装饰器,为函数加上统计时间的功能
import time def timmer(func): def wrapper(*args,**kwargs): start=time.time() res=func(*args,**kwargs) end=time.time() print("程序执行时间{}".format(end-start)) return res return wrapper
三:编写装饰器,为函数加上认证的功能
file.txt内容:
egon:123:1230000 alex:123:23200000 lxx:123:2424
//代码
def auth(func): def wrapper(*args, **kwargs): inp_name = input("请输入用户名:>>>").strip() inp_pwd = input("请输入密码:>>>").strip() with open("file.txt", "r", encoding="utf-8") as f: for line in f: name, pwd, *_ = line.strip(" ").split(":") if inp_name == name and inp_pwd == pwd: print("用户{}登录成功".format(inp_name)) func(*args, **kwargs) break else: print("用户名或密码错误,登录失败") return wrapper @auth def index(name): print("welecome to {} index page".format(name)) index("egon")
四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码
注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式
file.txt内容:
{"name":"egon","password":"123"}
{"name":"alex","password":"123"}
//代码
import time login_user=None def timmer(func): def wrapper(*args, **kwargs): start = time.time() res = func(*args, **kwargs) end = time.time() print("程序执行时间{}".format(end - start)) return res return wrapper def auth(func): def wrapper(*args, **kwargs): global login_user if login_user: func(*args, **kwargs) else: inp_name = input("请输入用户名:>>>").strip() inp_pwd = input("请输入密码:>>>").strip() with open("file.txt", "r", encoding="utf-8") as f: for line in f: line=eval(line.strip()) if inp_name == line["name"] and inp_pwd == line["password"]: print("用户{}登录成功".format(inp_name)) login_user=inp_name func(*args, **kwargs) break else: print("用户名或密码错误,登录失败") return wrapper @auth def index(name): print("welecome to {} index page".format(name)) @auth def home(name): print("welecome to {} home page".format(name)) index("egon") home("alex")
五:编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录
file.txt内容:
{"name":"egon","password":"123"}
{"name":"alex","password":"123"}
//代码
import time login_user={'user':None,'login_time':None,'timeout':3,} def timmer(func): def wrapper(*args, **kwargs): start = time.time() res = func(*args, **kwargs) end = time.time() print("程序执行时间{}".format(end - start)) return res return wrapper def auth(func): def wrapper(*args, **kwargs): global login_user if login_user["user"]: time_out=time.time()-login_user["login_time"] if time_out < login_user["timeout"]: return func(*args, **kwargs) inp_name = input("请输入用户名:>>>").strip() inp_pwd = input("请输入密码:>>>").strip() with open("file.txt", "r", encoding="utf-8") as f: for line in f: line=eval(line.strip()) if inp_name == line["name"] and inp_pwd == line["password"]: login_user["user"]=inp_name login_user["login_time"]=time.time() print("用户{}登录成功".format(inp_name)) return func(*args, **kwargs) else: print("用户名或密码错误,登录失败") return wrapper @auth def index(name): time.sleep(3) print("welecome to {} index page".format(name)) @auth def home(name): time.sleep(3) print("welecome to {} home page".format(name)) index("egon") home("alex")
六:选做题
# 思考题(选做),叠加多个装饰器,加载顺序与运行顺序,可以将上述实现的装饰器叠加起来自己验证一下
# @deco1 # index=deco1(deco2.wrapper的内存地址)
# @deco2 # deco2.wrapper的内存地址=deco2(deco3.wrapper的内存地址)
# @deco3 # deco3.wrapper的内存地址=deco3(index)
# def index():
# pass
file.txt内容:
{"name":"egon","password":"123"}
{"name":"alex","password":"123"}
代码:
import time login_user = {'user': None, 'login_time': None, 'timeout': 3, } def timmer(func): def wrapper(*args, **kwargs): print("开始计算执行时间") start = time.time() res = func(*args, **kwargs) end = time.time() print("程序执行时间{}".format(end - start)) return res return wrapper def auth(func): def wrapper(*args, **kwargs): global login_user print("开始执行认证") if login_user["user"]: time_out = time.time() - login_user["login_time"] if time_out < login_user["timeout"]: res = func(*args, **kwargs) print("认证结束") return res inp_name = input("请输入用户名:>>>").strip() inp_pwd = input("请输入密码:>>>").strip() with open("file.txt", "r", encoding="utf-8") as f: for line in f: line = eval(line.strip()) if inp_name == line["name"] and inp_pwd == line["password"]: login_user["user"] = inp_name login_user["login_time"] = time.time() print("用户{}登录成功".format(inp_name)) res=func(*args, **kwargs) print("认证结束") return res else: print("用户名或密码错误,登录失败") return wrapper @auth @timmer def index(name): time.sleep(3) print("welecome to {} index page".format(name)) index("alex") #执行结果 开始执行认证 #auth装饰器开始运行 请输入用户名:>>>egon 请输入密码:>>>123 用户egon登录成功 开始计算执行时间 #timmer装饰器开始运行 welecome to alex index page #被装饰函数体代码运行 程序执行时间3.0004971027374268 #timmer装饰器结束运行 认证结束 #auth装饰器结束运行