What is ASTEVAL?
masterASTEVAL is a minimal Python AST (Abstract Syntax Tree) evaluator designed to evaluate Python expressions and statements more safely than Python's built-in eval() and more capably than ast.literal_eval().
It provides an embedded interpreter that evaluates a subset of the Python language, with a primary focus on mathematical expressions. It includes functions from the math module by default and automatically includes a large number of numpy functions if numpy is installed. It also supports numpy-financial functions if that package is present.
Key features supported by default include:
- Mathematical expressions
- Array slicing and subscripting
- Control flow:
if-then-elseconditionals,whileloops,forloops, andtry-exceptblocks - List comprehensions
- User-defined functions
- Standard Python data structures: strings, dictionaries, tuples, lists, sets, and numpy arrays (including their built-in methods).