1.利用有参装饰器编写多种用户登录校验策略
"""
1.直接写死的 jason 123
2.数据来源于列表 ['jason|123','kevin|321','tony|222']
3.数据来源于文件 jason|123\n tom|321\n
"""
有参装饰器套路模板:
def login_auth(condition):
def outer(func):
def inner(*args, **kwargs):
username = input('shuruyonghuming')
password = input('shurumima')
if condition == 'absolut':
if username == 'jason' and password == '123':
res = func(*args, **kwargs)
return res
if condition == 'list_type':
l1 = ['jason|123', 'kevin|321', 'tony|222']
for i in l1:
realname, realpwd = i.split('|')
if realname == username and realpwd == password:
res = func(*args, **kwargs)
return res
if condition == 'file_type':
with open(r'a.txt', 'r', encoding='utf8') as f:
for i in f:
realname,realpwd = i.split('|')
if realname == username and realpwd.strip('\n') == password:
res = func(*args, **kwargs)
return res
return res
return inner
return outer
@login_auth('absolut')
def index():
print('打印index')
index()
@login_auth('list_type')
def fooo():
print('打印aaa')
fooo()
@login_auth('file_type')
def qqqqq():
print('打印cccccccc')
qqqqq()
11
标签:return,python,res,--,123,kwargs,第十五,jason,def
From: https://www.cnblogs.com/tengyifan888/p/16794769.html