Python,有条件地使用装饰器(未在所有环境中安装包)

Python, conditional use of a decorator (package not installed in all env)

我正在使用 New Relic 监控我的 Python (2.7) 应用程序,但仅限于我的生产环境。我想使用他们的 function_trace 装饰器。

在wsgi.py

try:
    import newrelic.agent
    newrelic.agent.initialize('/path/')
except Exception as e:
    pass

在views.py

if settings.ENV == "test":
    pass
else:
    import newrelic

@newrelic.agent.function_trace()
def my_function():
    ....

这在生产中效果很好,但在测试中当然失败了。我不能将 if-else 直接放在装饰器周围,所以我想我可以使用条件装饰器:

def newrelic_conditional_decorator(function)
    # What magic goes in here...
    if settings.ENV == "test":
        just return function
    else:
        use @newrelic.agent.function_trace() decorator

@newrelic_conditional_decorator
def my_function():
    ....

我总是对装饰器有点模糊,所以我希望能在这里得到一些帮助! (或者另一种处理测试中与生产中没有相同包的方法。)

装饰器接受一个函数,return从中得到一个新函数。所以如果你想要一个条件装饰器,你只需要 return 当你不想应用装饰器时的初始函数。

您的具体情况

def newrelic_conditional_decorator(function):

    if settings.ENV == "test":
        # We do not apply the decorator to the function
        return function
    else:
        # We apply the decorator and return the new function
        return newrelic.agent.function_trace()(function)

@newrelic_conditional_decorator
def my_function():
    ...

有点笼统

您可以将其抽象化以制作一个更通用的条件装饰器,它应用给定的生成器提供一些 condition 函数 returns True.

def conditional_decorator(decorator, condition, *args):

    def wrapper(function):
        if condition(*args):
            return decorator(function)
        else:
            return function

    return wrapper

@conditional_decorator(
    newrelic.agent.function_trace(),
    lambda settings: settings.ENV != "test",
    settings)
def my_function():
    ...