I am working on finding a way to reduce boilerplate decorators. We have a lot of classes that use a @decorate. For example:
class MyClass(Base):
@decorate
def fun1(self):
pass
@decorate
def fun2(self):
pass
def fun3(self):
pass
I want to make it so by default the decorator is there, unless someone specifies otherwise.
I use this code to do the autowrap
from functools import wraps
def myDecorator(func):
@wraps(func)
def decorator(self, *args, **kwargs):
try:
print 'enter'
ret = func(self, *args, **kwargs)
print 'leave'
except:
print 'exception'
ret = None
return ret
return decorator
class TestDecorateAllMeta(type):
def __new__(cls, name, bases, local):
for attr in local:
value = local[attr]
if callable(value):
local[attr] = myDecorator(value)
return type.__new__(cls, name, bases, local)
class TestClass(object):
__metaclass__ = TestDecorateAllMeta
def test_print2(self, val):
print val
def test_print(self, val):
print val
c = TestClass()
c.test_print1("print 1")
c.test_print2("print 2")
My question are:
- Is there a better way to accompish auto-decorating?
- How can I go about overriding?
Ideally my end solution would be something like:
class TestClass(object):
__metaclass__ = TestDecorateAllMeta
def autowrap(self):
print("Auto wrap")
@dont_decorate
def test_dont_decorate(self, val):
print val
Edit
To speak to one of the comments below, since classess are callable instead of doing
if callable(value):
It should read:
if isinstance(value,types.FunctionType)