闭包函数作业

# 一:编写函数,(函数执行的时间用time.sleep(n)模拟)
# def indea(x,y):
# time.sleep(2)
# 二:编写装饰器,为函数加上统计时间的功能

# 三:编写装饰器,为函数加上认证的功能
# import time
# tag=True
# def beg(func):
#
#     def warpper(*args,**kwargs):
#         global tag
#         start=time.time()
#         while tag:
#             account=input("请输入账号")
#             pwd=input("请输入密码")
#             with open("账号密码",mode="r",encoding="utf8")as f:
#                 for i in f:
#                     acount,password=i.strip().split(":")
#                     if account==acount and pwd==password:
#                         print("登录成功  认证成功")
#                         a=input("是否计算程序时间计算命令y:n")
#                         if a=="y":
#                             res=func(*args,**kwargs)
#                             stop=time.time()
#                             print('{}{}'.format("统计时间是",stop-start))
#                             tag=False
#                             return res
#                         elif a=="n":
#                             print("好的正在退出程序")
#                             break
#                     else:
#                         print("登录失败账号密码不纯在")
#             break
#     return warpper                      #记得装饰器 返回函数不需要括号
# @beg
# def indea():
#     time.sleep(2)
#     return "返回被装饰函数的值成功"
# indea()

 




#
# 四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码
# 注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式
# import time
# tag=True
# n=0
# def beg(func):
#     def warpper(*args,**kwargs):
#         global tag,n
#         start=time.time()
#         while tag:
#             if n<=0:
#                 account=input("请输入账号")
#                 pwd=input("请输入密码")
#                 with open("账号密码",mode="r",encoding="utf8")as f:
#                     for i in f:
#                         acount,password=i.strip().split(":")
#                         if account==acount and pwd==password:
#                             print("登录成功  认证成功")
#                             n=1
#                             break
#                         else:
#                             print("登录失败账号密码不纯在")
#             else:
#                 a=input("是否计算程序时间计算命令y:n")
#                 if a=="y":
#                     res=func(*args,**kwargs)
#                     stop=time.time()
#                     print('{}{}'.format("统计时间是",stop-start))
#
#                     continue
#
#                 elif a=="n":
#                     print("好的正在退出程序")
#                     tag = False
#
#     return warpper                      #记得装饰器 返回函数不需要括号
# @beg
# def indea():
#     time.sleep(2)
#     return "返回被装饰函数的值成功"
# indea()
# 五:编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录
# 六:选做题
# # 思考题(选做),叠加多个装饰器,加载顺序与运行顺序,可以将上述实现的装饰器叠加起来自己验证一下
# # @deco1 # index=deco1(deco2.wrapper的内存地址)
# # @deco2 # deco2.wrapper的内存地址=deco2(deco3.wrapper的内存地址)
# # @deco3 # deco3.wrapper的内存地址=deco3(index)
# # def index():
# # pass
posted @ 2021-03-15 19:52  欧阳锦涛  阅读(46)  评论(0)    收藏  举报
TOP 底部