首页 技术 正文
技术 2022年11月18日
0 收藏 817 点赞 3,505 浏览 2239 个字

通过一个任务来加深对装饰器的理解和应用

回顾:装饰器的框架

def timmer(func):
def wrapper():
func()
return wrapper

任务:给以下正在运行的程序加一个验证功能的装饰器正在运行的程序加一个验证功能的装饰器

def index():
print('欢迎来到京东主页')def home(name):
print('欢迎回家%s' %name)def shopping_car(name):
print('%购物车里有[%s,%s,%s]' %(name,'奶茶','蛋糕','娃娃'))

Step1:(初步实现一个人验证功能)

 def check(func):
def wrapper(*args,**kwargs):
username = input('请输入用户名==>').strip()
passwd = input('请输入密码==>').strip()
if username == 'xhg' and passwd == '':
res = func(*args,**kwargs)
return res
else:
print('用户名或者密码错误,请重新登录')
return wrapper
@check
def index():
print('欢迎来到京东主页')
@check
def home(name):
print('欢迎回家%s' %name)
@check
def shopping_car(name):
print('%s购物车里有[%s,%s,%s]' %(name,'奶茶','蛋糕','娃娃')) index()
home('xhg')
shopping_car('xhg')

#上述程序可以实现基本功能
#缺点:每次执行一个函数时,均需要重新登录。

继续优化

Step2:(实现单个人在登录状态下验证一次)

 current_user = {'username': None, 'passwd':False }
def check(func):
def wrapper(*args,**kwargs):
if current_user['username'] and current_user['passwd']:
res = func(*args, **kwargs)
return res
username = input('请输入用户名==>').strip()
passwd = input('请输入密码==>').strip()
if username =='xhg' and passwd == '':
current_user['username'] = username
current_user['passwd'] = True
res = func(*args,**kwargs)
return res
else:
print('用户名或者密码错误,请重新登录')
return wrapper
@check
def index():
print('欢迎来到京东主页')
@check
def home(name):
print('欢迎回家%s' %name)
@check
def shopping_car(name):
print('%s购物车里有[%s,%s,%s]' %(name,'奶茶','蛋糕','娃娃')) index()
home('xhg')
shopping_car('xhg')

#缺点:显示生活中并不可能只有一个用户,要考虑多个用户的情况

继续优化

step3:(登录状态与数据库进行匹配,并在登录状态下验证一次)

 user_list=[
{'name':'a','passwd':''},
{'name':'b','passwd':''},
{'name':'c','passwd':''},
{'name':'d','passwd':''},
]
current_user = {'username': None, 'login': False}
def check(func):
def wrapper(*args,**kwargs):
if current_user['username'] and current_user['login']:
res = func(*args,**kwargs)
return res
username = input('请输入用户名==>').strip()
passwd = input('请输入密码==>').strip()
for user in user_list:
if username == user['name'] and passwd == user['passwd']:
current_user['username'] = username
current_user['login'] = True
res = func(*args,**kwargs)
return res
else:
print('用户名或者密码输入错误,请重新输入')
return wrapper
@check
def index():
print('欢迎来到京东主页')
@check
def home(name):
print('欢迎回家%s'%name)
@check
def shopping_car(name):
print('%s购物车里有[%s,%s,%s]'%(name, '奶茶', '蛋糕', '娃娃'))
index()
home(current_user['username'])
shopping_car(current_user['username'])

#感想:
当拿到一个要求,不要急于一下子写出完美的程序。要不断去思考、去联想、去调试。去想象还有什么需要完善的地方
自己还是练得少
以后要多加练习

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781