python装饰器应用实例

Python装饰器是一个消除冗余的强大工具。随着将功能模块化为大小合适的方法,即使是最复杂的工作流,装饰器也能使它变成简洁的功能。

例如让我们看看Django web框架,该框架处理请求的方法接收一个方法对象,返回一个响应对象:

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

我最近遇到一个案例,需要编写几个满足下述条件的api方法:

  • 返回json响应

  • 如果是GET请求,那么返回错误码

做为一个注册api端点例子,我将会像这样编写:

01def register(request):
02    result = None
03    # check for post only
04    if request.method != 'POST':
05        result = {"error""this method only accepts posts!"}
06    else:
07        try:
08            user = User.objects.create_user(request.POST['username'],
09                                            request.POST['email'],
10                                            request.POST['password'])
11            # optional fields
12            for field in ['first_name''last_name']:
13                if field in request.POST:
14                    setattr(user, field, request.POST[field])
15            user.save()
16            result = {"success"True}
17        except KeyError as e:
18            result = {"error"str(e) }
19    response = HttpResponse(json.dumps(result))
20    if "error" in result:
21        response.status_code = 500
22    return response

然而这样我将会在每个api方法中编写json响应和错误返回的代码。这将会导致大量的逻辑重复。所以让我们尝试用装饰器实现DRY原则吧。


装饰器简介

如果你不熟悉装饰器,我可以简单解释一下,实际上装饰器就是有效的函数包装器,python解释器加载函数的时候就会执行包装器,包装器可以修改函数的接收参数和返回值。举例来说,如果我想要总是返回比实际返回值大一的整数结果,我可以这样写装饰器:

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

现在我们就可以用@符号和这个装饰器去装饰另外一个函数了:

1@increment
2def plus(a, b):
3    return + b
4
5result = plus(46)
6assert(result == 11"We wrote our decorator wrong!")

装饰器修改了存在的函数,将装饰器返回的结果赋值给了变量。在这个例子中,'plus'的结果实际指向increment(plus)的结果。


对于非post请求返回错误

现在让我们在一些更有用的场景下应用装饰器。如果在django中接收的不是POST请求,我们用装饰器返回一个错误响应。

01def post_only(f):
02    """ Ensures a method is post only """
03    def wrapped_f(request):
04        if request.method != "POST":
05            response = HttpResponse(json.dumps(
06                {"error""this method only accepts posts!"}))
07            response.status_code = 500
08            return response
09        return f(request)
10    return wrapped_f

现在我们可以在上述注册api中应用这个装饰器:

01@post_only
02def register(request):
03    result = None
04    try:
05        user = User.objects.create_user(request.POST['username'],
06                                        request.POST['email'],
07                                        request.POST['password'])
08        # optional fields
09        for field in ['first_name''last_name']:
10            if field in request.POST:
11                setattr(user, field, request.POST[field])
12        user.save()
13        result = {"success"True}
14    except KeyError as e:
15        result = {"error"str(e) }
16    response = HttpResponse(json.dumps(result))
17    if "error" in result:
18        response.status_code = 500
19    return response

现在我们就有了一个可以在每个api方法中重用的装饰器。



发送json响应

为了发送json响应(同时处理500状态码),我们可以新建另外一个装饰器:

1def json_response(f):
2    """ Return the response as json, and return a 500 error code if an error exists """
3    def wrapped(*args, **kwargs):
4        result = f(*args, **kwargs)
5        response = HttpResponse(json.dumps(result))
6        if type(result) == dict and 'error' in result:
7            response.status_code = 500
8        return response

现在我们就可以在原方法中去除json相关的代码,添加一个装饰器做为代替:

01@post_only
02@json_response
03def register(request):
04    try:
05        user = User.objects.create_user(request.POST['username'],
06                                        request.POST['email'],
07                                        request.POST['password'])
08        # optional fields
09        for field in ['first_name''last_name']:
10            if field in request.POST:
11                setattr(user, field, request.POST[field])
12        user.save()
13        return {"success"True}
14    except KeyError as e:
15        return {"error"str(e) }

