The PEAK Developers' Center   Diff for "DecoratorTools" UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View
Differences between version dated 2009-11-16 17:01:05 and 2010-05-06 15:20:17
Deletions are marked like this.
Additions are marked like this.

 
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

    ... def decorator(func):
    ... [template_function()] # could also be @template_function in 2.4
    ... def before_and_after2(__func, __message):
    ... '''
    ... return '''
    ... print "before", __message
    ... try:
    ... return __func($args)

the generated function to be able to access (aside from builtins) must be
declared as arguments to the decorating function, and all arguments must be
named so as not to conflict with the names of any of the decorated function's
arguments. The docstring must either fit on one line, or begin with a newline
and have its contents indented by at least two spaces. The string ``$args``
may be used one or more times in the docstring, whenever calling the original
function. The first argument of the decorating function must always be the
original function.
arguments.
 
The function template must return a static string that will be compiled into
a new function by DecoratorTools. The returned string must either fit on one
line, or begin with a newline and have its contents indented by at least two
spaces. The string ``$args`` may be used one or more times in the returned
string, whenever calling the original function. The first argument of the
decorating function must always be the original function.
 
Note, however, that function template is only called *once*, in order to get
this string, and it's called with dummy arguments. So the function must not
attempt to actually *use* any of its arguments, and must **always return a
static string**. Any attempt to insert the supplied arguments into the
template will result in an error::
 
    >>> def broken_decorator(func):
    ... [template_function()]
    ... def broken_template(__func, __message):
    ... # This doesn't work; don't do this:
    ... return '''
    ... print "before %(__message)s"
    ... try:
    ... return __func($args)
    ... finally:
    ... print "after %(__message)s"
    ... ''' % locals()
    ... return broken_template(func, "test")
 
    >>> broken_decorator(foo)
    Traceback (most recent call last):
      ...
    RuntimeError: template functions must return a static string!
 
 
Debugging Generated Code

    ...
    ... def __new__(cls, *args, **kw):
    ... print "new called with", args, kw
    ... return super(Demo, cls).__new__(cls, *args, **kw)
    ... return super(Demo, cls).__new__(cls)
    ...
    ... def __init__(self, *args, **kw):
    ... print "init called with", args, kw

PythonPowered
ShowText of this page
EditText of this page
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck