python装饰器作业

# user_dic={'xiaohong':'123','xiaobai':'123','xiaohei':'123'}
# with open('idpd.txt','w',encoding='utf-8') as f:
#     f.write(str(user_dic))
# with open('idpd.txt','r',encoding='utf-8') as f2:
#     read=f2.read()
#     print(read)
#     user_dic=eval(read)
#将user_dic字典写入文件,以及将文件字典导入
# idpd_path=r'F:Pycharmprojects615idpd.txt'
# idpd_dic={'id':False,'password':False}
# def Login(func):
#     def login(*args,**kwargs):
#         if idpd_dic['id'] and idpd_dic['password']:
#             res=func(*args,**kwargs)
#             return res
#         with open(idpd_path,'r',encoding='utf-8') as f:
#             login_dic=eval(f.read())
制作缓存
# from urllib.request import urlopen
# import os
# cashe_path=r'F:Pycharmprojects615makecache.txt'
# def make_cashe(func):
#     def wrapper(*args,**kwargs):
#         if os.path.getsize(cashe_path):
#             print('33[45m====>有缓存33[0m')
#             with open(cashe_path, 'rb', ) as f:
#                 res=f.read()
#         else:
#             res=func(*args,**kwargs)
#             with open(cashe_path,'wb') as f:A
#                 f.write(res)
#         return res
#     return wrapper
# @make_cashe
# def get(url):
#     return urlopen(url).read()



#在文件开头声明一个空字典,然后在每个函数前加上装饰器,完成自动添加到字典的操作
# fun_dic={}
# def Outer(key):
#     def outer(func):
#         def wrapper(*args,**kwargs):
#             fun_dic[key]=func
#         return wrapper
#     return outer
# 
# @Outer('f1')
# def f1():
#     print('from f1')
# @Outer('f2')
# def f2():
#     print('from f2')
# @Outer('f3')
# def f3():
#     print('fromt f3')
# f1()
# f2()
# f3()
# print(fun_dic)



#             i=input('id:')
#             password=input('password:')
#         if i in login_dic and password in login_dic:
#             print('login successfully')
#             idpd_dic['id']=True
#             idpd_dic['password']=True
#             res=func(*args,**kwargs)
#             return res
#         else:
#             print('login error')
#     return login
#认证成功后,之后多次运行其他不再需要认证
#在文件开头声明一个空字典,然后在每个函数前加上装饰器,完成自动添加到字典的操作
# fun_dic={}
# def Outer(key):
#     def outer(func):
#         def wrapper(*args,**kwargs):
#             fun_dic[key]=func
#         return wrapper
#     return outer
# 
# @Outer('f1')
# def f1():
#     print('from f1')
# @Outer('f2')
# def f2():
#     print('from f2')
# @Outer('f3')
# def f3():
#     print('fromt f3')
# f1()
# f2()
# f3()
# print(fun_dic)
原文地址:https://www.cnblogs.com/c491873412/p/7019688.html