Python eXtraordinary Decorators
Project description
Decorators in Python are limited to action when the function they wrap is actually called. This package adds the ability to define decorators to be called at any of the three points of the function’s existence: when the function is defined, when an instance of the class containing the function is created, and when the function is itself called.
This enables the use of decorators to, e.g., tag deprecated methods and pull up a report; register instance methods as listeners to events; manipulate methods at class creation without metaclasses; and so on.
pyxdeco includes decorator base classes:
>>> from pyxdeco import ClassLevelDecorator >>> from pyxdeco import InstanceLevelDecorator >>> from pyxdeco import MethodLevelDecorator
and factory decorators for easy creation of decorators from functions:
>>> from pyxdeco import class_level_decorator >>> from pyxdeco import instance_level_decorator >>> from pyxdeco import method_level_decorator
Create a decorator by subclassing any of the base classes and overriding the decorate method. Alternatively, decorate a function with the factory decorator and the function will be used as the decorate method for the decorator.
Class-Level Decorators
Class-level decorators are invoked when the class is created. They are called with the decorated method and the class as arguments:
>>> deprecated_methods = [] >>> >>> @class_level_decorator ... def deprecated(func, cls): ... deprecated_methods.append(func.__name__) ... >>> class A(object): ... ... def not_deprecated(self): ... pass ... ... @deprecated ... def deprecated_method(self): ... pass ... ... @deprecated ... def also_deprecated_method(self): ... pass ... >>> deprecated_methods ['deprecated_method', 'also_deprecated_method']
Instance-Level Decorators
Instance-level decorators are invoked when an instance of the class containing the decorated method is created (that is, after __init__). The are called with the decorated instance method, the instance, and the class as arguments:
>>> click_listeners = [] >>> >>> @instance_level_decorator ... def onclick(func, inst, cls): ... click_listeners.append('%s.%s' % (inst.id, func.__name__)) ... >>> class A(object): ... def __init__(self, id): ... self.id = id ... ... @onclick ... def click_listener(self): ... pass ... >>> len(click_listeners) == 0 True >>> >>> ob1 = A('ob1') >>> click_listeners ['ob1.click_listener'] >>> >>> ob2 = A('ob2') >>> click_listeners ['ob1.click_listener', 'ob2.click_listener']
Method-Level Decorators
Method-level decorators are no different from ordinary decorators but are provided for the sake of completeness and flexibility. They are passed the decorated function (or instance method) and any arguments:
>>> called = [] >>> >>> @method_level_decorator ... def noticeme(func, *args, **kwargs): ... called.append(func.__name__) ... >>> class A(object): ... @noticeme ... def amethod(self): ... pass ... >>> a = A() >>> len(called) == 0 True >>> >>> a.amethod() >>> called ['amethod'] >>> >>> a.amethod() >>> called ['amethod', 'amethod']
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file pyxdeco-0.1.1.tar.gz
.
File metadata
- Download URL: pyxdeco-0.1.1.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | feb72a12d71bd6ce5b5a5a997d45c72b5dfc630ac0cf596988fc93c8bad00abb |
|
MD5 | a66a7a60dab9d9a64f5dc435063eb326 |
|
BLAKE2b-256 | fc0984dec755cf609fe3f8b667b133bf326f793fbb382e8daeb66c1ae02bb53e |
File details
Details for the file pyxdeco-0.1.1-py2.7.egg
.
File metadata
- Download URL: pyxdeco-0.1.1-py2.7.egg
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b409942e60f8863fa40781312a0cc9909fd5dbb89e1014ec52411667c01ac25 |
|
MD5 | 4f303a395b64758002a59c2c53eb30dc |
|
BLAKE2b-256 | ade0a399dddc8005149cdd2f6a38ca259ef79145bd61d8541f68e7d2b2cad04c |
File details
Details for the file pyxdeco-0.1.1-py2.6.egg
.
File metadata
- Download URL: pyxdeco-0.1.1-py2.6.egg
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b26637836f332f18741b83520c36be72c5247b74fc4b06d785fe2cf8409a675 |
|
MD5 | ffabc151d5384a1c21352bbfa9fd5715 |
|
BLAKE2b-256 | 8ff3aa76ffb209f59108350a5268570c2f5e1b30281f8aa9dc4796226a3b68dc |