Expression
- class caqtus.types.expression.Expression(body: str)
Bases:
objectRepresents a mathematical expression that can be evaluated in a given context.
This class is a wrapper around a python string expression that can be evaluated later when the values of the variables it depends on are known.
The expression is immutable.
If the expression contains syntax errors, they only will be raised when the expression is evaluated.
The expression must be a valid python expression, with some exceptions:
The % symbol is understood as a multiplication by 0.01.
The ° symbol is understood as the degree symbol and will be replaced by the name deg in the expression.
Implicit multiplication is allowed, so that “a b” will be parsed as “a * b”.
- evaluate(
- variables: Mapping[DottedVariableName, Any],
Evaluate an expression on specific values for its variables.
- Parameters:
variables – The context in which the expression will be evaluated. The variables the expression depends upon will be replaced by the values in this mapping. The mapping is also concatenated with some built-in functions and constants.
- Returns:
The result of the evaluation. The type of the result depends on the body of the expression and the values of the upstream variables.
- Raises:
EvaluationError – if an error occurred during evaluation.
- property upstream_variables: frozenset[VariableName]
Return the name of the other variables the expression depend on.
- check_syntax() SyntaxError | None
Force parsing of the expression.
It is not necessary to call this method explicitly, as the expression will be parsed automatically when it is evaluated. However, this method can be used to force the parsing to happen at a specific time, for example to catch syntax errors early.
- Returns:
None if the expression is valid, or a SyntaxError otherwise.