天天看點

python 裝飾器 (列舉flask 架構路由裝飾器 和 django源碼裝飾器,和常見的裝飾器)五種裝飾器帶你飛

python裝飾器可以在不改變原有函數代碼的基礎上,實作一些新功能的加入,是python開發的一大神器。

下面結合我自身的學習經曆列舉一些常見的裝飾器,以供大家借鑒參考:

0 通用裝飾器

特點:

functools.wraps 裝飾參數func後,裝飾器生成的新test函數可保留自己的函數名和文檔資訊

形參設為不定長參數,也可根據需要自行定義

import functools
def set_fun(func):
    @functools.wraps(func)
    def call_fun(*args,**kwargs):
        # 添加所需的功能
        &"填寫需要增加的功能"&
        return func(*args,**kwargs)
    return call_fun

@set_fun
def test(*args,**kwargs):
    pass
           

1 入門裝飾器

單層函數實作裝飾器

特點:簡介,優美的實作了裝飾器的功能,結構簡單清晰,裝飾器中不運作被裝飾函數,不涉及傳參問題

         類似于中間件的代碼風格,

def set_func(func):

    print(func.__name__)
    # 增加需要的功能
    return func


@set_func
def index():
    print("hello,world")

index()
           

2 進階裝飾器

雙層函數裝飾器,可傳遞參數value

def route(value):

    def set_func(func):
        print(value)
        print(func.__name__)
        # 添加需要的功能
        return func

    return set_func

@route("hhhhh") # 實作向裝飾器傳參
def index():
    print("hello,world")
           

3 flask route 路由功能路由裝飾器源代碼

flask 路由裝飾器函數,在flask0.1版本中定義于Flask 類下的一個執行個體方法

執行個體方法定義為裝飾器,同時實作傳遞複雜參數的操作,值得參考借鑒

class Flask(object):
    ...

    def add_url_rule(self, rule, endpoint, **options):
        “”“裝飾器調用函數”“”
        options['endpoint'] = endpoint
        options.setdefault('methods', ('GET',))
        self.url_map.add(Rule(rule, **options))

    def route(self, rule, **options):
        “”“路由功能裝飾器”“”
        def decorator(f):
            self.add_url_rule(rule, f.__name__, **options)
            self.view_functions[f.__name__] = f
            return f
        return decorator
           

4 django 檢查使用者是否登入裝飾器

這裡列出的是django2.0 架構中auth 使用者子產品的一個裝飾器,目的檢查使用者是否登入之類的功能。

三層閉包函數實作傳參和裝飾功能,結構更容易了解,也很值得借鑒。

def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
    """
    Decorator for views that checks that the user passes the given test,
    redirecting to the log-in page if necessary. The test should be a callable
    that takes the user object and returns True if the user passes.
    """

    def decorator(view_func):
        @wraps(view_func)
        def _wrapped_view(request, *args, **kwargs):
            if test_func(request.user):
                return view_func(request, *args, **kwargs)
            path = request.build_absolute_uri()
            resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)
            # If the login url is the same scheme and net location then just
            # use the path as the "next" url.
            login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
            current_scheme, current_netloc = urlparse(path)[:2]
            if ((not login_scheme or login_scheme == current_scheme) and
                    (not login_netloc or login_netloc == current_netloc)):
                path = request.get_full_path()
            from django.contrib.auth.views import redirect_to_login
            return redirect_to_login(
                path, resolved_login_url, redirect_field_name)
        return _wrapped_view
    return decorator
           

end