openepda.expr module

openepda.expr.py

This file contains a parser of analytic expressions.

The parser can be used to parse the analytic expressions and thus check their correctness in a safe way, and also to evaluate the expressions.

The grammar is intended to be very similar to the tinyexpr library grammar (https://github.com/codeplea/tinyexpr).

Author: Dima Pustakhod Copyright: 2020, TU/e - PITC and authors

exception openepda.expr.OpenEpdaError[source]
exception openepda.expr.OpenEpdaExpressionError[source]
openepda.expr.check_parameter_name(name, reserved=())[source]

Check if name can be used as identifier in the expression.

  • Contains letters, underscores, or digits

  • Starts with a letter

  • Is not a python keyword

  • Is not one of the reserved identifiers

Parameters
  • name (str) – identifier to be checked

  • reserved (iterable of str) – identifiers which are reserved and are therefore illegal, e.g. for other functions.

Returns

True if name is a correct identifier

Return type

bool

openepda.expr.evaluate_expression(expr: Union[str, numbers.Real, Sequence[TNestedSequence]], params=None) → Union[numbers.Real, List[Union[numbers.Real, List[TNestedRealList]]]][source]

Evaluate expression if necessary and return a numeric value.

Parameters
  • expr (TNestedSequence) – expression to be checked and evaluated if needed. Can be a sequence of expressions, or a sequence of sequences…

  • params (Optional[Dict[str, Any]]) – Parameter values to be used during expression evaluation. We do not restrict parameter values to the Real type, however, this could make sense. It depends on whether the functions used in the expression are able to produce numeric output for non-numeric inputs.

Returns

res

the input expression if it is a number, or the expression evaluation

result if it is a number.

Return type

Union[Real, TNestedRealList]

Raises

TypeError – In cases when the expression cannot be evaluated, or when it returned a non-numeric result.

openepda.expr.iskeyword()

x.__contains__(y) <==> y in x.

openepda.expr.make_choice_list(*choices, exclude=())[source]

Combine choices into a single list with unique items.

Some of the choices can be excluded.

Parameters
  • choices (iterable of str) – each iterable contains a list of functions to be included into the symbols.

  • exclude (iterable of str) – list of symbols to be excluded.

Returns

sorted list of symbols, where all choices are united, and strings from exclude parameter are removed.

Return type

list of str

openepda.expr.make_choice_str(choices=None)[source]
Parameters

choices (iterable of str) –

Returns

Return type

str

openepda.expr.make_symbol_mapping(mapping, exclude=())[source]

Import all symbols from the mapping using their name

Parameters
  • mapping (Dict[str, Tuple(str, str)]) – keys are symbol names, values are 2-tuples. value[0] is a module name, value[1] is attribute name to be imported from this module and to be used under symbol name.

  • exclude (iterable of str) – identifiers to be excluded from the final list

Returns

keys are imported symbol names, values are imported entities, e.g. function, parameters, etc.

Return type

Dict[str, Any]