Toggling Decorators
Solution 1:
You could add the conditional to the decorator itself:
defbenchmark(func):
ifnot <config.use_benchmark>:
return func
defdecorator():
# fancy benchmarking return decorator
Solution 2:
I've been using the following approach. It's almost identical to the one suggested by CaptainMurphy, but it has the advantage that you don't need to call the decorator like a function.
import functools
classSwitchedDecorator:
def__init__(self, enabled_func):
self._enabled = False
self._enabled_func = enabled_func
@propertydefenabled(self):
return self._enabled
@enabled.setterdefenabled(self, new_value):
ifnotisinstance(new_value, bool):
raise ValueError("enabled can only be set to a boolean value")
self._enabled = new_value
def__call__(self, target):
if self._enabled:
return self._enabled_func(target)
return target
defdeco_func(target):
"""This is the actual decorator function. It's written just like any other decorator."""defg(*args,**kwargs):
print("your function has been wrapped")
return target(*args,**kwargs)
functools.update_wrapper(g, target)
return g
# This is where we wrap our decorator in the SwitchedDecorator class.
my_decorator = SwitchedDecorator(deco_func)
# Now my_decorator functions just like the deco_func decorator,# EXCEPT that we can turn it on and off.
my_decorator.enabled=True@my_decoratordefexample1():
print("example1 function")
# we'll now disable my_decorator. Any subsequent uses will not# actually decorate the target function.
my_decorator.enabled=False@my_decoratordefexample2():
print("example2 function")
In the above, example1 will be decorated, and example2 will NOT be decorated. When I have to enable or disable decorators by module, I just have a function that makes a new SwitchedDecorator whenever I need a different copy.
Solution 3:
I think you should use a decorator a to decorate the decorator b, which let you switch the decorator b on or off with the help of a decision function.
This sounds complex, but the idea is rather simple.
So let's say you have a decorator logger:
from functools import wraps
deflogger(f):
@wraps(f)definnerdecorator(*args, **kwargs):
print (args, kwargs)
res = f(*args, **kwargs)
print res
return res
return innerdecorator
This is a very boring decorator and I have a dozen or so of these, cachers, loggers, things which inject stuff, benchmarking etc. I could easily extend it with an if statement, but this seems to be a bad choice; because then I have to change a dozen of decorators, which is not fun at all.
So what to do? Let's step one level higher. Say we have a decorator, which can decorate a decorator? This decorator would look like this:
@point_cut_decorator(logger)defmy_oddly_behaving_function
This decorator accepts logger, which is not a very interesting fact. But it also has enough power to choose if the logger should be applied or not to my_oddly_behaving_function. I called it point_cut_decorator, because it has some aspects of aspect oriented programming. A point cut is a set of locations, where some code (advice) has to be interwoven with the execution flow. The definitions of point cuts is usually in one place. This technique seems to be very similar.
How can we implement it decision logic. Well I have chosen to make a function, which accepts the decoratee, the decorator, file and name, which can only say if a decorator should be applied or not. These are the coordinates, which are good enough to pinpoint the location very precisely.
This is the implementation of point_cut_decorator, I have chosen to implement the decision function as a simple function, you could extend it to let it decide from your settings or configuration, if you use regexes for all 4 coordinates, you will end up with something very powerful:
from functools import wraps
myselector is the decision function, on true a decorator is applied on false it is not applied. Parameters are the filename, the module name, the decorated object and finally the decorator. This allows us to switch of behaviour in a fine grained manner.
defmyselector(fname, name, decoratee, decorator):
print fname
if decoratee.__name__ == "test"and fname == "decorated.py"and decorator.__name__ == "logger":
returnTruereturnFalse
This decorates a function, checks myselector and if myselector says go on, it will apply the decorator to the function.
defpoint_cut_decorator(d):
definnerdecorator(f):
@wraps(f)defwrapper(*args, **kwargs):
if myselector(__file__, __name__, f, d):
ps = d(f)
return ps(*args, **kwargs)
else:
return f(*args, **kwargs)
return wrapper
return innerdecorator
deflogger(f):
@wraps(f)definnerdecorator(*args, **kwargs):
print (args, kwargs)
res = f(*args, **kwargs)
print res
return res
return innerdecorator
And this is how you use it:
@point_cut_decorator(logger)deftest(a):
print"hello"return"world"
test(1)
EDIT:
This is the regular expression approach I talked about:
from functools import wraps
import re
As you can see, I can specify somewhere a couple of rules, which decides a decorator should be applied or not:
rules = [{
"file": "decorated.py",
"module": ".*",
"decoratee": ".*test.*",
"decorator": "logger"
}]
Then I loop over all rules and return True if a rule matches or false if a rule doesn't matches. By making rules empty in production, this will not slow down your application too much:
def myselector(fname, name, decoratee, decorator):
for rule in rules:
file_rule, module_rule, decoratee_rule, decorator_rule = rule["file"], rule["module"], rule["decoratee"], rule["decorator"]
if (
re.match(file_rule, fname)
and re.match(module_rule, name)
and re.match(decoratee_rule, decoratee.__name__)
and re.match(decorator_rule, decorator.__name__)
):
return True
return False
Solution 4:
Here is what I finally came up with for per-module toggling. It uses @nneonneo's suggestion as a starting point.
Random modules use decorators as normal, no knowledge of toggling.
foopkg.py:
from toggledeco import benchmark
@benchmarkdeffoo():
print("function in foopkg")
barpkg.py:
from toggledeco import benchmark
@benchmarkdefbar():
print("function in barpkg")
The decorator module itself maintains a set of function references for all decorators that have been disabled, and each decorator checks for its existence in this set. If so, it just returns the raw function (no decorator). By default the set is empty (everything enabled).
toggledeco.py:
import functools
_disabled = set()
defdisable(func):
_disabled.add(func)
defenable(func):
_disabled.discard(func)
defbenchmark(func):
if benchmark in _disabled:
return func
@functools.wraps(func)defdeco(*args,**kwargs):
print("--> benchmarking %s(%s,%s)" % (func.__name__,args,kwargs))
ret = func(*args,**kwargs)
print("<-- done")
return deco
The main program can toggle individual decorators on and off during imports:
from toggledeco import benchmark, disable, enabledisable(benchmark) # no benchmarks...
import foopkg
enable(benchmark) # until they are enabled again
import barpkg
foopkg.foo() # no benchmarking
barpkg.bar() # yes benchmarking
reload(foopkg)
foopkg.foo() # now with benchmarking
Output:
functionin foopkg
--> benchmarking bar((),{})functionin barpkg
<-- done--> benchmarking foo((),{})functionin foopkg
<-- done
This has the added bug/feature that enabling/disabling will trickle down to any submodules imported from modules imported in the main function.
EDIT:
Here's class suggested by @nneonneo. In order to use it, the decorator must be called as a function ( @benchmark()
, not @benchmark
).
classbenchmark:
disabled = False @classmethoddefenable(cls):
cls.disabled = False @classmethoddefdisable(cls):
cls.disabled = Truedef__call__(cls,func):
if cls.disabled:
return func
@functools.wraps(func)defdeco(*args,**kwargs):
print("--> benchmarking %s(%s,%s)" % (func.__name__,args,kwargs))
ret = func(*args,**kwargs)
print("<-- done")
return deco
Solution 5:
I would implement a check for a config file inside the decorator's body. If benchmark has to be used according to the config file, then I would go to your current decorator's body. If not, I would return the function and do nothing more. Something in this flavor:
# deco.pydefbenchmark(func):
if config == 'dontUseDecorators': # no use of decorator# do nothingreturn func
defdecorator(): # else call decorator# fancy benchmarking return decorator
What happens when calling a decorated function ? @
in
@benchmarkdeff():
# body comes here
is syntactic sugar for this
f = benchmark(f)
so if config wants you to overlook decorator, you are just doing f = f()
which is what you expect.
Post a Comment for "Toggling Decorators"