欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python裝飾器在Django框架下去除冗余代碼的教程

 更新時(shí)間:2015年04月16日 17:43:57   投稿:goldensun  
這篇文章主要介紹了使用Python裝飾器在Django框架下去除冗余代碼的教程,主要是處理JSON代碼的一些冗余,需要的朋友可以參考下

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

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

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

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

  •     返回json響應(yīng)
  •     如果是GET請求,那么返回錯(cuò)誤碼

做為一個(gè)注冊api端點(diǎn)例子,我將會像這樣編寫:
 

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

然而這樣我將會在每個(gè)api方法中編寫json響應(yīng)和錯(cuò)誤返回的代碼。這將會導(dǎo)致大量的邏輯重復(fù)。所以讓我們嘗試用裝飾器實(shí)現(xiàn)DRY原則吧。

裝飾器簡介

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

# 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.

現(xiàn)在我們就可以用@符號和這個(gè)裝飾器去裝飾另外一個(gè)函數(shù)了:
 

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

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

對于非post請求返回錯(cuò)誤

現(xiàn)在讓我們在一些更有用的場景下應(yīng)用裝飾器。如果在django中接收的不是POST請求,我們用裝飾器返回一個(gè)錯(cuò)誤響應(yīng)。
 

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

現(xiàn)在我們可以在上述注冊api中應(yīng)用這個(gè)裝飾器:
 

@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

現(xiàn)在我們就有了一個(gè)可以在每個(gè)api方法中重用的裝飾器。

發(fā)送json響應(yīng)

為了發(fā)送json響應(yīng)(同時(shí)處理500狀態(tài)碼),我們可以新建另外一個(gè)裝飾器:
 

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

現(xiàn)在我們就可以在原方法中去除json相關(guān)的代碼,添加一個(gè)裝飾器做為代替:

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) }

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

@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: 參數(shù)化你的請求方法

我曾經(jīng)使用過Tubogears框架,其中請求參數(shù)直接解釋轉(zhuǎn)遞給方法這一點(diǎn)我很喜歡。所以要怎樣在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&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

注意這是一個(gè)參數(shù)化裝飾器的例子。在這個(gè)例子中,函數(shù)的結(jié)果是實(shí)際的裝飾器。

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

@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}

現(xiàn)在我們有了一個(gè)簡潔的、易于理解的api。

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

很不幸,使用裝飾器的一個(gè)副作用是沒有保存方法名(__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'

這將對使用反射的應(yīng)用造成麻煩,比如Sphinx,一個(gè) 自動生成文檔的應(yīng)用。

為了解決這個(gè)問題,我們可以使用'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'裝飾器

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

你可以安裝python egg 'decorator',其中包含一個(gè)提供裝飾器模板的'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)

這個(gè)裝飾器更牛逼的一點(diǎn)是保存了__name__和__doc__的返回值,也就是它封裝了 functools.wraps的功能!

相關(guān)文章

  • python中實(shí)現(xiàn)數(shù)組和列表讀取一列的方法

    python中實(shí)現(xiàn)數(shù)組和列表讀取一列的方法

    下面小編就為大家分享一篇python中實(shí)現(xiàn)數(shù)組和列表讀取一列的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python正則抓取網(wǎng)易新聞的方法示例

    Python正則抓取網(wǎng)易新聞的方法示例

    這篇文章主要介紹了Python正則抓取網(wǎng)易新聞的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python使用正則進(jìn)行網(wǎng)易新聞抓取操作的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下
    2017-04-04
  • PYTHON如何讀取和寫入EXCEL里面的數(shù)據(jù)

    PYTHON如何讀取和寫入EXCEL里面的數(shù)據(jù)

    這篇文章主要介紹了PYTHON如何讀取和寫入EXCEL里面的數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Python灰度變換中的對數(shù)變換專項(xiàng)分析實(shí)現(xiàn)

    Python灰度變換中的對數(shù)變換專項(xiàng)分析實(shí)現(xiàn)

    灰度變換是指根據(jù)某種目標(biāo)條件按一定變換關(guān)系逐點(diǎn)改變源圖像中每個(gè)像素灰度值的方法。目的是改善畫質(zhì),使圖像顯示效果更加清晰。圖像的灰度變換處理是圖像增強(qiáng)處理技術(shù)中的一種非?;A(chǔ)、直接的空間域圖像處理方法,也是圖像數(shù)字化軟件和圖像顯示軟件的一個(gè)重要組成部分
    2022-10-10
  • 深入淺析Python獲取對象信息的函數(shù)type()、isinstance()、dir()

    深入淺析Python獲取對象信息的函數(shù)type()、isinstance()、dir()

    這篇文章主要介紹了Python獲取對象信息的函數(shù)type()、isinstance()、dir()的相關(guān)知識,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • Python繪制數(shù)據(jù)圖表的超詳細(xì)教程

    Python繪制數(shù)據(jù)圖表的超詳細(xì)教程

    畫一個(gè)吸引人注意的圖表相當(dāng)重要,當(dāng)你探索一個(gè)數(shù)據(jù)集,需要畫圖表,圖表看起來令人愉悅是件很高興的事,下面這篇文章主要給大家介紹了關(guān)于Python繪制數(shù)據(jù)圖表的超詳細(xì)教程,需要的朋友可以參考下
    2022-11-11
  • python實(shí)現(xiàn)接口并發(fā)測試腳本

    python實(shí)現(xiàn)接口并發(fā)測試腳本

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)接口并發(fā)測試腳本,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • ansible作為python模塊庫使用的方法實(shí)例

    ansible作為python模塊庫使用的方法實(shí)例

    ansible是一個(gè)python package,是個(gè)完全的unpack and play軟件,對客戶端唯一的要求是有ssh有python,并且裝了python-simplejson包,部署上簡單到發(fā)指。下面這篇文章就給大家主要介紹了ansible作為python模塊庫使用的方法實(shí)例,需要的朋友可以參考借鑒。
    2017-01-01
  • 基于spring boot 日志(logback)報(bào)錯(cuò)的解決方式

    基于spring boot 日志(logback)報(bào)錯(cuò)的解決方式

    今天小編就為大家分享一篇基于spring boot 日志(logback)報(bào)錯(cuò)的解決方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • 2023年最新版Python?3.12.0安裝使用指南(推薦!)

    2023年最新版Python?3.12.0安裝使用指南(推薦!)

    這篇文章主要給大家介紹了關(guān)于2023年最新版Python?3.12.0安裝使用的相關(guān)資料,Python?現(xiàn)在是非常流行的編程語言,當(dāng)然并不是說Python語言性能多么強(qiáng)大,而是Python使用非常方便,特別是現(xiàn)在AI和大數(shù)據(jù)非常流行,用?Python?實(shí)現(xiàn)是非常容易的,需要的朋友可以參考下
    2023-10-10

最新評論