python装饰器之登录练习(老男孩)
程序员文章站
2022-07-15 08:16:41
...
-- coding: utf-8 --
@Time : 19-3-16 上午11:37
@Author : Slip
@Software: PyCharm
@e-mail : [email protected]
jdlogin_status = False
wxlogin_status = False
key = 0
def logger(auth_type):
def login(f):
global jdlogin_status #获取全局变量来实现该变量的赋值修改
global wxlogin_status
if jdlogin_status == False:
username = input("Username:").strip()
passwd = input("passwd:").strip()
a = open(auth_type, 'r', encoding='utf8') #这里的auth_type 也是调用的文件的文件名
at = a.read()
a.close()
dic = eval(at)
if username in dic and passwd == dic[username]:#判断用户名和密码是否在文件之中
f()
jdlogin_status =True
else:
print('账号密码错误')
elif wxlogin_status == False:
username = input("Username:").strip()
passwd = input("passwd:").strip()
a = open(auth_type, 'r', encoding='utf8')
at = a.read()
a.close()
dic = eval(at)
if username in dic and passwd == dic[username]:
f()
wxlogin_status = True
else:
print('账号密码错误')
elif jdlogin_status == True:
f()
elif wxlogin_status == True:
f()
else:
pass
return login
def judge(key):
if key == 1:
@logger("jingdong")
def home():
print('welcome to home\n')
elif key == 2:
@logger("weixin")
def finance():
print('welcome to finance\n')
elif key == 3:
@logger("jingdong")
def book():
print('welcome to book\n')
def start():
global key
list = {1: 'home', 2: 'finance', 3: 'book'}
for i in list:
print(i, list[i])
select_num = input('请输入访问网页的编号[退出:q]>>')
if select_num == 'q':
exit()
elif select_num.isdigit() and select_num in list:
key = int(select_num)
else:
print('请输入有效值!')
return
judge(key)
while 1:
start()
文件 jingdong 为
{‘aa’: ‘bb’, ‘bb’: ‘cc’, ‘dd’: ‘cc’}
文件 weixin 为
{‘11’: ‘22’, ‘33’: ‘44’, ‘55’: ‘44’}