Package nltk :: Module decorators
[hide private]
[frames] | no frames]

Module decorators

source code

Decorator module by Michele Simionato <michelesimionato@libero.it> Copyright Michele Simionato, distributed under the terms of the BSD License (see below). http://www.phyast.pitt.edu/~micheles/python/documentation.html

Included in NLTK for its support of a nice memoization decorator.

Functions [hide private]
 
getinfo(func)
Returns an info dictionary containing: - name (the name of the function : str) - argnames (the names of the arguments : list) - defaults (the values of the default arguments : tuple) - signature (the signature : str) - doc (the docstring : str) - module (the module name : str) - dict (the function __dict__ : str)
source code
 
update_wrapper(wrapper, model, infodict=None) source code
 
new_wrapper(wrapper, model)
An improvement over functools.update_wrapper.
source code
 
__call__(self, func) source code
 
decorator_factory(cls)
Take a class with a .caller method and return a callable decorator object.
source code
 
decorator(caller)
General purpose decorator factory: takes a caller function as input and returns a decorator with the same attributes.
source code
 
getattr_(obj, name, default_thunk)
Similar to .setdefault in dictionaries.
source code
 
memoize(func) source code
Function Details [hide private]

new_wrapper(wrapper, model)

source code 
An improvement over functools.update_wrapper. The wrapper is a generic callable object. It works by generating a copy of the wrapper with the right signature and by updating the copy, not the original. Moreovoer, 'model' can be a dictionary with keys 'name', 'doc', 'module', 'dict', 'defaults'.

decorator_factory(cls)

source code 
Take a class with a .caller method and return a callable decorator object. It works by adding a suitable __call__ method to the class; it raises a TypeError if the class already has a nontrivial __call__ method.

decorator(caller)

source code 

General purpose decorator factory: takes a caller function as input and returns a decorator with the same attributes. A caller function is any function like this:

def caller(func, *args, **kw):
    # do something
    return func(*args, **kw)

Here is an example of usage:

>>> @decorator
... def chatty(f, *args, **kw):
...     print "Calling %r" % f.__name__
...     return f(*args, **kw)
>>> chatty.__name__
'chatty'
>>> @chatty
... def f(): pass
...
>>> f()
Calling 'f'

decorator can also take in input a class with a .caller method; in this case it converts the class into a factory of callable decorator objects. See the documentation for an example.

memoize(func)

source code 
Decorators:
  • @decorator