Order nested YAML output using ruamel.yaml with custom key ordering
Project description
orderedYAML
A small Python package to recursively convert Python dicts/lists into ruamel.yaml-compatible, ordered YAML using CommentedMap. Preserves formatting, ordering, and supports custom key orders at any nesting depth.
Features
- Recursively reorders YAML dictionaries based on custom paths
- Outputs block-style YAML using
ruamel.yaml - Compatible with Python 3.7+
Installation
pip install yaml-order
Usage
from orderedYAML import OrderedYAML
data = {
'metadata': {
'labels': {'env': 'prod', 'app': 'my-app'},
'name': 'my-resource',
},
'apiVersion': 'v1',
'kind': 'Service',
'spec': {
'ports': [{'port': 80}],
'selector': {'app': 'my-app'},
'type': 'ClusterIP',
},
}
ordering = {
(): ['apiVersion', 'kind', 'metadata', 'spec'],
('metadata',): ['name', 'labels'],
('spec',): ['selector', 'ports', 'type'],
}
yaml_obj = OrderedYAML(data, ordering)
# To stdout
yaml_obj.dump()
# Or to a file
with open("out.yaml", "w") as f:
yaml_obj.dump(f)
# Or get as a string
yaml_string = yaml_obj.dumps()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
orderedyaml-0.1.0.tar.gz
(3.2 kB
view details)
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 orderedyaml-0.1.0.tar.gz.
File metadata
- Download URL: orderedyaml-0.1.0.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0784943eb967d3ad6b02d2e6e59ed158eb8222c2e4dc8ea2921a3f405cfe9674
|
|
| MD5 |
410eb52c2c1d5ece07cbd32260ad82ad
|
|
| BLAKE2b-256 |
b514af3ec8d9372f34cf02946e9346670d0a0df445b4bc3e62660ca14acccd8b
|
File details
Details for the file orderedyaml-0.1.0-py3-none-any.whl.
File metadata
- Download URL: orderedyaml-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07cec4d9cc0af9f905a8c5e90b6d779b410d0ffc7329627f38ee83b76901ab64
|
|
| MD5 |
696d78553769c0bbfca475725cb632ee
|
|
| BLAKE2b-256 |
66719f3a4b21f02faea3beae49df0b1b590e5f0ca6dd8e2fb814ca63fd60294c
|