Skip to main content

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.

Project details


Download files

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

Source Distribution

DecoratorTools-1.8.zip (29.6 kB view details)

Uploaded Source

File details

Details for the file DecoratorTools-1.8.zip.

File metadata

  • Download URL: DecoratorTools-1.8.zip
  • Upload date:
  • Size: 29.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for DecoratorTools-1.8.zip
Algorithm Hash digest
SHA256 da73f03a9fab80a205c8febcecee83f8a989eab8ce81affd7989210e4a3d9ea8
MD5 f161004115c3d04ed976c230c8a91d87
BLAKE2b-256 86788de8bc5f64c125d17be429c5da33344bf1690f745f121737d5b6fbfe828c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page