装饰器作业
作业1:编辑装饰器,为多个函数加上认证功能(用户的账号密码源于文件),要求登录成功一次,后续的函数都无需输入用户名和密码
1 li=[] 2 username=input('请输入您的用户名:') 3 password=input('请输入您的密码:') 4 with open('list_of_info',mode='w+',encoding='utf-8') as f: 5 f.write('{}\n{}'.format(username,password)) 6 with open('list_of_info',mode='r+',encoding='utf-8') as f1: 7 for line in f1: 8 li.append(line) 9 10 def shoping(func): 11 def inner(*args,**kwargs): 12 username_1=input('请输入您的登录用户名:').strip() 13 password_1=input('请输入您的登陆密码:').strip() 14 if username_1==li[0].strip() and password_1==li[1].strip():#这里一定要加上.strip,否则报错 15 print('登录成功') 16 ret = func(*args,**kwargs) 17 return ret 18 else: 19 print('登录失败,请重新登录') 20 # return ret 21 return inner 22 @shoping 23 def shopinglist_add(): 24 print('增加一件商品') 25 26 @shoping 27 def shopinglist_del(): 28 print('删除一件商品') 29 30 shopinglist_add() 31 shopinglist_del() 32 33 D:\anoconda\python.exe F:/python/python学习/人工智能/第一阶段day2/456.py 34 请输入您的用户名:KL 35 请输入您的密码:123 36 请输入您的登录用户名:KL 37 请输入您的登陆密码:123 38 ['KL\n', '123'] 39 KL 40 KL 41 KL 42 43 增加一件商品 44 请输入您的登录用户名:
上面的代码已经解决了第一个def shopinglist_add()的问题,但是到def shopinglist_del()这里还需要再次登录,感觉没有达到题目的要求。继续修改代码,
1 li=[] 2 username=input('请输入您的用户名:') 3 password=input('请输入您的密码:') 4 with open('list_of_info',mode='w+',encoding='utf-8') as f: 5 f.write('{}\n{}'.format(username,password)) 6 with open('list_of_info',mode='r+',encoding='utf-8') as f1: 7 for line in f1: 8 li.append(line) 9 10 FLAG=False#这个知识还没学到,以后会学习 11 def shoping(func): 12 def inner(*args,**kwargs): 13 global FLAG 14 if FLAG: 15 ret = func(*args, **kwargs) 16 return ret 17 else: 18 username_1=input('请输入您的登录用户名:').strip() 19 password_1=input('请输入您的登陆密码:').strip() 20 if username_1==li[0].strip() and password_1==li[1].strip(): 21 FLAG=True 22 ret = func(*args, **kwargs) 23 return ret 24 print('登录成功') 25 else: 26 print('登录失败,请重新登录') 27 return inner 28 @shoping 29 def shopinglist_add(): 30 print('增加一件商品') 31 32 @shoping 33 def shopinglist_del(): 34 print('删除一件商品') 35 36 shopinglist_add() 37 shopinglist_del() 38 39 40 D:\anoconda\python.exe F:/python/python学习/人工智能/第一阶段day2/456.py 41 请输入您的用户名:KL 42 请输入您的密码:123 43 请输入您的登录用户名:KL 44 请输入您的登陆密码:123 45 增加一件商品 46 删除一件商品
2、编写装饰器,为多个函数加上记录调用功能,要求每次调用函数都将被调用函数的函数名名称写入文件
1 def log(func): 2 def inner(*args,**kwargs): 3 with open('log','a',encoding='utf-8')as f:#注意这里本来是需要用mode='a',但是mode可以省略 4 f.write(func.__name__+'\n')#获取函数的名字 5 ret=func(*args,**kwargs) 6 return ret 7 return inner 8 9 10 @log 11 def shopinglist_add(): 12 print('增加一件商品') 13 14 @log 15 def shopinglist_del(): 16 print('删除一件商品') 17 18 19 shopinglist_add() 20 shopinglist_del()
受当前知识所限,上面的代码目前看起来还不是很完美,当以后随着知识的增加,不仅仅可以记录函数名,还可以用户名,时间等,同时将这些东西存储进数据库。
3、编写下载网页内容的函数,要求功能是:用户传入一个url,函数返回下载页面的结果
1 import urllib.request#注意这里跟老师的讲解可能有出入,以这个为准 2 3 def get(url): 4 code=urllib.request.urlopen(url) 5 return code 6 7 ret=get('https://www.baidu.com') 8 print(ret) 9 10 11 D:\anoconda\python.exe F:/python/python学习/人工智能/第一阶段day2/14.py 12 <http.client.HTTPResponse object at 0x0000025003BF92E8> 13 14 Process finished with exit code 0
4、为上题编写装饰器,实现缓存网页内容的功能
具体:实现下载页面存放在文件中,如果文件内有值(文件大小不为0),就优先从文件中读取网页内容,否则就去下载,然后再读取
1 import os 2 import requests#注意这里跟老师的讲解可能有出入,以这个为准 3 4 def cache(func): 5 def inner(*args,**kwargs): 6 if os.path.getsize('web_cache'): 7 with open('web_cache','rb')as f: 8 return f.read() 9 ret=func(*args,**kwargs) 10 with open('web_cache','wb')as f: 11 f.write(ret) 12 return ret 13 return inner 14 15 @cache 16 def get(url): 17 code=requests.get(url) 18 return code 19 20 ret=get('https://www.baidu.com') 21 print(ret) 22 ret=get('https://www.baidu.com/') 23 print(ret) 24 ret=get('https://www.baidu.com/') 25 print(ret) 26 27 28 D:\anoconda\python.exe F:/python/python学习/人工智能/第一阶段day2/14.py 29 Traceback (most recent call last): 30 File "F:/python/python学习/人工智能/第一阶段day2/14.py", line 20, in <module> 31 ret=get('https://www.baidu.com') 32 File "F:/python/python学习/人工智能/第一阶段day2/14.py", line 11, in inner 33 f.write(ret) 34 TypeError: a bytes-like object is required, not 'Response' 35 36 Process finished with exit code 1
5.周末大作业:实现员工信息表
文件存储格式如下:
id,name,age,phone,job
1,Alex,22,13651054608,IT
2,Egon,23,13304320533,Tearcher
3,nezha,25,1333235322,IT
现在需要对这个员工信息文件进行增删改查。
不允许一次性将文件中的行都读入内存。
基础必做:
a.可以进行查询,支持三种语法:
select 列名1,列名2,… where 列名条件
支持:大于小于等于,还要支持模糊查找。
示例:
select name, age where age>22
select * where job=IT
select * where phone like 133
进阶选做:
b.可创建新员工记录,id要顺序增加
c.可删除指定员工记录,直接输入员工id即可
d.修改员工信息
语法:set 列名=“新的值” where 条件
#先用where查找对应人的信息,再使用set来修改列名对应的值为“新的值”
注意:要想操作员工信息表,必须先登录,登陆认证需要用装饰器完成
其他需求尽量用函数实现
1 username=input('请输入您的用户名:') 2 password=input('请输入您的密码:') 3 def login(func): 4 def inner(*args,**kwargs): 5 username_1=input('请输入您的登录用户名:') 6 password_1=input('请输入您的登录密码:') 7 if username_1==username.strip() and password_1==password.strip(): 8 print('登录成功') 9 ret = func(*args, **kwargs) 10 return ret 11 else: 12 print('请输入正确的用户名和密码') 13 return inner 14 15 16 @login 17 def f(): 18 with open('list_of_info','r+',encoding='utf-8') as f: 19 for line in f: 20 i = line.strip().split(',') 21 # print(i) 22 print(i[0])
这是自己写的代码,暂时不知道怎么往下写了,先留着以后再写。