Python小白学习之路(二十五)—【装饰器的应用】

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

回顾:装饰器的框架

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:(初步实现一个人验证功能)

 1 def check(func):
 2     def wrapper(*args,**kwargs):
 3         username = input('请输入用户名==>').strip()
 4         passwd = input('请输入密码==>').strip()
 5         if username == 'xhg' and passwd == '123':
 6             res = func(*args,**kwargs)
 7             return res
 8         else:
 9             print('用户名或者密码错误,请重新登录')
10     return wrapper
11 @check
12 def index():
13     print('欢迎来到京东主页')
14 @check
15 def home(name):
16     print('欢迎回家%s' %name)
17 @check
18 def shopping_car(name):
19     print('%s购物车里有[%s,%s,%s]' %(name,'奶茶','蛋糕','娃娃'))
20 
21 index()
22 home('xhg')
23 shopping_car('xhg')

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

继续优化

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

 1 current_user = {'username': None, 'passwd':False }
 2 def check(func):
 3     def wrapper(*args,**kwargs):
 4         if current_user['username'] and current_user['passwd']:
 5             res = func(*args, **kwargs)
 6             return res
 7         username = input('请输入用户名==>').strip()
 8         passwd = input('请输入密码==>').strip()
 9         if username =='xhg' and passwd == '123':
10             current_user['username'] = username
11             current_user['passwd'] = True
12             res = func(*args,**kwargs)
13             return res
14         else:
15             print('用户名或者密码错误,请重新登录')
16     return wrapper
17 @check
18 def index():
19     print('欢迎来到京东主页')
20 @check
21 def home(name):
22     print('欢迎回家%s' %name)
23 @check
24 def shopping_car(name):
25     print('%s购物车里有[%s,%s,%s]' %(name,'奶茶','蛋糕','娃娃'))
26 
27 index()
28 home('xhg')
29 shopping_car('xhg')

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

继续优化

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

 1 user_list=[
 2     {'name':'a','passwd':'111'},
 3     {'name':'b','passwd':'222'},
 4     {'name':'c','passwd':'333'},
 5     {'name':'d','passwd':'444'},
 6 ]
 7 current_user = {'username': None, 'login': False}
 8 def check(func):
 9     def wrapper(*args,**kwargs):
10             if current_user['username'] and current_user['login']:
11                 res = func(*args,**kwargs)
12                 return res
13             username = input('请输入用户名==>').strip()
14             passwd = input('请输入密码==>').strip()
15             for user in user_list:
16                 if username == user['name'] and passwd == user['passwd']:
17                     current_user['username'] = username
18                     current_user['login'] = True
19                     res = func(*args,**kwargs)
20                     return res
21             else:
22                 print('用户名或者密码输入错误,请重新输入')
23     return wrapper
24 @check
25 def index():
26     print('欢迎来到京东主页')
27 @check
28 def home(name):
29     print('欢迎回家%s'%name)
30 @check
31 def shopping_car(name):
32     print('%s购物车里有[%s,%s,%s]'%(name, '奶茶', '蛋糕', '娃娃'))
33 index()
34 home(current_user['username'])
35 shopping_car(current_user['username'])

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

原文地址:https://www.cnblogs.com/guoruxin/p/10108767.html