ANN: decorator 2.3

  • Thread starter Michele Simionato
  • Start date
M

Michele Simionato

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top