decorator重新编写登录程序

# _*_coding:utf-8_*_
# 作者:王佃元
# 日期:2019/12/26
import time
pages = ['home page', 'finance page', 'book page']
is_login = False


def decorator(jingdong_pay):
def wrapper(*args, **kwargs):
global is_login
if is_login:
jingdong_pay(*args, **kwargs)
else:
is_login = login()
return wrapper


def login():
user_info, user_num = get_user_info()
username = input('请输入用户名:')
password = input('请输入密码:')
if username in user_info and password == user_info[username]:
print('欢迎进入{}!'.format(pages[user_num]))
return True
else:
return False


def get_user_info():
for page in pages:
print('%d:' % (pages.index(page) + 1), page)
user_num = int(input('请输入登录页面编号:'))
if user_num == 1:
with open('jingdong', 'r', encoding='utf-8') as jingdong:
user_info = eval(jingdong.read().strip())
if user_num == 2:
with open('weixin', 'r', encoding='utf-8') as weixin:
user_info = eval(weixin.read().strip())
return user_info, user_num


@decorator
def jingdong_pay(money):
print('正在付款......')
time.sleep(2)
print('付款成功,您支付{}元'.format(money))


jingdong_pay(1800)
jingdong_pay(2000)

你可能感兴趣的:(decorator重新编写登录程序)