How to decorate a parent class and make it use it? python

I want to create a class decorator to decorate a class and work with subclasses.

Imagine this class:

class CustomBaseTest(TransactionTestCase):
    def __init__(self, *args, **kwargs):
        ...

    def more_custom_helpers(self):
        ...

and the real test:

class FooTest(CustomBaseTest):
    def decorate_this_foo_is_ok(self):
        ....

    def decorate_this_fails(self):
        ...

I want to use a decorator in CustomBaseTestwhich finds all methods starting with 'decoratte_this_' and executes custom code after and before. I already have a decorator, something like this:

def class_decorator(klass):
    is_method_test = lambda m: not m.startswith('_') and m.startswith('decorate_this_') and isinstance(getattr(klass, m), MethodType)
    test_methods = filter(is_method_test, dir(klass))

    for method_name in test_methods:
        class_method = getattr(klass, method_name)

        def helper(mname, method):
            @wraps(method)
            ... some logic here
            retval = method(*a, **kw)
            ... more logic here
            return retval
        return wrapper

        fn = MethodType(helper(method_name, class_method), None, klass)
        setattr(klass, method_name, fn)
    return klass

Do you know if this can be done? And How?

thank!!!

+3
source share
1 answer

Thanks @Markku and @BrenBarn.

Here is the solution.

First we have a simple decorator:

from functools import wraps
def my_decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        # do some stuff
        retval = func(*args, **kwargs)
        # do more stuff
        return retval
    return wrapper

and metaclass:

class ProfileMetaByClass(type):
    def __init__(cls, name, bases, dct):
        for method_name, method in dct.items():
            if method_name.startswith('decorate_this_'):
                setattr(cls, key, my_decorator(value))
        type.__init__(cls, name, bases, dct)

and it worked for me !! I apologize in advance if I mourn.

+1
source

All Articles