pydeco.MethodsDecorator

class pydeco.MethodsDecorator(mapping={})[source]

Class that enables to decorate specific methods with given decorator.

Parameters:
mapping : dict

Mapping containing decorators as key and methods (as str or a list of str) as values (ex: {Timer(): ['fit', 'predict']})

Examples

We assume here that a decorator named Timer (used to compute running time of called functions) is already implemented. The below code applies such a decorator to the method method_1() of class MyClass so as to compute running time every time method_1() is called.

>>> @MethodsDecorator(mapping={Timer(): 'method_1'})
>>> class MyClass():
>>>
>>>    def method_1(self, *args, **kwargs):
>>>       return

The “@” syntax is a proxy for the below implementation:

>>> class MyClass():
>>>
>>>    def method_1(self, *args, **kwargs):
>>>       return
>>>
>>> MyClass = MethodsDecorator(mapping={Timer(): 'method_1'})(MyClass)

See the examples section for more insights on how to use MethodsDecorator.

Methods

__call__(self, cls) Return wrapped input class with decorated methods.