formparse package¶
Submodules¶
formparse.formula module¶
Formula that can be parsed and evaluated
- class formparse.formula.Formula(formula: str)[source]¶
Bases:
objectSimple 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:
- Raises:
FormulaComplexityError – If calculating the result size itself
is to expensive. –
- Returns:
The maximum power to ten of the result.
- Return type:
- 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:
FormulaRuntimeError – If the arguments are not a dictionary.
FormulaRuntimeError – If the evaluation fails for any other reason.
- Returns:
The value of the result.
- Return type:
- 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
- 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:
FormulaRuntimeErrorException raised if the expected result is to big to calculate.
- exception formparse.formula.FormulaException[source]¶
Bases:
ExceptionGeneric Exception for Formula, base class for FormulaSyntaxError and FormulaRuntimeError.
- exception formparse.formula.FormulaRuntimeError[source]¶
Bases:
FormulaExceptionException raised if there is an error during the runtime of the formula, especially with the argument input.
- exception formparse.formula.FormulaSyntaxError[source]¶
Bases:
FormulaExceptionException raised if there is an error in the syntax of the formula input.
- exception formparse.formula.FormulaZeroDivisionError[source]¶
Bases:
FormulaRuntimeErrorException raised if there is a division throgh 0 error.