M(ath)YAML: evaluate math expressions in YAML files.
pip install myaml
myaml
allows you to define math expressions in YAML files:
# test.yaml
---
- x1:
x: !eval 2**(3 - 1)
y: !eval (12 % 9) - sqrt(9)
- x2:
x: !eval (2**3) - 1.0
y: !eval -0.75 ** (9 - cos(3.1415) * log(2.718))
- x3:
x: hello world
y: /this/is/a/path
Expressions annotated with !eval
tag will be evaluated at load-time.
Any other strings will be left intact:
>>> import myaml
>>> xs = myaml.safe_load('test.yaml')
>>> xs
[
{'x1': {'x': 4, 'y': 0}},
{'x2': {'x': 7, 'y': -0.056315}},
{'x3': {
'x': 'hello world',
'y': '/this/is/a/path'
}}
]
myaml
supports arbitrary nesting, conforming with YAML spec.myaml
is built on top of PyYAML'ssafe_load
.- expressions should be SymPy compatible, since
parse_expr
is used for evaluation.