Strict Standards: Declaration of action_plugin_blog::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /www/htdocs/w00d9226/oliverh.com/lib/plugins/blog/action.php on line 13
Strict Standards: Declaration of action_plugin_discussion::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /www/htdocs/w00d9226/oliverh.com/lib/plugins/discussion/action.php on line 745
Strict Standards: Declaration of action_plugin_importoldchangelog::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /www/htdocs/w00d9226/oliverh.com/lib/plugins/importoldchangelog/action.php on line 157
Deprecated: Assigning the return value of new by reference is deprecated in /www/htdocs/w00d9226/oliverh.com/inc/parserutils.php on line 202
Deprecated: Assigning the return value of new by reference is deprecated in /www/htdocs/w00d9226/oliverh.com/inc/parserutils.php on line 205
Deprecated: Assigning the return value of new by reference is deprecated in /www/htdocs/w00d9226/oliverh.com/inc/parserutils.php on line 314
Deprecated: Assigning the return value of new by reference is deprecated in /www/htdocs/w00d9226/oliverh.com/inc/parserutils.php on line 454
Strict Standards: Declaration of cache_instructions::retrieveCache() should be compatible with cache::retrieveCache($clean = true) in /www/htdocs/w00d9226/oliverh.com/inc/cache.php on line 291
Deprecated: Function split() is deprecated in /www/htdocs/w00d9226/oliverh.com/inc/auth.php on line 146
Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/w00d9226/oliverh.com/lib/plugins/blog/action.php:13) in /www/htdocs/w00d9226/oliverh.com/inc/auth.php on line 236
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/w00d9226/oliverh.com/inc/auth.php on line 390
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/w00d9226/oliverh.com/inc/auth.php on line 390
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/w00d9226/oliverh.com/inc/auth.php on line 387
Strict Standards: Only variables should be passed by reference in /www/htdocs/w00d9226/oliverh.com/doku.php on line 69
Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/w00d9226/oliverh.com/lib/plugins/blog/action.php:13) in /www/htdocs/w00d9226/oliverh.com/inc/actions.php on line 350
====== Python Code Object Utilities ======
This library provides a simple interface for changing the behavior of existing function/method objects.
Download: [[http://www.oliverh.com/files/pycodeutils.zip|pycodeutils.zip]] (License: MIT License, see file ''LICENSE'' in the distribution)
===== Usage =====
The behavior of a function/method object is changed by replacing its associated code object with a new one. The new code object delegates the execution to a given user-defined function (delegatee). The arguments as well as the original code are passed to the delegatee. So it is possible to either execute additional code before/after the original code or to replace the original code completely.
# New behaviour
def delegatee(f, args, kw): ...
# Change behavior of function my_func
from codeinst import delegate_code
delegate_code(my_func, delegatee)
===== Example =====
The following example changes the behavior of two methods of Python's standard ''Template'' class so that an additional substitution dictionary is used.
# Import advise function of the code instrumentation library
from codeinst import delegate_code
from string import Template
# Define your new behaviour
global_subst_kws = {'x': 'X'}
def substitute_delegatee(f,args,kw):
kw.update(global_subst_kws)
return f(*args,**kw)
# Apply the new behavior to Template's substitution methods
delegate_code([Template.substitute, Template.safe_substitute],
substitute_delegatee)
# Test the new behavior
print Template("$x and $y").substitute({'x': '1', 'y': '2'}) # => X and 2
print Template("$x and $z").safe_substitute({'z': '3'}) # => X and 3
===== Delegatee =====
The delegatee is a function with the signature ''delegatee(f, args, kw)'':
* ''f'': Function object representing the original code((Note that the function object ''f'' representing the original code is created each time the function is invoked.))
* ''args'': Tuple containing the positional arguments followed by the variable arguments
* ''kw'': Dictionary containing the remaining keyword arguments (i.e. the keyword arguments which could not be bound to positional arguments of ''f'').
You can call ''f'' whenever you want to use the original function. You can change the arguments. It is even possible to call ''f'' several times. If you just want to proceed with the original behavior, you can use ''f(*args,**kw)'' (don't forget to return the value).
===== Comparison to Function Wrappers =====
Note that replacing the code object is different from wrapping a function object. A wrapper is a complete new function object which encapsulates the original function. However, because it is a new object, the effect of the wrapper is only //local//. Already established references to the function object (e.g. in other modules) still refer to original function.
Contrariwise, by means of replacing the code object, the function object itself remains the same. The new code object is in effect for all existent references to the function.
===== Remarks =====
Only modifications to plain functions and unbound methods are allowed. Trying to instrument a bound method (i.e. a method already associated with a particular ''self'' instance) will raise a ''TypeError''. This is intentional.
Furthermore, built-in functions and methods are not supported because they do not provide a code object.