formparse package

Submodules

formparse.formula module

Formula that can be parsed and evaluated

class formparse.formula.Formula(formula: str)[source]

Bases: object

Simple formula, generated from a string input can it be evaluated with it .eval()`method. The currently supported operators are `+, -, * and /.

BIN_OPERATORS = {<class '_ast.Add'>: <built-in function add>, <class '_ast.Sub'>: <built-in function sub>, <class '_ast.Mult'>: <built-in function mul>, <class '_ast.Div'>: <built-in function truediv>, <class '_ast.Pow'>: <built-in function pow>}
EVALUATORS = {<class '_ast.Expression'>: '_eval_expression', <class '_ast.Constant'>: '_eval_constant', <class '_ast.Name'>: '_eval_name', <class '_ast.BinOp'>: '_eval_binop', <class '_ast.UnaryOp'>: '_eval_unaryop', <class '_ast.Call'>: '_eval_call'}
FUNCTIONS = {'abs': <built-in function abs>, 'max': <built-in function max>, 'min': <built-in function min>}
MAX_FORMULA_LENGTH = 255

Maximum length accepted for the formula, as is (incl. whitespaces, etc.)

MAX_RESULT_POTENCY = 18

Maximum maximum power of ten that is allowed for a expected result.

UN_OPERATORS = {<class '_ast.USub'>: <built-in function neg>}
classmethod estimate_result_size(node, args: dict | None = None) int[source]

Estimate the result size based one the power of tens of its operands.

A more indepth explanation will follow.

Parameters:
  • node (ast.AST) – Node to estimate result size for.

  • args (Optional[dict], optional) – Parameters provided for

  • evaluation

  • provided (if not) –

  • assigned (each variable is) –

  • None. (potency of 1. Defaults to) –

Raises:
Returns:

The maximum power to ten of the result.

Return type:

int

eval(args: dict | None = None) float[source]

Evaluate the formula for a set if given arguments

Parameters:

args (Optional[dict], optional) – A dictionary with the arguments. Defaults to {}.

Raises:
Returns:

The value of the result.

Return type:

float

classmethod parse_formula(formula: str) AST[source]

Parse a given formula into an ast node.

Parameters:

formula (str) – Formula to parse.

Returns:

Parsed node.

Return type:

ast.AST

classmethod validate(node: AST) Tuple[bool, str][source]

Check whether or not formula provided in node is valid.

NOTE: Formula.validate() does not check for length constraint but just for general validity. :param node: Formula to check, either as str or parsed ast Tree. :type node: ast.AST | str

Returns:

If the formula is valid, if not, provide reason in second value.

Return type:

Tuple[bool, str | None]

validate_result_size(args: dict | None = None) None[source]

Make sure that the maximum estimated result is not bigger than as set by MAX_RESULT_POTENCY.

Parameters:
  • args (Optional[dict], optional) – Parameters provided for

  • evaluation

  • provided (if not) –

  • assigned (each variable is) –

  • None. (potency of 1. Defaults to) –

Raises:

FormulaComplexityError – If the expected result is bigger than allowed.

exception formparse.formula.FormulaComplexityError[source]

Bases: FormulaRuntimeError

Exception raised if the expected result is to big to calculate.

exception formparse.formula.FormulaException[source]

Bases: Exception

Generic Exception for Formula, base class for FormulaSyntaxError and FormulaRuntimeError.

exception formparse.formula.FormulaRuntimeError[source]

Bases: FormulaException

Exception raised if there is an error during the runtime of the formula, especially with the argument input.

exception formparse.formula.FormulaSyntaxError[source]

Bases: FormulaException

Exception raised if there is an error in the syntax of the formula input.

exception formparse.formula.FormulaZeroDivisionError[source]

Bases: FormulaRuntimeError

Exception raised if there is a division throgh 0 error.

Module contents