• python函数式编程之装饰器(二)


    以前用装饰器,都是定义好了装饰器后,使用@装饰器名的方法写入被装饰函数的正上方

    在这里,定义的装饰器都是没有参数的

    在定义装饰器的函数的时候,没有在括号里定义参数,这就叫做无参装饰器

    既然有无参装饰器,那么当然也就会有有参装饰器

    有参装饰器的定义和使用

    定义一个普通的装饰器

    db_path = "db.txt"
    
    login_dic = {
        "user": None,
        "status": None,
    }
    
    def auth(func):
        def wrapper(*args, **kwargs):
            if login_dic['user'] and login_dic['status']:
                res = func(*args, **kwargs)
                return res
    
            name = input("your name:").strip()
            password = input("your password:").strip()
            with open(db_path, "r", encoding="utf-8") as f:
                user_dic = eval(f.read())
    
            if name in user_dic and password == user_dic[name]:
                print("login ok")
                login_dic['user'] = name
                login_dic['status'] = True
                res = func(*args, **kwargs)
                return res
            else:
                print("login error")
        return wrapper
    
    @auth
    def index():
        print("welcome to index page")
    
    @auth
    def home(name):
        print("welcome to %s home page" % name)
    
    index()
    home()
    

    上面的例子是一个典型的基于文件的认证方式的装饰器

    在生产环境中,用户的认证方式可能有很多种,比如还有LDAP和数据库的认证方式

    如果想把上面的代码修改,添加基于不同认证方式的用户认证

    db_path = "db.txt"
    
    login_dic = {
        "user": None,
        "status": None,
    }
    
    def deco(auth_type):
        def auth(func):
            def wrapper(*args, **kwargs):
                if auth_type == "file":
                    if login_dic['user'] and login_dic['status']:
                        res = func(*args, **kwargs)
                        return res
    
                    name = input("your name:").strip()
                    password = input("your password:").strip()
                    with open(db_path, "r", encoding="utf-8") as f:
                        user_dic = eval(f.read())
    
                    if name in user_dic and password == user_dic[name]:
                        print("login ok")
                        login_dic['user'] = name
                        login_dic['status'] = True
                        res = func(*args, **kwargs)
                        return res
                    else:
                        print("login error")
                elif auth_type == 'ldap':
                    print("ldap认证方式")
                elif auth_type == "sql":
                    print("数据库的认证方式")
                else:
                    print("不知道的认证方式")
            return wrapper
        return auth
    

    有参装饰器的调用

    @deco(auth_type="file")
    def index():
        print("welcome to index page")
    
    @deco(auth_type="abc")
    def home(name):
        print("welcome to %s home page" % name)
    
    index()
    home()
    

    在上面的例子里,deco(auth_type="file")这个函数的执行以后就得到了auth函数,但是现在得到的auth函数跟以前的auth函数是不一样的现在的auth函数内部添加了认证方式这样一个参数

    这样添加auth_type参数后的结果就相当于加在index函数和home函数正上方的是auth函数,这样就为最开始时的auth函数添加了一个参数

    函数中的路由功能

    先定义3个函数,现在想做的是只需要用户输入字符串,就执行对应的函数

    可以通过为每个函数添加装饰器的功能,把普通函数添加到函数字典中,然后到函数字典中找到输入字符串对应的函数地址,加括号就可以运行

    类似于

    func_dic = {"f1": f1, "f2": f2, "f3": f3}
    
    def f1():
        print("f1 func")
    
    def f2():
        print("f2 func")
    
    def f3():
        print("f3 func")
    

    这里就要使用到有参装饰器

    定义有参装饰器

    def make_func_dic(key):
        def deco(func):
            func_dic[key] = func
    
        return deco
    

    用装饰器make_func_dic装饰函数

    func_dic = {}
    
    def make_func_dic(key):
        def deco(func):
            func_dic[key] = func
    
        return deco
    
    @make_func_dic("f1")		# 等同于deco(f1)
    def f1():
        print("f1 func")
    
    @make_func_dic("f2")		# 等同于deco(f2)
    def f2():
        print("f2 func")
    
    @make_func_dic("f3")		# 等同于deco(f3)
    def f3():
        print("f3 func")
    

    打印func_dic,得到结果

    {'f1': <function f1 at 0x0000000002D2A510>, 'f2': <function f2 at 0x0000000002D2A598>, 'f3': <function f3 at 0x0000000002D2A620>}
    

    可以看到func_dic字典中,每个字符串对应的是相应字符串的函数的内存地址

    这样想运行f1函数,只需要把func_dic中的"f1"的值加括号运行就可以了

    然后使用户输入字符串,运行字符串对应的函数

    while True:
        cmd=input(">>: ").strip()
        if cmd in func_dic:
            func_dic[cmd]()
    

    这样就可以得到想要的结果了

    这样做的好处:只需要输入对应的字符串,就可以执行对应的函数,不需要使用if和elif对输入的字符串进行一条一条的判断

    这就是使用装饰器在代码级别达到函数的路由功能

  • 相关阅读:
    B/S与C/S的联系与区别
    ASP.NET中常用的26个优化性能方法(二)
    ASP.NET下如何防范SQL注入式攻击
    ASP.NET中常用的26个优化性能方法(一)
    Invoke and BeginInvoke BeginInvoke和EndInvoke方法 (转)
    C#中海量数据的批量插入和更新 [转]
    [译文]从底层角度看ASP.NETA lowlevel Look at the ASP.NET Architecture( 转)
    C#制作Windows service服务系列
    通过C++ Interop把Windows窗体集成到MFC应用程序中
    【转】.NET内存管理、垃圾回收
  • 原文地址:https://www.cnblogs.com/renpingsheng/p/8516306.html
Copyright © 2020-2023  润新知