这篇文章主要介绍了使用Python装饰器在Django框架下去除冗余代码的教程,主要是处理JSON代码的一些冗余,需要的朋友可
Python是一个消除冗余的强大工具。随着将功能模块化为大小合适的方法,即使是最复杂的工作流,装饰器也能使它变成简洁的功能。
例如让我们看看Django web框架,该框架处理请求的方法接收一个方法对象,返回一个响应对象:
def handle_request(request): return HttpResponse("Hello, World")
我最近遇到一个案例,需要编写几个满足下述条件的api方法:
做为一个注册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) }
现在,如果我需要编写新的方法,那么我就可以使用装饰器做冗余的工作。如果我要写登录方法,我只需要写真正相关的代码:
知优网 » 使用Python装饰器在Django框架下去除冗余代码的教程