# 一:编写函数,(函数执行的时间是随机的) import time import random # def foo(): # time.sleep(random.randrange(1,5)) # print('haha') # foo() # 二:编写装饰器,为函数加上统计时间的功能 # def timmer(func): # def wrapper(): # start_time=time.time() # func() # stop_time=time.time() # print('run time is %s'%(stop_time-start_time)) # return wrapper # @timmer # def foo(): # time.sleep(random.randrange(1,5)) # print('haha') # foo() # 三:编写装饰器,为函数加上认证的功能 # def auth(func): # def deco(): # name=input('name: ') # pwd=input('password: ') # if name=='egon' and pwd=='123': # print('login successful') # func() # else: # print('login erro') # return deco # @auth # def foo(): # time.sleep(random.randrange(1,5)) # print('haha') # foo() # 四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码 # 注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式 # user_dic={ # 'egon':'123', # 'alex':'alex3714', # 'wupeiqi':'wu13', # 'yuanhao':'123123' # } # with open('db.txt','w',encoding='utf-8') as f: # f.write(str(user_dic)) # with open('db.txt','r',encoding='utf-8') as f: # res=f.read() # #print(res,type(res))#字符串类型 # user_dic=eval(res) # #print(user_dic,type(user_dic))#字典类型 # db_path='db.txt' # login_dic={ # 'user':None, # 'status':False, # } # def auth(func): # def wrapper(*args,**kwargs): # if login_dic['user'] and login_dic['status']: # res = func(*args, **kwargs) # return res # name=input('your name: ') # password=input('your password: ') # with open(db_path,'r',encoding='utf-8') as f: # user_dic=eval(f.read()) # if name in user_dic and password == user_dic[name]: # print('login ok') # login_dic['user']=name # login_dic['status']=True # res=func(*args,**kwargs) # return res # else: # print('login err') # return wrapper # @auth #auth(index) # def index(): # print('welecome to index') # @auth # def home(name): # print('welecome %s to home page' %name) # index() # home('egon') # 五:编写下载网页内容的函数,要求功能是:用户传入一个url,函数返回下载页面的结果 # from urllib.request import urlopen # def index(url): # def get(): # return urlopen(url).read() # return get # python = index('https://www.python.org') # print(python()) # 六:为题目五编写装饰器,实现缓存网页内容的功能: # 具体:实现下载的页面存放于文件中,如果文件内有值(文件大小不为0), # 就优先从文件中读取网页内容,否则,就去下载,然后存到文件中 # from urllib.request import urlopen # import os # cache_path=r'C:\Users\Administrator\PycharmProjects\python5期\day8\cache.txt' # def make_cache(func): # def wrapper(*args,**kwargs): # if os.path.getsize(cache_path): # #有缓存,文件不为空 # print('\033[45m=========>有缓存\033[0m') # with open(cache_path,'rb') as f: # res=f.read() # else: # res=func(*args,**kwargs) #下载 # with open(cache_path,'wb') as f: #制作缓存 # f.write(res) # return res # return wrapper # @make_cache # def get(url): # return urlopen(url).read() # print('================>first') # print(get('https://www.python.org')) # print('================>second') # print(get('https://www.python.org')) # print('================>third') # print(get('https://www.python.org')) # 七:还记得我们用函数对象的概念,制作一个函数字典的操作吗, # 来来来,我们有更高大上的做法,在文件开头声明一个空字典, # 然后在每个函数前加上装饰器,完成自动添加到字典的操作 # func_dic = {} # def deco(key): # def deco2(func): # func_dic[key]=func # return deco2 # @deco('f1')#@deco2 func1=deco2(func1) # def func1(): # print("from f1") # @deco('f2') # def func2(): # print("from f2") # @deco('f3') # def func3(): # print("from f3") # print(func_dic) # while True: # cmd=input('>>: ').strip() # if cmd in func_dic: # func_dic[cmd]()