atomica.function_parser.parse_function

atomica.function_parser.parse_function(fcn_str)[source]

Parses a string into a Python function

This function takes in the string representation of a function e.g. 'x+y'. It returns an Python function object that takes in a keyword arguments corresponding to the original quantities that appeared in the function. For example:

>>> fcn, deps = atomica.parse_function('x+y')
>>> fcn
<function atomica.function_parser.parse_function.<locals>.fcn(**deps)>
>>> deps
['x', 'y']
>>> fcn(x=2,y=3)
5

Note that for security, only a subset of Python functions are allowed to be called. These are mainly mathematical operations such as max or exp. A full listing can be found in function_parser.py.

A common usage pattern is to construct a dict of inputs to the parsed function using the list of dependencies returned by parse_function. For example:

>>> argdict = dict.fromkeys(deps,2)
>>> fcn(**argdict)
4
Parameters

fcn_str (str) – A string containing a single Python expression

Return type

tuple

Returns

A tuple containing a function, and a list of arguments required by the function