装饰器的使用方式

装饰器的使用方式

无参装饰器

user_data={'username':'dream','password':'521'}
bank_data={'dream':{'pay_pwd':'521','balance':1000}}

def register_input(func):
    def inner(*args, **kwargs):
        username = input("请输入用户名 :>>>> ").strip()
        password = input("请输入密码 :>>>> ").strip()
        if user_data['username'] == username and user_data['password'] == password:
            return func(*args, **kwargs)
        else:
            return f'用户和密码错误!'
    return inner
def balance_input(func):
    def inner(*args, **kwargs):
        password = input("请输入支付密码 :>>>> ").strip()
        bank_pwd=bank_data['dream'].get('pay_pwd')
        if password!=bank_pwd:
            return True, "支付密码错误"

        balance = input("请输入金额 :>>>> ").strip()
        money=bank_data['dream'].get('balance')
        # 校验余额是否为数字
        if not balance.isdigit():
            return False, f"余额不能小于零"
        # 校验余额是否大于0小于总余额
        if int(balance) <= 0 or int(balance) > money:
            return False, f"余额不足,当前余额为:{money}"

        return func(balance=balance,*args, **kwargs)
    return inner

@register_input
@balance_input
def get_balance(*args, **kwargs):
    username=user_data['username']
    return True, f'用户 {username} 提现 {kwargs.get("balance")} 完成!'

a=get_balance()
print(a)

有参装饰器

user_data={'username':'dream','password':'521'}
bank_data={'dream':{'pay_pwd':'521','balance':1000}}

def aaa(tag_func=None,tag=None):
    if tag_func=='login':
        def register_input(func):
            def inner(*args, **kwargs):
                username = input("请输入用户名 :>>>> ").strip()
                password = input("请输入密码 :>>>> ").strip()
                if user_data['username'] == username and user_data['password'] == password:
                    return func(*args, **kwargs)
                else:
                    return f'用户和密码错误!'
            return inner
        return register_input
    elif tag_func=='balance':
        def balance_input(func):
            def inner(*args, **kwargs):
                password = input("请输入支付密码 :>>>> ").strip()
                bank_pwd=bank_data['dream'].get('pay_pwd')
                if password!=bank_pwd:
                    return True, "支付密码错误"
                if tag=='add':
                    balance = input("请输入金额 :>>>> ").strip()
                else:
                    balance = input("请输入金额 :>>>> ").strip()
                    money=bank_data['dream'].get('balance')
                    # 校验余额是否为数字
                    if not balance.isdigit():
                        return False, f"余额不能小于零"
                    # 校验余额是否大于0小于总余额
                    if int(balance) <= 0 or int(balance) > money:
                        return False, f"余额不足,当前余额为:{money}"

                return func(balance=int(balance),*args, **kwargs)
            return inner
        return balance_input

取款

@aaa(tag_func='login')
@aaa(tag_func='balance')
def get_balance(*args, **kwargs):
    username=user_data['username']
    bank_data['dream']['balance']-=kwargs.get("balance")
    print(bank_data)
    return True, f'用户 {username} 提现 {kwargs.get("balance")} 完成!'
a=get_balance()
print(a)


请输入用户名 :>>>> dream
请输入密码 :>>>> 521
请输入支付密码 :>>>> 521
请输入金额 :>>>> 123
{'dream': {'pay_pwd': '521', 'balance': 877}}
(True, '用户 dream 提现 123 完成!')

存款

@aaa(tag_func='login')
@aaa(tag_func='balance',tag='add')
def get_balance(*args, **kwargs):
    username=user_data['username']
    bank_data['dream']['balance']+=kwargs.get("balance")
    print(bank_data)
    return True, f'用户 {username} 存款 {kwargs.get("balance")} 完成!'
a=get_balance()
print(a)


请输入用户名 :>>>> dream
请输入密码 :>>>> 521
请输入支付密码 :>>>> 521
请输入金额 :>>>> 2569
{'dream': {'pay_pwd': '521', 'balance': 3569}}
(True, '用户 dream 存款 2569 完成!')

伪装装饰器

from functools import wraps
def wrapper(func):
    @wraps(func)
    def inner(*args, **kwargs):
        '''
        # 这是验证登录的装饰器
            --- 确认账号是 dream 密码是 521 才是超级管理员
        '''
        return func(*args, **kwargs)
    return inner

@wrapper
def add():
    return 1 + 1

print(help(add))



#add()

#None
posted @ 2023-12-12 14:12  蓝幻ﹺ  阅读(5)  评论(0编辑  收藏  举报