Implements the parameter function parser

Parameter functions are entered as strings in the Framework file. This module implements the function parser that converts the string into an executable Python representation.


parse_function(fcn_str) Parses a string into a Python function

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)

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

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)
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