API Reference for Enthought Tool Suite 3.2.0
Allows a function to execute as if locals are a context
Turn arguments of a function into local variables in a code object
Take (operation, argument) tuples and return a bytecode string.
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.
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.
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
Decorator that specifies a context_factory to be used for this function
This is a thin wrapper around a context_function call.
Take a bytecode string and generate (operation, argument) tuples.
Generator which replaces *_FAST and *_GLOBAL ops with *_NAME ops.
| Local name | Refers to |
|---|---|
| dis | dis |
| new | new |
| struct | struct |
| wraps | functools.wraps |
© Copyright 2002-2009 Enthought, Inc.