module documentation

Allows a function to execute as if locals are a context

Class ContextFunctionError Undocumented
Function args_to_locals Turn arguments of a function into local variables in a code object
Function compile_bytecode Take (operation, argument) tuples and return a bytecode string.
Function context_function Allows a function to execute as if locals are a context
Function local_context Decorator that specifies a context_factory to be used for this function
Function parse_bytecode Take a bytecode string and generate (operation, argument) tuples.
Function patch_load_and_store Generator which replaces \*_FAST and \*_GLOBAL ops with \*_NAME ops.
def args_to_locals(co): (source)

Turn arguments of a function into local variables in a code object

def compile_bytecode(ops): (source)

Take (operation, argument) tuples and return a bytecode string.

def context_function(f, context_factory): (source)

Allows a function to execute as if locals are a context This decorator modifies a function so that it uses contexts generated by a context_factory in place of the usual local dictionary. In most cases the context_factory function should return a fresh context on each call. Potential uses include: * over-riding internal globals by pre-inserting values into the local namespace (eg. replacing math with numpy in the function's namespace so that a function can be converted to use with arrays). * internal unit conversion * introspection of function operation This decorator works by re-writing the function's bytecode, so it will not work for functions coming from C extension modules. It also cannot currently work with functions that contain closures. Parameters f : function the function to be decorated context_factory : callable a callable that returns a context to be used as the function's local namespace Returns a function that can be used in place of f Examples Over-writing a global in a function using a pre-filled local context >>> import math >>> def f(x): ... return 2*math.sin(x) + math.cos(x) >>> import numpy >>> def numpy_math_context(): ... return {'math': numpy} >>> f = context_function(f, numpy_math_context) >>> f(numpy.array([0, 0.5, 1])*numpy.pi) array([1.0, 2.1213203435596424, 2]) Poor-man's closure: >>> def accumulator(value): ... total += value >>> accumulation_dict = {'total': 0} >>> def accumulator_factory(): ... return accumulator_dict >>> accumulator = context_function(accumulator, accumulator_factory) >>> for i in range(10): ... accumulator(i) >>> accumulation_dict['total'] 45 Closure raises an exception: >>> def f(x): ... a = 1 ... def g(y): ... return y+a ... return x+g(x) >>> context_function(f, dict) ContextFunctionError: can't create context_function for function containing closure

def local_context(context_factory): (source)

Decorator that specifies a context_factory to be used for this function This is a thin wrapper around a context_function call.

def parse_bytecode(bytes): (source)

Take a bytecode string and generate (operation, argument) tuples.

def patch_load_and_store(ops, argcount, nglobals, ncellandfreevars): (source)

Generator which replaces \*_FAST and \*_GLOBAL ops with \*_NAME ops.