python装饰器练习
1、编写装饰器,为多个函数加上认证功能(用户的账户密码来源文件)
要求登录成功一次,后续的函数都无需再输入用户名和密码
flag = False def login(func): def inner(*args,**kwargs): global flag """登录程序""" if flag: ret = func(*args,**kwargs) return ret else: username = input('you name: ') password = input('you password:') if username == 'dj' and password == '123': flag = True ret = func(*args, **kwargs) return ret else: print('logining error') return inner @login # shopListAdd = login(shopListAdd) def shopListAdd(): print('增加物品') @login def shopListDel(): print('删除物品') shopListAdd() shopListDel()
2、编写装饰器,为多个函数加上记录调用功能,要求每次调用函数都将被调用的函数名称写入文件
def log(func): def inner(*args,**kwargs): with open('log',mode='a',encoding='utf-8') as f: f.write(func.__name__+'\n') ret = func(*args,**kwargs) return ret return inner @log # shopListAdd = log(shopListAdd) def shopListAdd(): print('增加一件物品') @log #shopListDel = log(shopListDel) def shopListDel(): print('删除一件物品') shopListAdd() shopListDel()
1.编写下载网页内容的函数,要求功能是:用户传入一个url,函数返回下载页面的结果
2.为题目1编写装饰器,实现缓存网页内容的功能:
具体:实现下载的页面存放于文件中,如果文件内有值(文件大小不为0),就优先从文件中读取网页内容,否则,就去下载,然后存到文件中
import os from urllib.request import urlopen def cache(func): def inner(*args,**kwargs): if os.path.getsize('web_cache'): with open('web_cache','rb') as f: return f.read() ret = func(*args,**kwargs) #get() with open('web_cache','wb') as f: f.write(b'*********'+ret) return ret return inner @cache def get(url): code = urlopen(url).read return code ret = get('http://www.baidu.com') print(ret)