通過 Python 裝飾器實現DRY(不重復代碼)原則

jopen 8年前發布 | 13K 次閱讀 Python Python開發

Python 裝飾器 是一個消除冗余的強大工具。隨著將功能模塊化為大小合適的方法,即使是最復雜的工作流,裝飾器也能使它變成簡潔的功能。

例如讓我們看看 Django web框架 ,該框架處理請求的方法接收一個方法對象,返回一個響應對象:

def handle_request(request):
    return HttpResponse("Hello, World")
defhandle_request(request):
    return HttpResponse("Hello, World")

我最近遇到一個案例,需要編寫幾個滿足下述條件的api方法:

  • 返回json響應
  • 如果是GET請求,那么返回錯誤碼

做為一個注冊api端點例子,我將會像這樣編寫:

def register(request):
    result = None
    # check for post only
    if request.method != 'POST':
        result = {"error": "this method only accepts posts!"}
    else:
        try:
            user = User.objects.create_user(request.POST['username'],
                                            request.POST['email'],
                                            request.POST['password'])
            # optional fields
            for field in ['first_name', 'last_name']:
                if field in request.POST:
                    setattr(user, field, request.POST[field])
            user.save()
            result = {"success": True}
        except KeyError as e:
            result = {"error": str(e) }
    response = HttpResponse(json.dumps(result))
    if "error" in result:
        response.status_code = 500
    return response
defregister(request):
    result = None
    # check for post only
    if request.method != 'POST':
        result = {"error": "this method only accepts posts!"}
    else:
        try:
            user = User.objects.create_user(request.POST['username'],
                                            request.POST['email'],
                                            request.POST['password'])
            # optional fields
            for fieldin ['first_name', 'last_name']:
                if fieldin request.POST:
                    setattr(user, field, request.POST[field])
            user.save()
            result = {"success": True}
        exceptKeyErroras e:
            result = {"error": str(e) }
    response = HttpResponse(json.dumps(result))
    if "error" in result:
        response.status_code = 500
    return response

然而這樣我將會在每個api方法中編寫json響應和錯誤返回的代碼。這將會導致大量的邏輯重復。所以讓我們嘗試用裝飾器實現DRY原則吧。

裝飾器簡介

如果你不熟悉裝飾器,我可以簡單解釋一下,實際上裝飾器就是有效的函數包裝器,python解釋器加載函數的時候就會執行包裝器,包裝器可以修改函數的接收參數和返回值。舉例來說,如果我想要總是返回比實際返回值大一的整數結果,我可以這樣寫裝飾器:

# a decorator receives the method it's wrapping as a variable 'f'
def increment(f):
    # we use arbitrary args and keywords to
    # ensure we grab all the input arguments.
    def wrapped_f(*args, **kw):
        # note we call f against the variables passed into the wrapper,
        # and cast the result to an int and increment .
        return int(f(*args, **kw)) + 1
    return wrapped_f  # the wrapped function gets returned.
# a decorator receives the method it's wrapping as a variable 'f'
defincrement(f):
    # we use arbitrary args and keywords to
    # ensure we grab all the input arguments.
    defwrapped_f(*args, **kw):
        # note we call f against the variables passed into the wrapper,
        # and cast the result to an int and increment .
        return int(f(*args, **kw)) + 1
    return wrapped_f  # the wrapped function gets returned.

現在我們就可以用@符號和這個裝飾器去裝飾另外一個函數了:

@increment
def plus(a, b):
    return a + b

result = plus(4, 6)
assert(result == 11, "We wrote our decorator wrong!")
@increment
defplus(a, b):
    return a + b
 
result = plus(4, 6)
assert(result == 11, "We wrote our decorator wrong!")

裝飾器修改了存在的函數,將裝飾器返回的結果賦值給了變量。在這個例子中,’plus’的結果實際指向increment(plus)的結果。

對于非post請求返回錯誤

現在讓我們在一些更有用的場景下應用裝飾器。如果在django中接收的不是POST請求,我們用裝飾器返回一個錯誤響應。

def post_only(f):
    """ Ensures a method is post only """
    def wrapped_f(request):
        if request.method != "POST":
            response = HttpResponse(json.dumps(
                {"error": "this method only accepts posts!"}))
            response.status_code = 500
            return response
        return f(request)
    return wrapped_f
