Python学习笔记------带返回值的装饰器(二)

# 全局变量
user_dic={'userName':None, 'login':False}
# user_NameandPass=[
#     {'userName':'alex', 'password':'123'},
#     {'userName':'alex1', 'password':'1234'},
#     {'userName':'alex2', 'password':'12345'},
#     {'userName':'alex3', 'password':'123456'},
#     {'userName':'alex4', 'password':'1234567'},
# ]
def readDataFromFile():
    with open('people') as file:#读取文件内的信息
        for i in file:
            yield  i

data = readDataFromFile()#文件中的所有数据
def auth(auth_type = 'filedb'):
    def auth_login(func):
       print('认证类型为=======%s' %auth_type)
       def login(*args, **kwargs):
        if user_dic['userName'] and user_dic['login']:
            res = func(*args, **kwargs)
            return res
        username = input('用户名: ').strip()
        password = input('密码: ').strip()
        for i in data:
            userdict = eval(i)
            if username == userdict['userName'] and password == userdict['password']:
                user_dic['userName'] = username
                user_dic['login'] = True
                res = func(*args, **kwargs)
                return res
        else:#for循环遍历完,仍然没有找到正确的
            print('用户名或密码错误')
       return  login
    return  auth_login

@auth(auth_type='filedb')
def first(name):
    print('%s欢迎来到京东首页' %name)

@auth(auth_type='filedb')
def home(name):
    print('%s欢迎回家' %name)


@auth(auth_type='filedb')
def shoping_car(name):
    print('%s的购物车里面有[%s, %s, %s]' %(name, '奶茶', '牛奶', '面包'))



first('alex')
home('asasa')
shoping_car('jms')






你可能感兴趣的:(编程语言,Python,人工智能)