多个python装饰器(multiple python decorators)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
多个python装饰器(multiple python decorators)

我有一个带有装饰方法(my_method)的Django模型(MyModel)。 我希望装饰器对my_method执行一些检查:

如果检查成功,my_method应该返回一个字符串;

如果检查不成功,my_method应该返回装饰器返回的失败消息。

逻辑如下:

# models.py class MyModel(models.Model): @decorator1 @decorator2 def my_method(self, request, *args, **kwargs): return u'The result that must be returned if all the checks performed by the decorator succeed' # decorators.py from functools import wraps # decorator1 checks if certain conditions are met. If yes, it returns the decorated method (method_to_decorate); if not, it returns a tuple def decorator1(method_to_decorate): @wraps(method_to_decorate) def wrapper1(self, request, *args, **kwargs): if a_condition : return method_to_decorate(self, request, *args, **kwargs) else: # we return a failure message return ('failure', 'message') return wrapper1 # in decorator2, we want to know if the check performed by decorator1 was successful # in case of success, we perform decorator2's check # in case of failure, decorator2 should simply pass the failure message returned by decorator1 def decorator2(method_to_decorate): @wraps(method_to_decorate) def wrapper2(self, request, *args, **kwargs): # if the first decorator succeeded if decorator1_s_test_was_successful: # we check if the conditions of the second decorator are met if decorator2_s_test_was_successful: # we return the method return method_to_decorate(self, request, *args, **kwargs) else: # we return a failure message return ('another failure', 'message') # if the first decorator did not succeed else: # decorator1 returned a tuple : ('failure', 'message') return the_failure_that_decorator1_returned return wrapper2

因此,如果decorator1返回失败,我希望an_instance_of_my_model_instance.my_method(request)返回('failure','message')。 如果decorator1成功但不是decorator2,我会期望('另一个失败','消息')。 如果所有测试都通过了,那么如果装饰器执行的所有检查都成功,则必须返回结果

如果decorator1的检查成功通过,我不知道如何检查decorator2。 我试图通过检查decorator2中method_to_decorate的type()来做到这一点,但似乎该类型使用原始方法本身,而不是decorator1返回的结果(好像装饰器不知道前面装饰器执行的操作) 。

先谢谢你!

I have a Django Model (MyModel) with a decorated method (my_method). I expect the decorators to perform some checks on my_method:

if the checks succeed, my_method should return a string;

if the checks do not succeed, my_method should return the failure messages returned by the decorators.

The logic is the following:

# models.py class MyModel(models.Model): @decorator1 @decorator2 def my_method(self, request, *args, **kwargs): return u'The result that must be returned if all the checks performed by the decorator succeed' # decorators.py from functools import wraps # decorator1 checks if certain conditions are met. If yes, it returns the decorated method (method_to_decorate); if not, it returns a tuple def decorator1(method_to_decorate): @wraps(method_to_decorate) def wrapper1(self, request, *args, **kwargs): if a_condition : return method_to_decorate(self, request, *args, **kwargs) else: # we return a failure message return ('failure', 'message') return wrapper1 # in decorator2, we want to know if the check performed by decorator1 was successful # in case of success, we perform decorator2's check # in case of failure, decorator2 should simply pass the failure message returned by decorator1 def decorator2(method_to_decorate): @wraps(method_to_decorate) def wrapper2(self, request, *args, **kwargs): # if the first decorator succeeded if decorator1_s_test_was_successful: # we check if the conditions of the second decorator are met if decorator2_s_test_was_successful: # we return the method return method_to_decorate(self, request, *args, **kwargs) else: # we return a failure message return ('another failure', 'message') # if the first decorator did not succeed else: # decorator1 returned a tuple : ('failure', 'message') return the_failure_that_decorator1_returned return wrapper2

So, if decorator1 returns a failure, I expect an_instance_of_my_model_instance.my_method(request) to return ('failure', 'message'). If decorator1 succeeded but not decorator2, I would expect ('another failure', 'message'). And if all the test were passed, u'The result that must be returned if all the checks performed by the decorator succeed'

I do not know how to check in decorator2 if decorator1's checks were successfully passed. I tried to do it by checking the type() of the method_to_decorate in decorator2, but it seems that type uses the original method itself, not its result as returned by decorator1 (as if decorators did not know about the operations performed by previous decorators).

Thank you in advance!

最满意答案

如果你想让decorator2检查返回的@decorator1 ,你需要交换 @decorator1和@decorator2行:

@decorator2 @decorator1 def my_method(self, request, *args, **kwargs): return u'The result that must be returned if all the checks performed by the decorator succeed'

现在decorator2将包装decorator1返回的任何方法,因此您可以检查该方法返回的内容。

def decorator2(method_to_decorate): @wraps(method_to_decorate) def wrapper2(self, request, *args, **kwargs): result = method_to_decorate(self, request, *args, **kwargs) if isinstance(result, tuple) and result and result[0] == 'failure': # decorator1 returned a failure return result else: # decorator1 passed through the wrapped method call if decorator2_s_test_was_successful: return result else: return ('another failure', 'message') return wrapper2

You'll need to swap the @decorator1 and @decorator2 lines if you want decorator2 to check up on whatever decorator1 returned:

@decorator2 @decorator1 def my_method(self, request, *args, **kwargs): return u'The result that must be returned if all the checks performed by the decorator succeed'

Now decorator2 will wrap whatever method decorator1 returned, and you can thus inspect what that method returns.

def decorator2(method_to_decorate): @wraps(method_to_decorate) def wrapper2(self, request, *args, **kwargs): result = method_to_decorate(self, request, *args, **kwargs) if isinstance(result, tuple) and result and result[0] == 'failure': # decorator1 returned a failure return result else: # decorator1 passed through the wrapped method call if decorator2_s_test_was_successful: return result else: return ('another failure', 'message') return wrapper2

更多推荐

本文发布于:2023-04-17 08:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/e7529c0d8bca6be7c90e3f86a5010fc3.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   python   decorators   multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!