• Python学习之路:装饰器实现终极版


    网站实现验证功能装饰器:

    import time
    user,passwd='alex','abc123'
    def auth(func):
        def wrapper(*args,**kwargs):
            print("wraper func args:",*args,**kwargs)
            username=input("Username:").strip()
            password=input("Password:").strip()
    
            if user==username and passwd==password:
                print("33[32;1mUser has passed authentication33[0m")
                func(*args,**kwargs)#
                #print("---after authentication---")#保留要装饰函数home的输出结果
                #return res
            else:
                exit("33[31;1mInvalid username or password33[0m")
        return wrapper
    
    
    def index():
        print("welcome to index page")
    
    @auth
    def home():
        print("welcome to home page ")
        return "from home"
    
    @auth
    def bbs():
        print("welcome to bbs page")
    
    index()
    home()
    print(home())#执行结果为空,调用home相当于调用wraper
    bbs()
    

     保留要装饰函数的返回结果:

    import time
    user,passwd='alex','abc123'
    def auth(func):
        def wrapper(*args,**kwargs):
            print("wraper func args:",*args,**kwargs)
            username=input("Username:").strip()
            password=input("Password:").strip()
    
            if user==username and passwd==password:
                print("33[32;1mUser has passed authentication33[0m")
                res=func(*args,**kwargs)#
                print("---after authentication---")#保留要装饰函数home的输出结果
                return res
            else:
                exit("33[31;1mInvalid username or password33[0m")
        return wrapper
    
    
    def index():
        print("welcome to index page")
    
    @auth
    def home():
        print("welcome to home page ")
        return "from home"
    
    @auth
    def bbs():
        print("welcome to bbs page")
    
    index()
    home()
    print(home())#执行结果为空,调用home相当于调用wraper
    bbs()
    

     不同网页不同验证方式的装饰器:

    import time
    user,passwd='alex','abc123'
    def auth(auth_type):
        print("auth func:",auth_type)
        def outer_auth(func):
            def wrapper(*args,**kwargs):
                print("wraper func args:",*args,**kwargs)
                if auth_type=="local":
                    username=input("Username:").strip()
                    password=input("Password:").strip()
    
                    if user==username and passwd==password:
                        print("33[32;1mUser has passed authentication33[0m")
                        res=func(*args,**kwargs)#
                        print("---after authentication---")#保留要装饰函数home的输出结果
                        return res
                    else:
                        exit("33[31;1mInvalid username or password33[0m")
                elif auth_type=="ldap":
                    print("搞毛线ldap,不会。。。。")
            return wrapper
        return outer_auth
    
    def index():
        print("welcome to index page")
    
    @auth(auth_type="local")
    def home():
        print("welcome to home page ")
        return "from home"
    
    @auth(auth_type="ldap")
    def bbs():
        print("welcome to bbs page")
    
    index()
    home()
    print(home())#执行结果为空,调用home相当于调用wraper
    bbs()
    
  • 相关阅读:
    sublime less css less-plugin-clean-css lessc
    matplotlib 中文显示问题
    关闭edge新标签页广告
    刷写网卡MAC地址
    tar打包命令
    Print Spooler服务意外停止
    使用cmd命令为windows添加环境变量
    wps多用户使用问题
    屏蔽知乎登录弹窗
    Excel中无法移动分页符
  • 原文地址:https://www.cnblogs.com/xiaobai005/p/7909175.html
Copyright © 2020-2023  润新知