modelx.trace_locals#

trace_locals(index=-1)[source]#

Retuns the local variables of a formula execution in the last traceback.

This function is a shotcut for get_traceback()[index][2], and returns a dict of the local variables referenced by the formula execution at index in the traceback list. By default, index is -1, so the local variables of the last formula execution in which the error is raised, are returned.

If the last formula execution is sucessful, i.e. the traceback list is empty, then returns None.

Parameters:

index (optional) – The position of the formula exectuion in the traceback list.

Example

>>> import modelx as mx

>>> @mx.defcells
... def foo(x):
...     a = 1
...     return bar(x) + a

>>> @mx.defcells
... def bar(y):
...     b = 2
...     return 2 * y / 0  # raises ZeroDivisionError

>>> foo(1)
modelx.core.errors.FormulaError: Error raised during formula execution
ZeroDivisionError: division by zero
Formula traceback:
0: Model1.Space1.foo(x=1), line 3
1: Model1.Space1.bar(y=1), line 3
Formula source:
def bar(y):
    b = 2
    return 2 * y / 0 #  raise ZeroDivizion

>>> mx.trace_locals()
{'y': 1, 'b': 2}

>>> mx.trace_locals(-2)
{'x': 1, 'a': 1}

See also

get_traceback()