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

Posted dabai123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()

技术图片

以上是关于python课堂整理22----装饰器进阶的主要内容,如果未能解决你的问题,请参考以下文章

python装饰器2:进阶

Python进阶装饰器(Decorator)

Python进阶----装饰器

课堂练习-python 装饰器

进阶Python:装饰器 全面详解

Python进阶第九篇装饰器