码迷,mamicode.com
首页 > 其他好文 > 详细

装饰器

时间:2018-10-10 22:09:08      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:sed   span   user   put   ica   **kwargs   input   ret   def   

import time
user,passwd = alex,abc123
def auth(auth_type):
    print("auth func:",auth_type)
    def outer_wrapper(func):
        print("out_wrapper func:",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()
home() #wrapper()
bbs()

 

装饰器

标签:sed   span   user   put   ica   **kwargs   input   ret   def   

原文地址:https://www.cnblogs.com/khfuu1706165/p/9768693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!