现在,如果我需要编写新的方法,那么我就可以使用装饰器做冗余的工作。如果我要写登录方法,我只需要写真正相关的代码:

01@post_only
02@json_response
03def login(request):
04    if request.user is not None:
05        return {"error""User is already authenticated!"}
06    user = auth.authenticate(request.POST['username'], request.POST['password'])
07    if user is not None:
08        if not user.is_active:
09            return {"error""User is inactive"}
10        auth.login(request, user)
11        return {"success"True"id": user.pk}
12    else:
13        return {"error""User does not exist with those credentials"}

BONUS: 参数化你的请求方法

我曾经使用过Tubogears框架,其中请求参数直接解释转递给方法这一点我很喜欢。所以要怎样在Django中模仿这一特性呢?嗯,装饰器就是一种解决方案!

例如:

01def parameterize_request(types=("POST",)):
02    """
03    Parameterize the request instead of parsing the request directly.
04    Only the types specified will be added to the query parameters.
05
06    e.g. convert a=test&b=cv in request.POST to
07    f(a=test, b=cv)
08    """
09    def wrapper(f):
10        def wrapped(request):
11            kw = {}
12            if "GET" in types:
13                for k, v in request.GET.items():
14                    kw[k] = v
15            if "POST" in types:
16                for k, v in request.POST.items():
17                    kw[k] = v
18            return f(request, **kw)
19        return wrapped
20    return wrapper

注意这是一个参数化装饰器的例子。在这个例子中,函数的结果是实际的装饰器。

现在我就可以用参数化装饰器编写方法了!我甚至可以选择是否允许GET和POST,或者仅仅一种请求参数类型。

01@post_only
02@json_response
03@parameterize_request(["POST"])
04def register(request, username, email, password,
05             first_name=None, last_name=None):
06    user = User.objects.create_user(username, email, password)
07    user.first_name=first_name
08    user.last_name=last_name
09    user.save()
10    return {"success"True}

现在我们有了一个简洁的、易于理解的api。


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

很不幸,使用装饰器的一个副作用是没有保存方法名(__name__)和docstring(__doc__)值:

01def increment(f):
02    """ Increment a function result """
03    wrapped_f(a, b):
04        return f(a, b) + 1
05    return wrapped_f
06
07@increment
08def plus(a, b)
09    """ Add two things together """
10    return + b
11
12plus.__name__  # this is now 'wrapped_f' instead of 'plus'
13plus.__doc__   # this now returns 'Increment a function result' instead of 'Add two things together'

这将对使用反射的应用造成麻烦,比如Sphinx,一个 自动生成文档的应用。

为了解决这个问题,我们可以使用'wraps'装饰器附加上名字和docstring:

01from functools import wraps
02
03def increment(f):
04    """ Increment a function result """
05    @wraps(f)
06    wrapped_f(a, b):
07        return f(a, b) + 1
08    return wrapped_f
09
10@increment
11def plus(a, b)
12    """ Add two things together """
13    return + b
14
15plus.__name__  # this returns 'plus'
16plus.__doc__   # this returns 'Add two things together'

BONUS #3: 使用'decorator'装饰器

如果仔细看看上述使用装饰器的方式,在包装器声明和返回的地方也有不少重复。

你可以安装python egg 'decorator',其中包含一个提供装饰器模板的'decorator'装饰器!

使用easy_install:

1sudo easy_install decorator

或者Pip:

1$ pip install decorator

然后你可以简单的编写:

view sourceprint?

01from decorator import decorator
02
03@decorator
04def post_only(f, request):
05    """ Ensures a method is post only """
06    if request.method != "POST":
07        response = HttpResponse(json.dumps(
08            {"error""this method only accepts posts!"}))
09        response.status_code = 500
10        return response
11    return f(request)

这个装饰器更牛逼的一点是保存了__name__和__doc__的返回值,也就是它封装了 functools.wraps的功能!


转载于:https://my.oschina.net/shniu/blog/215402

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值