ANN: decorator 2.3

Discussion in 'Python' started by Michele Simionato, Aug 5, 2008.

  1. 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
    Michele Simionato, Aug 5, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean Lutrin
    Replies:
    8
    Views:
    633
    Chris Uppal
    Nov 18, 2004
  2. William Z.
    Replies:
    0
    Views:
    468
    William Z.
    Mar 22, 2006
  3. Sandy Norton

    tweaking @decorator syntax

    Sandy Norton, Aug 4, 2004, in forum: Python
    Replies:
    14
    Views:
    484
    Dave Brueck
    Aug 6, 2004
  4. glomde
    Replies:
    5
    Views:
    515
    glomde
    Mar 29, 2007
  5. Jake McArthur

    [ANN] Cache Decorator

    Jake McArthur, Jul 21, 2006, in forum: Ruby
    Replies:
    2
    Views:
    95
    Jake McArthur
    Jul 21, 2006
Loading...

Share This Page