yayaml makes yaml nicer. yay!
Project description
yayaml
The yayaml package provides extensions to ruamel.yaml that allow creating some often-needed Python objects directly via YAML tags and making it easier to represent custom objects when writing YAML files.
yay 🥳
yayaml is used in the following projects:
- paramspace: for config-file-based grid search with deeply nested dicts
- dantro: for loading, processing and visualizing high-dimensional simulation output
- utopya: a versatile simulation framework
Installation
First, create and/or enter a Python virtual environment in which you would like to install this package.
The package can then be installed using pip:
pip install yayaml
Use
Loading and writing YAML files is easy:
import yayaml as yay
# Loading and writing
yay.yaml.load("{foo: bar}")
yay.load_yml("path/to/file.yml")
yay.write_yml(dict(foo="bar"), path="path/to/output.yml")
The main feature of yayaml is to provide 40+ additional YAML tags for common operations, super-charing your YAML files.
For example:
# Math operations
sum: !sum [1, 2, 3] # 6
product: !prod [2, 3, 4] # 24
power: !pow [2, 8] # 256
expr: !expr 2**8 + 1 # 257
# Comparisons and logic
check: !gt [5, 3] # true
all_true: !all [true, true] # true
result: !if-else [!gt [5, 3], "yes", "no"] # "yes"
# Strings
msg: !format ["{} + {} = {}", 1, 2, 3] # "1 + 2 = 3"
csv: !join [", ", [a, b, c]] # "a, b, c"
# Environment and paths
home: !env HOME # /home/user
config: !expanduser ~/.config/myapp # /home/user/.config/myapp
path: !joinpath ["~", data, file.csv] # ~/data/file.csv
# Numeric ranges (useful for configs)
params: !linspace [0, 1, 5] # [0.0, 0.25, 0.5, 0.75, 1.0]
range: !arange [0, 10, 2] # [0, 2, 4, 6, 8]
See the documentation for the full list of YAML tags. It is also easily possible to create custom constructors and representers using simple decorators.
For developers
If you would like to develop for this project, installation should be done from a local git clone of the repository.
After having the project cloned, enter a virtual environment for development.
To then install the package (in editable mode), run:
cd yayaml
pip install -e ".[dev]"
Or, using uv:
cd yayaml
uv sync
This will include development-related dependencies (for tests and building of the documentation).
To automatically run pre-commit hooks, install the configured git hooks using pre-commit install.
Running tests
Enter the virtual environment, then run pytest:
python -m pytest -v tests/ --cov=yayaml --cov-report=term-missing
Building the documentation
cd doc
make doc
make linkcheck # optional
make doctest # optional
The documentation can then be found in doc/_build/html.
To automatically generate figures, set the YAYAML_USE_TEST_OUTPUT_DIR environment variable before invoking make doc.
export YAYAML_USE_TEST_OUTPUT_DIR=yes
License
The yayaml package is open-source software licensed under BSD 2-clause, see LICENSE.
Copyright holders
- Yunus Sevinchan (maintainer)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file yayaml-0.3.0.tar.gz.
File metadata
- Download URL: yayaml-0.3.0.tar.gz
- Upload date:
- Size: 31.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e068b3435520d9258c239468764514fe7a09570aac05d09ba8acb4c6e6576d40
|
|
| MD5 |
11f1fb9d078392398867c216abb8156c
|
|
| BLAKE2b-256 |
aab5b4420ae9a75b8cb0d22388dc0e65e99269f9c0ece5eaba3f4cafa2febdc9
|
File details
Details for the file yayaml-0.3.0-py3-none-any.whl.
File metadata
- Download URL: yayaml-0.3.0-py3-none-any.whl
- Upload date:
- Size: 20.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e873e1aec5aa98306f09ee981a8b7bf479ffb0e1c49fcb3efab2ceba079a4745
|
|
| MD5 |
2600f2f90ae74d4bd777ca85f5b62fd1
|
|
| BLAKE2b-256 |
67251e15aabcce63619d4b19b3756b85be1b03db6809534a27d1a36fba4af8f9
|