• day04_02 装饰器 高阶版


    __author__ = "Alex Li"
    
    """
    import time
    user,passwd = 'alex','abc123'
    def auth(auth_type):
        print("auth func:",auth_type)
        def outer_wrapper(func):
            def wrapper(*args, **kwargs):
                print("wrapper func args:", *args, **kwargs)
                if auth_type == "local":
                    username = input("Username:").strip()
                    password = input("Password:").strip()
                    if user == username and passwd == password:
                        print("\033[32;1mUser has passed authentication\033[0m")
                        res = func(*args, **kwargs)  # from home
                        print("---after authenticaion ")
                        return res
                    else:
                        exit("\033[31;1mInvalid username or password\033[0m")
                elif auth_type == "ldap":
                    print("搞毛线ldap,不会。。。。")
    
            return wrapper
        return outer_wrapper
    
    def index():
        print("welcome to index page")
    
    @auth(auth_type="local") # home = wrapper()
    def home():
        print("welcome to home  page")
        return "from home"
    
    @auth(auth_type="ldap")
    def bbs():
        print("welcome to bbs  page")
    
    index()
    print(home()) #wrapper()
    bbs()
    """
    
    
    import time
    user,passwd = 'alex','abc123'
    
    def auth(auth_type):
        print("auth func:", auth_type)
        def outer_wrapper(func):
            def wrapper(*args,**kwargs):
                if auth_type == "local":
                    print("wrapper func args:", *args,**kwargs)
                    username = input("Username:").strip()
                    password = input("Password:").strip()
    
                    if user == username and password == password:
                        print("\033[32;1mUser has passed authentication\033[0m")
                        # func(*args,**kwargs)    #from home
                        res = func(*args, **kwargs)  # res from home()
                        print("---after authenticaion ",res)
                        return res
                    else:
                        exit("\033[31;1mInvalid username or password\033[0m")
                elif auth_type == "ldap":
                    print("搞毛线ldap,不会。。。。")
            return wrapper
        return outer_wrapper
    
    
    def index():
        print("welcome to index page")
    @auth(auth_type="local")    #1.home = auth() 2.home = wrapper()
    def home():
        print("welcome to home  page")
        return "from home"
    
    @auth(auth_type="ldap")
    def bbs():
        print("welcome to bbs  page")
    
    index()
    home()  #3.wrapper()
    bbs()
    
  • 相关阅读:
    jquery全屏幻灯轮播焦点图
    PHP curl 上传文件版本兼容问题
    一个网站同一域名不同目录下的文件访问到的cookie值不同是什么原因?
    Linux系统查找清理磁盘大文件方法
    REDIS常用命令
    CentOS 6.4安装pip,CentOS安装python包管理安装工具pip的方法
    centos6 编译安装nodejs4.3
    centos yum安装php5.6.19 remi源按照
    Mac下用brew搭建PHP(LNMP/LAMP)开发环境
    为什么JAVA要提供 wait/notify 机制?是为了避免轮询带来的性能损失
  • 原文地址:https://www.cnblogs.com/netflix/p/14854388.html
Copyright © 2020-2023  润新知