Skip to content

alexandru-dinu/myaml

Repository files navigation

myaml

workflow License pypi Code style: black

M(ath)YAML: evaluate math expressions in YAML files.

Install

pip install myaml

Usage

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'
    }}
]

Notes