mmfutils.debugging¶
Some debugging tools.
Most of these are implemented as decorators.
-
class
mmfutils.debugging.
persistent_locals
(func)[source]¶ Bases:
object
Decorator that stores the function’s local variables.
Examples
>>> @persistent_locals ... def f(x): ... y = x**2 ... z = 2*y ... return z >>> f(1) 2 >>> sorted(f.locals.items()) [('x', 1), ('y', 1), ('z', 2)] >>> f.clear_locals() >>> f.locals {}
-
locals
¶
-
-
mmfutils.debugging.
debug
(*v, **kw)[source]¶ Decorator to wrap a function and dump its local scope.
Parameters: (or env) (locals) – Function’s local variables will be updated in this dict. Use locals() if desired. Examples
>>> env = {} >>> @debug(env) ... def f(x): ... y = x**2 ... z = 2*y ... return z >>> f(1) 2 >>> sorted(env.items()) [('x', 1), ('y', 1), ('z', 2)]
This will put the local variables directly in the global scope:
>>> @debug(locals()) ... def f(x): ... y = x**2 ... z = 2*y ... return z >>> f(1) 2 >>> x, y, z (1, 1, 2) >>> f(2) 8 >>> x, y, z (2, 4, 8)
If an exception is raised, you still have access to the results:
>>> env = {} >>> @debug(env) ... def f(x): ... y = 2*x ... z = 2/y ... return z >>> f(0) Traceback (most recent call last): ... File "<doctest mmfutils.debugging.debug[14]>", line 1, in <module> f(0) File "<doctest mmfutils.debugging.debug[13]>", line 4, in f z = 2/y ZeroDivisionError: division by zero >>> sorted(env.items()) [('x', 0), ('y', 0)]