defpost_only(f):
    """ Ensures a method is post only """
    defwrapped_f(request):
        if request.method != "POST":
            response = HttpResponse(json.dumps(
                {"error": "this method only accepts posts!"}))
            response.status_code = 500
            return response
        return f(request)
    return wrapped_f

現在我們可以在上述注冊api中應用這個裝飾器:

@post_only
def register(request):
    result = None
    try:
        user = User.objects.create_user(request.POST['username'],
                                        request.POST['email'],
                                        request.POST['password'])
        # optional fields
        for field in ['first_name', 'last_name']:
            if field in request.POST:
                setattr(user, field, request.POST[field])
        user.save()
        result = {"success": True}
    except KeyError as e:
        result = {"error": str(e) }
    response = HttpResponse(json.dumps(result))
    if "error" in result:
        response.status_code = 500
    return response
@post_only
defregister(request):
    result = None
    try:
        user = User.objects.create_user(request.POST['username'],
                                        request.POST['email'],
                                        request.POST['password'])
        # optional fields
        for fieldin ['first_name', 'last_name']:
            if fieldin request.POST:
                setattr(user, field, request.POST[field])
        user.save()
        result = {"success": True}
    exceptKeyErroras e:
        result = {"error": str(e) }
    response = HttpResponse(json.dumps(result))
    if "error" in result:
        response.status_code = 500
    return response

現在我們就有了一個可以在每個api方法中重用的裝飾器。

發送json響應

為了發送json響應(同時處理500狀態碼),我們可以新建另外一個裝飾器:

def json_response(f):
    """ Return the response as json, and return a 500 error code if an error exists """
    def wrapped(*args, **kwargs):
        result = f(*args, **kwargs)
        response = HttpResponse(json.dumps(result))
        if type(result) == dict and 'error' in result:
            response.status_code = 500
        return response
defjson_response(f):
    """ Return the response as json, and return a 500 error code if an error exists """
    defwrapped(*args, **kwargs):
        result = f(*args, **kwargs)
        response = HttpResponse(json.dumps(result))
        if type(result) == dictand 'error' in result:
            response.status_code = 500
        return response

現在我們就可以在原方法中去除json相關的代碼,添加一個裝飾器做為代替:

@post_only
@json_response
def register(request):
    try:
        user = User.objects.create_user(request.POST['username'],
                                        request.POST['email'],
                                        request.POST['password'])
        # optional fields
        for field in ['first_name', 'last_name']:
            if field in request.POST:
                setattr(user, field, request.POST[field])
        user.save()
        return {"success": True}
    except KeyError as e:
        return {"error": str(e) }
@post_only
@json_response
defregister(request):
    try:
        user = User.objects.create_user(request.POST['username'],
                                        request.POST['email'],
                                        request.POST['password'])
        # optional fields
        for fieldin ['first_name', 'last_name']:
            if fieldin request.POST:
                setattr(user, field, request.POST[field])
        user.save()
        return {"success": True}
    exceptKeyErroras e:
        return {"error": str(e) }

現在,如果我需要編寫新的方法,那么我就可以使用裝飾器做冗余的工作。如果我要寫登錄方法,我只需要寫真正相關的代碼:

@post_only
@json_response
def login(request):
    if request.user is not None:
        return {"error": "User is already authenticated!"}
    user = auth.authenticate(request.POST['username'], request.POST['password'])
    if user is not None:
        if not user.is_active:
            return {"error": "User is inactive"}
        auth.login(request, user)
        return {"success": True, "id": user.pk}
    else:
        return {"error": "User does not exist with those credentials"}
@post_only
@json_response
deflogin(request):
    if request.useris not None:
        return {"error": "User is already authenticated!"}
    user = auth.authenticate(request.POST['username'], request.POST['password'])
    if useris not None:
        if not user.is_active:
            return {"error": "User is inactive"}
        auth.login(request, user)
        return {"success": True, "id": user.pk}
    else:
        return {"error": "User does not exist with those credentials"}

BONUS: 參數化你的請求方法

我曾經使用過 Tubogears 框架,其中請求參數直接解釋轉遞給方法這一點我很喜歡。所以要怎樣在Django中模仿這一特性呢?嗯,裝飾器就是一種解決方案!

例如:

