Provides syntax and lexical analyzers, and an expression calculator optimized for repeated calculations.
Packages
The module contains the following packages:
- Calculator - expression calculator
- CSV - CSV tokenizer
- IO - input/output utility classes to support lexical analysis
- Mustache - Mustache templating engine
- Tokenizers - lexical analyzers to break incoming character streams into tokens
- Variants - dynamic objects that can hold any values and operators for them
Use
Install the Python package as
pip install pip_services3_expressions
The example below shows how to use the expression calculator to dynamically calculate user-defined expressions.
from pip_services3_expressions.variants.Variant import Variant
from pip_services3_expressions.calculator.variables.Variable import Variable
from pip_services3_expressions.calculator.ExpressionCalculator import ExpressionCalculator
from pip_services3_expressions.calculator.variables.VariableCollection import VariableCollection
# ...
clalculator = ExpressionCalculator()
clalculator.expression = "A + b / (3 - Max(-123, 1)*2)"
vars = VariableCollection()
vars.add(Variable("A", Variant(1)))
vars.add(Variable("B", Variant(3)))
result = clalculator.evaluate_with_variables(vars)
print('The result of the expression is ' + result.as_string)
# ...
The following is an example on how to process mustache templates.
mustache = MustacheTemplate()
mustache.template = "Hello, {{{NAME}}}{{#ESCLAMATION}}!{{/ESCLAMATION}}{{#unless ESCLAMATION}}.{{/unless}}"
result = mustache.evaluate_with_variables({ 'NAME': 'Mike', 'ESCLAMATION': True })
print("The result of template evaluation is '" + result + "'")