Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Class, function, and metaclass decorators -- even in Python 2.3 (now with source debugging for generated code)!

Project Description

Want to use decorators, but still need to support Python 2.3? Wish you could have class decorators, decorate arbitrary assignments, or match decorated function signatures to their original functions? Want to get metaclass features without creating metaclasses? How about synchronized methods?

“DecoratorTools” gets you all of this and more. Some quick examples:

# Method decorator example
from peak.util.decorators import decorate

class Demo1(object):
    decorate(classmethod)   # equivalent to @classmethod
    def example(cls):
        print "hello from", cls


# Class decorator example
from peak.util.decorators import decorate_class

def my_class_decorator():
    def decorator(cls):
        print "decorating", cls
        return cls
    decorate_class(decorator)

class Demo2:
    my_class_decorator()

# "decorating <class Demo2>" will be printed when execution gets here

Installing DecoratorTools (using "easy_install DecoratorTools" or "setup.py install") gives you access to the peak.util.decorators module. The tools in this module have been bundled for years inside of PEAK, PyProtocols, RuleDispatch, and the zope.interface package, so they have been widely used and tested. (Unit tests are also included, of course.)

This standalone version is backward-compatible with the bundled versions, so you can mix and match decorators from this package with those provided by zope.interface, TurboGears, etc.

For complete documentation, see the DecoratorTools manual.

Changes since version 1.7:

  • The @template_function decorator now supports using a return value instead of a docstring, in order to work with the “-OO” option to Python; it’s highly recommended that you update your template functions to use a return value instead of a docstring. (The error message has also been improved for the missing docstring case.)
  • Fixed metaclass collisions in classy subclasses that mix in abstract classes (e.g. collections.Sequence) in Python 2.6+.

Changes since version 1.6:

  • Added synchronized decorator to support locking objects during method execution.

Changes since version 1.5:

  • Added classy base class that allows you to do the most often-needed metaclass behviors without needing an actual metaclass.

Changes since version 1.4:

  • Added enclosing_frame() function, so that complex decorators that call DecoratorTools functions while being called by DecoratorTools functions, will work correctly.

Changes since version 1.3:

  • Added support for debugging generated code, including the code generated by rewrap() and template_function.

Changes since version 1.2:

  • Added rewrap() function and template_function decorator to support signature matching for decorated functions. (These features are similar to the ones provided by Michele Simionato’s “decorator” package, but do not require Python 2.4 and don’t change the standard idioms for creating decorator functions.)
  • decorate_class() will no longer apply duplicate class decorator callbacks unless the allow_duplicates argument is true.

Changes since version 1.1:

  • Fixed a problem where instances of different struct types could equal each other

Changes since version 1.0:

  • The struct() decorator makes it easy to create tuple-like data structure types, by decorating a constructor function.
Release History

Release History

This version
History Node

1.8

History Node

1.7

History Node

1.6

History Node

1.5

History Node

1.4

History Node

1.3

History Node

1.2

History Node

1.1

History Node

1.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
DecoratorTools-1.8.zip (29.6 kB) Copy SHA256 Checksum SHA256 Source May 6, 2010

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting