customize()
add_operator(operator_symbol, num_args, left_right_assoc, operator_function)
add_variable(variable, value)
add_function(function_name, num_args, function_method)
parse(expression)
evaluate(expression)
vars()
LEFT
andRIGHT
MAX_VARS
ident_letters
user_defined_functions_supported
Creating a custom ArithmeticParser class
Defining a new variable
Adding a new operator
Adding a new function
Accessing evaluated results and variables from your code:
parser['xyz'] = 100
parser.evaluate("√xyz") # Returns 10.0
Defining an operator that is both unary and binary
- Do not add functions that use or give access to
eval
,exec
,compile
,import
,subprocess
oros
- If adding functions that start separate threads, limit the total number of threads that your parser will create at one time.
- Be extremely careful if reading/writing to the file system
- Take care when exposing access to an underlying database or server files
- Some math functions may need to be constrained to avoid extended arithmetic processing (see
constrained_factorial
in theBaseArithmeticParser
as an example) - When populating web page elements with gathered input strings, be sure to escape potential quotation and control
characters (see
bottle_repl.py
as an example) - Be aware that your functions may get called recursively, or in an endless loop