python课堂整理22----装饰器进阶

一、函数闭包模拟session

user_dic = {'username': 'None', 'login': False}
def auth_func(func):
    def wrapper(*args, **kwargs):
        if user_dic['username'] and user_dic['login']:
            res = func(*args, **kwargs)
            return res
        else:
            username = input("请输入用户名:")
            passwd = input("请输入密码:")
            if username == 'jinling' and passwd == '123':
                user_dic['username'] = username
                user_dic['login'] = True
                res = func(*args, **kwargs)
                return res
            else:
                print('用户名或密码错误')
    return wrapper

@auth_func
def index():
    print('欢迎来到京东主页')
@auth_func
def home(name):
    print('欢迎回家%s' %name)
index()
home('jinling')

多账号认证:

user_list = [
    {'name': 'liu', 'passwd': '521'},
    {'name': 'jinling', 'passwd': '123'},
    {'name': 'lala', 'passwd': '456'},
    {'name': 'sb', 'passwd': '000'},
    {'name': 'wocao', 'passwd': '222'}
    ]
user_dic = {'username': 'None', 'login': False}
def auth_func(func):
    def wrapper(*args, **kwargs):
        if user_dic['username'] and user_dic['login']:
            res = func(*args, **kwargs)
            return res
        else:
            username = input('请输入用户名:')
            passwd = input('请输入密码:')
            for user in user_list:
                if username == user['name'] and passwd == user['passwd']:
                    user_dic['username'] = username
                    user_dic['login'] = True
                    res = func(*args, **kwargs)
                    return res
            else:
                print('用户名或密码错误')
    return wrapper

@auth_func
def index():
    print('欢迎来到京东主页')
@auth_func
def home():
    print('欢迎回家')
index()
home()

添加认证方式:带参装饰器

user_list = [
    {'name': 'liu', 'passwd': '521'},
    {'name': 'jinling', 'passwd': '123'},
    {'name': 'lala', 'passwd': '456'},
    {'name': 'sb', 'passwd': '000'},
    {'name': 'wocao', 'passwd': '222'}
    ]
user_dic = {'username': 'None', 'login': False}
def auth(auth_type = 'filedb'):
    def auth_func(func):
        def wrapper(*args, **kwargs):
            if auth_type == 'filedb':
                if user_dic['username'] and user_dic['login']:
                    res = func(*args, **kwargs)
                    return res
                else:
                    username = input('请输入用户名:')
                    passwd = input('请输入密码:')
                    for user in user_list:
                        if username == user['name'] and passwd == user['passwd']:
                            user_dic['username'] = username
                            user_dic['login'] = True
                            res = func(*args, **kwargs)
                            return res
                    else:
                        print('用户名或密码错误')
            elif auth_type == 'dsb':
                print('难搞哦')
        return wrapper

    return auth_func
@auth(auth_type='filedb')
def index():
    print('欢迎来到京东主页')
@auth(auth_type='dsb')
def home():
    print('欢迎回家')
index()
home()

posted @ 2019-07-30 17:04  绝世刘  阅读(170)  评论(0编辑  收藏  举报