通过 Python 装饰器实现DRY(不重复代码)原则

英文原文:DRY Principles through Python Decorators

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

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

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

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

装饰器简介

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

?
1
2
3
4
5
6
7
8
9
# 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.

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

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

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

对于非post请求返回错误

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

?
1
2
3
4
5
6
7
8
9
10
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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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

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

发送json响应

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

?
1
2
3
4
5
6
7
8
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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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) }

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@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"}

 

BONUS: 参数化你的请求方法

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

例如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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&b=cv in request.POST to
    f(a=test, b=cv)
    """
    def wrapper(f):
        def wrapped(request):
            kw = {}
            if "GET" in types:
                for k, v in request.GET.items():
                    kw[k] = v
            if "POST" in types:
                for k, v in request.POST.items():
                    kw[k] = v
            return f(request, **kw)
        return wrapped
    return wrapper

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

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

?
1
2
3
4
5
6
7
8
9
10
@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}

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

 

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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 + 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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 + b
plus.__name__  # this returns 'plus'
plus.__doc__   # this returns 'Add two things together'

 

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

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

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

使用easy_install:

?
1
sudo easy_install decorator

或者Pip:

?
1
$ pip install decorator

然后你可以简单的编写:

?
1
2
3
4
5
6
7
8
9
10
11
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)

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

 
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们

通过 Python 装饰器实现DRY(不重复代码)原则相关推荐

  1. 通过 Python 装饰器实现DRY(不重复代码)原

    通过 Python 装饰器实现DRY(不重复代码)原 Python装饰器是一个消除冗余的强大工具.随着将功能模块化为大小合适的方法,即使是最复杂的工作流,装饰器也能使它变成简洁的功能. 例如让我们看看 ...

  2. Python高级特性: 12步轻松搞定Python装饰器

    12步轻松搞定Python装饰器 通过 Python 装饰器实现DRY(不重复代码)原则:  http://python.jobbole.com/84151/ 基本上一开始很难搞定python的装饰器 ...

  3. Python 装饰器 函数

    Python装饰器学习(九步入门):http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html 浅谈Python装饰器:https://b ...

  4. python 装饰器实践,实现定时函数和失败异常重复调用

    python 装饰器实践,实现定时函数和失败异常重复调用 执行请求或函数,出现异常情况下指定重复执行次数 可以作为一个包调用 方法 get()和post 做请求,execcunt = 指定请求失败再次 ...

  5. 深入浅出 Python 装饰器:16 步轻松搞定 Python 装饰器

    2019独角兽企业重金招聘Python工程师标准>>> Python的装饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的De ...

  6. 一份来自 StackOverflow 的最佳 Python 装饰器教程

    (给Python开发者加星标,提升Python技能) 翻译:可乐,校对:艾凌风 发布:Python开发者(id:PythonCoder) 注意: 这是一篇 StackOverflow 上的问题回答,因 ...

  7. python简单装饰器_简单介绍Python装饰器(一)

    装饰器的作用 相信大家在 探索过程中已经了解装饰器的作用,也有很多花里胡哨的介绍. 这次小冰也来讲解一下关于Python装饰器的一些小知识. 它的作用: 性能测试 日志 安全验证 ...... 相信大 ...

  8. python装饰器-Python 装饰器

    简言之,python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使用python装饰器的好处就是在不用更改原函数的代码前提下给函数增加新的功能. 一般而言, ...

  9. python装饰器作用-Python装饰器的通俗理解

    在学习Python的过程中,我相信有很多人和我一样,对Python的装饰器一直觉得很困惑,我也是困惑了好久,并通过思考和查阅才能略有领悟,我希望以下的内容会对你有帮助,我也努力通过通俗的方式使得对Py ...

最新文章

  1. 太棒啦!PyCharm与Jupyter完美融合,Jupytext来啦!
  2. 问题集锦(16-20)
  3. 56. Netty源代码分析-服务器初始化 NioEventLoopGroup实例化
  4. python 多线程讲解(如何实现多线程,递归锁,互斥锁,信号量,事件等)
  5. 明白了为什么java方法上面为什么要加个@符号
  6. Codeforces Round #299 (Div. 2) D. Tavas and Malekas kmp
  7. c语言Linux用线程创建文件,Linux环境下C语言线程创建---简单代码
  8. 无线网卡dns服务器没有响应,无线网卡上不去网怎么设置dns
  9. 通过Repository Manager 1.3来管理戴尔驱动程序更新
  10. 疫情严峻!有高校不放寒假,直接开始新学期!还有高校紧急放假,停止考试直接回家...
  11. 【转】C# SqlServer操作辅助类(SqlServerHelper.cs)
  12. 游戏出海,技术先行 ——UCloud助力出海业务最佳实践直播公开课
  13. 实训报告html前端开发,web前端开发实习报告比你想象中简单
  14. c语言实型变量允许存放整型数,实型变量允许存放整形数吗
  15. 工程思维:把每件事都当作一个项目来推进
  16. Ansible Tests详解
  17. CH340G软件识别、usb转串口软件识别、测试
  18. https://start.spring.io‘ 的初始化失败请检查 URL、网络和代理设置
  19. 【JS】Proxy(代理)
  20. R语言入门代码(二)for循环的理解

热门文章

  1. 周边pd是什么意思_肿瘤百问百答(六)关于胃癌的PD-1抗体治疗,你应该知道些什么?...
  2. python上传文件到windows_python使用win32gui上传文件
  3. 苹果xsmax是什么接口_为什么苹果PD快充线头是银色而非金黄色?原来那根本就不是镀的银...
  4. 英语对IT从业者的影响
  5. android使用Dialog实现复选功能与数据库结合综合实例
  6. 1054: 猴子吃桃
  7. jQuery 增加 删除 修改select option
  8. 查询mysql所有表的大小
  9. Android 创世纪 第三天
  10. idea 常用配置和快捷键