美文网首页
函数 三

函数 三

作者: 吃可爱长大鸭 | 来源:发表于2021-02-06 14:43 被阅读0次

    无参装饰器模板

    def outh(func):
        def wrapper(*args,**kwargs):
            res=func(*args,**kwargs)
            return res
        return wrapper
    @outh
    #无参装饰器案例
    import time
    from functools import wraps
    def auth(func):
        @wraps(func)
        def wrapper(*args,**kwargs):
            name=input('name>>>: ').strip()
            pwd=input('pwd>>>: ').strip()
            if name == 'egon' and pwd == '123':
                print('login successfull')
                res=func(*args,**kwargs)
                return res
            else:
                print('user or pwd error')
        return wrapper
    
    
    def outter(func):
        def wrapper(*args,**kwargs):
            res=func(*args,**kwargs)
            return res
        return wrapper
    
    @auth
    def index():
        """
        sadadsda
        :return:
        """
        time.sleep(1)
        print('welcome to index page')
        return 1234
    
    print(index.__name__)
    print(index.__doc__)
    index()
    

    有参装饰器模板

    def auth(engine='file'):
        def outter(func):
            def wrapper(*args, **kwargs):
                res = func(*args, **kwargs)
                return res
            return wrapper
        return outter
    
    @auth('file')
    #有参装饰器案例
    import time
    
    current_user={'user':None}
    
    def auth(engine='file'):
        def outter(func):
            def wrapper(*args,**kwargs):
                if current_user['user'] is not None:
                    res=func(*args,**kwargs)
                    return res
    
                user=input('username>>>: ').strip()
                pwd=input('password>>>: ').strip()
    
                if engine == 'file':
                    # 基于文件的认证
                    if user == 'egon' and pwd == '123':
                        print('login successfull')
                        current_user['user']=user
                        res=func(*args,**kwargs)
                        return res
                    else:
                        print('user or password error')
                elif engine == 'mysql':
                    # 基于mysql的认证
                    print('基于mysql的认证')
                elif engine == 'ldap':
                    # 基于ldap的认证
                    print('基于ldap的认证')
                else:
                    print('不知道engine')
            return wrapper
        return outter
    
    @auth('file') #@outter #index=outter(index) # index=wrapper
    def index():
        time.sleep(1)
        print('from index')
    
    @auth('mysql') #@outter # home=outter(home) #home=wrapper
    def home(name):
        print('welcome %s' %name)
    
    index()
    home('egon')
    

    叠加多个装饰器

    #自上而下
    import time
    from functools import wraps
    def timmer(func):
        @wraps(func)
        def aaa(*args,**kwargs):
            start=time.time()
            res=func(*args,**kwargs) #最原始那个index的内存地址()
            stop=time.time()
            print('run time is %s' %(stop -start))
            return res
        return aaa
    
    
    def auth(func):
        @wraps(func)
        def bbb(*args,**kwargs):
            name=input('name>>>: ').strip()
            pwd=input('pwd>>>: ').strip()
            if name == 'mzk' and pwd == '123':
                print('login successfull')
                res=func(*args,**kwargs)
                return res
            else:
                print('user or pwd error')
        return bbb
    
    @timmer 
    @auth
    def index():
        """
        jklkljlkjlkjk
        :return:
        """
        time.sleep(1)
        print('from index')
    print(index.__doc__)
    index()
    

    相关文章

      网友评论

          本文标题:函数 三

          本文链接:https://www.haomeiwen.com/subject/vuhotltx.html