Package pypat :: Package structural :: Module decorator
[hide private]
[frames] | no frames]

Source Code for Module pypat.structural.decorator

 1  from functools import partial 
 2  from abc import ABCMeta, abstractmethod 
3 4 5 -class Decorator(object, metaclass=ABCMeta):
6 """ 7 Base Decorator class that all decorator classes inherit from. 8 9 - External Usage Documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Structural-Pattern-Usage} 10 - External Decorator Pattern documentation: U{https://en.wikipedia.org/wiki/Decorator_pattern} 11 """
12 - def __get__(self, instance, owner):
13 """ 14 Override __get__ in order to get the instance of a bound of method call. 15 """ 16 return partial(self.__call__, instance)
17 18 @abstractmethod
19 - def __call__(self, *args, **kwargs):
20 """ 21 All decorators must implement a __call__ method. 22 """ 23 pass
24
25 26 -class DecoratorSimple(Decorator, metaclass=ABCMeta):
27 """ 28 A Base Decorator class for decorators with no arguments. 29 30 - External Usage Documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Structural-Pattern-Usage} 31 - External Decorator Pattern documentation: U{https://en.wikipedia.org/wiki/Decorator_pattern} 32 """
33 - def __init__(self, func):
34 """ 35 Initialize a new DecoratorSimple instance. 36 37 @param func: The function being decorated. 38 """ 39 self.func = func
40
41 42 -class DecoratorComplex(Decorator, metaclass=ABCMeta):
43 """ 44 A Base Decorator class for decorators with arguments. 45 46 - External Usage Documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Structural-Pattern-Usage} 47 - External Decorator Pattern documentation: U{https://en.wikipedia.org/wiki/Decorator_pattern} 48 """ 49 @abstractmethod
50 - def __init__(self, *args, **kwargs):
51 """ 52 Initialize a new DecoratorComplex instance. 53 54 @param args: Args for the decorator. 55 @param kwargs: Keyword args for the decorator. 56 """ 57 pass
58 59 @abstractmethod
60 - def __call__(self, func, *args, **kwargs):
61 """ 62 Concrete DecoratorComplex instances must override the __call__ method. 63 64 @param func: The function being decorated. 65 @param args: Arguments for the decorated function. 66 @param kwargs: Keyword arguments for the decorated function. 67 @return: 68 """ 69 pass
70
71 72 -class CallWrapper(DecoratorSimple):
73 """ 74 A Decorator for wrapping DecoratorComplex __call__ methods. 75 76 - External Usage Documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Structural-Pattern-Usage} 77 - External Decorator Pattern documentation: U{https://en.wikipedia.org/wiki/Decorator_pattern} 78 """
79 - def __call__(self, instance, func):
80 """ 81 Wrap a concrete DecoratorComplex __call__ method. 82 """ 83 def wrapped(*args, **kwargs): 84 return self.func(instance, func, *args, **kwargs)
85 86 return wrapped
87