def parameterize_request(types=("POST",)):
    """
    Parameterize the request instead of parsing the request directly.
    Only the types specified will be added to the query parameters.

    e.g. convert a=test
defparameterize_request(types=("POST",)):
    """
    Parameterizetherequestinsteadofparsingtherequestdirectly.
    Onlythetypesspecifiedwillbeaddedto thequeryparameters.
 
    e.g. convert a=test

注意這是一個參數化裝飾器的例子。在這個例子中,函數的結果是實際的裝飾器。

現在我就可以用參數化裝飾器編寫方法了!我甚至可以選擇是否允許GET和POST,或者僅僅一種請求參數類型。

@post_only
@json_response
@parameterize_request(["POST"])
def register(request, username, email, password,
             first_name=None, last_name=None):
    user = User.objects.create_user(username, email, password)
    user.first_name=first_name
    user.last_name=last_name
    user.save()
    return {"success": True}
@post_only
@json_response
@parameterize_request(["POST"])
defregister(request, username, email, password,
            first_name=None, last_name=None):
    user = User.objects.create_user(username, email, password)
    user.first_name=first_name
    user.last_name=last_name
    user.save()
    return {"success": True}

現在我們有了一個簡潔的、易于理解的api。

BONUS #2: 使用functools.wraps保存docstrings和函數名

很不幸,使用裝飾器的一個副作用是沒有保存方法名(__name__)和docstring(__doc__)值:

def increment(f):
    """ Increment a function result """
    wrapped_f(a, b):
        return f(a, b) + 1
    return wrapped_f

@increment
def plus(a, b)
    """ Add two things together """
    return a + b

plus.__name__  # this is now 'wrapped_f' instead of 'plus'
plus.__doc__   # this now returns 'Increment a function result' instead of 'Add two things together'
defincrement(f):
    """ Increment a function result """
    wrapped_f(a, b):
        return f(a, b) + 1
    return wrapped_f
 
@increment
defplus(a, b)
    """ Add two things together """
    return a + b
 
plus.__name__  # this is now 'wrapped_f' instead of 'plus'
plus.__doc__  # this now returns 'Increment a function result' instead of 'Add two things together'

這將對使用反射的應用造成麻煩,比如Sphinx,一個 自動生成文檔的應用 。

為了解決這個問題,我們可以使用’wraps’裝飾器附加上名字和docstring:

from functools import wraps

def increment(f):
    """ Increment a function result """
    @wraps(f)
    wrapped_f(a, b):
        return f(a, b) + 1
    return wrapped_f

@increment
def plus(a, b)
    """ Add two things together """
    return a + b

plus.__name__  # this returns 'plus'
plus.__doc__   # this returns 'Add two things together'
fromfunctoolsimportwraps
 
defincrement(f):
    """ Increment a function result """
    @wraps(f)
    wrapped_f(a, b):
        return f(a, b) + 1
    return wrapped_f
 
@increment
defplus(a, b)
    """ Add two things together """
    return a + b
 
plus.__name__  # this returns 'plus'
plus.__doc__  # this returns 'Add two things together'

BONUS #3: 使用’decorator’裝飾器

如果仔細看看上述使用裝飾器的方式,在包裝器聲明和返回的地方也有不少重復。

你可以安裝python egg ‘decorator’,其中包含一個提供裝飾器模板的’decorator’裝飾器!

使用easy_install:

$ sudo easy_install decorator
$ sudoeasy_installdecorator

或者Pip:

$ pip install decorator
$ pipinstalldecorator

然后你可以簡單的編寫:

from decorator import decorator

@decorator
def post_only(f, request):
    """ Ensures a method is post only """
    if request.method != "POST":
        response = HttpResponse(json.dumps(
            {"error": "this method only accepts posts!"}))
        response.status_code = 500
        return response
    return f(request)
fromdecoratorimportdecorator
 
@decorator
defpost_only(f, request):
    """ Ensures a method is post only """
    if request.method != "POST":
        response = HttpResponse(json.dumps(
            {"error": "this method only accepts posts!"}))
        response.status_code = 500
        return response
    return f(request)

這個裝飾器更牛逼的一點是保存了__name__和__doc__的返回值,也就是它封裝了

functools.wraps的

功能!

來自: http://python.jobbole.com/84151/

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!