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

 更新时间:2018年03月05日 11:04:31   投稿:mrr  
python的装饰器就是一种代码简洁的手段,在函数和方法有改动时,使得改动量最小。这篇文章给大家介绍了Python 装饰器实现DRY(不重复代码)原则,感兴趣的朋友一起看看吧

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

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

def handle_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

然而这样我将会在每个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.

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

@increment
def plus(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

现在我们可以在上述注册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

现在我们就有了一个可以在每个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

现在我们就可以在原方法中去除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
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中模仿这一特性呢?嗯,装饰器就是一种解决方案!

例如:

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

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

现在我就可以用参数化装饰器编写方法了!我甚至可以选择是否允许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}

现在我们有了一个简洁的、易于理解的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'

这将对使用反射的应用造成麻烦,比如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'

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

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

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

使用easy_install:

$ sudo easy_install decorator

或者Pip:

$ pip install decorator

然后你可以简单的编写:

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的功能!

相关文章

  • 用Python3通过PyCharm上传代码到Git服务器的详细过程

    用Python3通过PyCharm上传代码到Git服务器的详细过程

    上传代码到服务器,如果不知道的情况下还用传统的方式上传很麻烦,现在很多IDE都提供上传代码的功能,例如:VSCode,PyCharm等等,本文讲解的是PyCharm,需要的朋友可以参考下
    2024-03-03
  • Python获取网页数据详解流程

    Python获取网页数据详解流程

    读万卷书不如行万里路,只学书上的理论是远远不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用Python来获取网页的数据,主要应用了Requests库,大家可以在过程中查缺补漏,提升水平
    2021-10-10
  • python sqlobject(mysql)中文乱码解决方法

    python sqlobject(mysql)中文乱码解决方法

    在使用python写项目的时候,用到了sqlobject库函数connectionForURI连接mysql,但是遇到了中文显示乱码的问题,在添加记录的时候还抛出异常
    2008-11-11
  • Python中的Pydantic序列化详解

    Python中的Pydantic序列化详解

    这篇文章主要介绍了Python中的Pydantic序列化详解,Pydantic 是 Python 中一个高性能的数据验证和序列化库,它提供了一个简单而强大的方式来定义结构化的数据,并在应用程序的各个层次中使用这些数据,需要的朋友可以参考下
    2023-10-10
  • 基于Python编写词云软件并显示分词结果

    基于Python编写词云软件并显示分词结果

    这篇文章主要为大家详细介绍了如何基于Python编写一个简单的词云制作软件并显示分词结果,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解一下
    2023-10-10
  • Python设置Word页面纸张方向为横向

    Python设置Word页面纸张方向为横向

    这篇文章主要为大家详细介绍了Python设置Word页面纸张方向为横向的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起
    2024-02-02
  • Python中 * 号的用法总结

    Python中 * 号的用法总结

    Python中的 *号是一个特殊的符号,在其他编程语言中,它最广为人知的用途就是作为乘法运算的符号,本文总结了Python中*号的所有用途,希望对大家有所帮助
    2023-11-11
  • python处理二进制数据的方法

    python处理二进制数据的方法

    这篇文章主要介绍了python处理二进制数据的方法,涉及Python针对二进制数据的相关操作技巧,需要的朋友可以参考下
    2015-06-06
  • 详解Python 3D引擎Ursina如何绘制立体图形

    详解Python 3D引擎Ursina如何绘制立体图形

    Python有一个不错的3D引擎——Ursina。本文就来手把手教你认识Ursina并学会绘制立体图形,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-01-01
  • pytest配置文件pytest.ini的详细使用

    pytest配置文件pytest.ini的详细使用

    这篇文章主要介绍了pytest配置文件pytest.ini的详细使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04

最新评论