The decorator module is a library written with the purpose of
simplifying your life with decorators. It is now more than three years
old and it is used in many Python frameworks (more than
I know of). Release 2.3 adds support for writing decorator factories
with a minimal effort (the feature was requested by Matthew Wilson).
Every class with a .call method with signature
call(self, func, *args, **kw) can be converted into
a decorator factory. Here is a simple example of usage:

from decorator import decorator

class Logging(object):
   "A decorator factory adding logging support to functions"
   def __init__(self, fname):
      self.logfile = file(fname, 'a')
   def call(self, f, *a, **k):
      self.logfile.write('Calling %s with args %s, %s\n' %
(f.__name__, a, k))
      return f(*a, **k)

Logging = decorator(Logging) # add a suitable __call__ method to the
class

@Logging('x.log')
def example():
   pass

example() # "Calling example with args (), {}" will be logged on x.log

If you are already using Python 2.6 you can use the class decorator
syntax, of course.

The main advantage of the library is that decorators implemented
using it are signature-preserving, which is not the case for naive
decorators. The full documentation is here:

http://www.phyast.pitt.edu/~micheles/python/documentation.html

You can download the archive

http://www.phyast.pitt.edu/~micheles/python/decorator-2.3.1.zip

or just run

easy_install decorator

Have fun!

                  Michele Simionato
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to