Metaclass which decorate all the class methods using two different decorator implementation
Asked Answered
M

1

7

I have a problem with the implementation of a decorator applied at this metaclass decorator I wrote:

def decorateAll(decorator):
    class MetaClassDecorator(type):

        def __new__(meta, classname, supers, classdict):
            for name, elem in classdict.items():
                if type(elem) is FunctionType:
                    classdict[name] = decorator(classdict[name])
            return type.__new__(meta, classname, supers, classdict)
    return MetaClassDecorator

This is the class in which I have used the metaclass:

class Account(object, metaclass=decorateAll(Counter)):

    def __init__(self, initial_amount):
        self.amount = initial_amount

    def withdraw(self, towithdraw):
        self.amount -= towithdraw

    def deposit(self, todeposit):
        self.amount += todeposit

    def balance(self):
        return self.amount

Everything seems to work great when I pass to the decorator metaclass a decorator implemented like this:

def Counter(fun):
    fun.count = 0
    def wrapper(*args):
        fun.count += 1
        print("{0} Executed {1} times".format(fun.__name__, fun.count))
        return fun(*args)
    return wrapper

But when I use a decorator implemented in this way:

class Counter():

    def __init__(self, fun):
        self.fun = fun
        self.count = 0

    def __call__(self, *args, **kwargs):
        print("args:", self, *args, **kwargs)
        self.count += 1
        print("{0} Executed {1} times".format(self.fun.__name__, self.count))
        return self.fun(*args, **kwargs)

I got this error:

line 32, in __call__
return self.fun(*args, **kwargs)
TypeError: __init__() missing 1 required positional argument: 'initial_amount'

Why? Using the two decorators implementation with others function doesn't show me problems. I think that the problem is tied to the fact that the methods to which I'm trying to decorate are class methods. Am I missing something?

Mechanistic answered 22/1, 2016 at 16:22 Comment(0)
L
6

You need to implement Counter as a callable descriptor. When __get__ is executed on the descriptor, you simulate binding the descriptor to the instance which is passed to it. Plus storing the count on a per method/object basis.

This code:

import collections
import functools
import types


def decorateAll(decorator):
    class MetaClassDecorator(type):

        def __new__(meta, classname, supers, classdict):
            for name, elem in classdict.items():
                if type(elem) is types.FunctionType:
                    classdict[name] = decorator(classdict[name])
            return type.__new__(meta, classname, supers, classdict)
    return MetaClassDecorator


class Counter(object):
    def __init__(self, fun):
        self.fun = fun
        self.cache = {None: self}
        self.count = collections.defaultdict(int)

    def __get__(self, obj, cls=None):
        if obj is None:
            return self

        try:
            return self.cache[obj]
        except KeyError:
            pass

        print('Binding {} and {}'.format(self.fun, obj))
        cex = self.cache[obj] = functools.partial(self.__call__, obj)
        return cex

    def __call__(self, obj, *args, **kwargs):
        print("args:", obj, *args, **kwargs)
        self.count[obj] += 1
        print("{0} Exec {1} times".format(self.fun.__name__, self.count[obj]))
        return self.fun(obj, *args, **kwargs)


class Account(object, metaclass=decorateAll(Counter)):

    def __init__(self, initial_amount):
        self.amount = initial_amount

    def withdraw(self, towithdraw):
        self.amount -= towithdraw

    def deposit(self, todeposit):
        self.amount += todeposit

    def balance(self):
        return self.amount


a = Account(33.5)

print(a.balance())

Produces the following output:

Binding <function Account.__init__ at 0x000002250BCD8B70> and <__main__.Account object at 0x000002250BCE8BE0>
args: <__main__.Account object at 0x000002250BCE8BE0> 33.5
__init__ Exec 1 times
Binding <function Account.balance at 0x000002250BCD8D90> and <__main__.Account object at 0x000002250BCE8BE0>
args: <__main__.Account object at 0x000002250BCE8BE0>
balance Exec 1 times
33.5

Which calls the __call__ method of the descriptor, stores the count on a per method by simulating a binding creating an object of type functools.partial.

Lisabethlisan answered 22/1, 2016 at 16:46 Comment(2)
when do you think is more convenient the use of this approach?Mechanistic
In your case it's appropriate because you are decorating regular methods (those taking self as first parameter, i.e: they are bound to the instance at instantiation time). An alternative is to return another object (rather than functools.partial) which contains a reference to the instance (obj) and is linked to the descriptorLisabethlisan

© 2022 - 2024 — McMap. All rights reserved.