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 orderedYAML
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.1.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.1.tar.gz.
File metadata
- Download URL: orderedyaml-0.1.1.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 |
6ccaf72b3dadaa169e988e859537603a262b39eedae8a797830644b381a63050
|
|
| MD5 |
f114a376319c211e1df6ac43214d147a
|
|
| BLAKE2b-256 |
9e4ce890c4b52848417a7cadaa72bf7edf40e2738e71c60c404f7bacbd08a5bd
|
File details
Details for the file orderedyaml-0.1.1-py3-none-any.whl.
File metadata
- Download URL: orderedyaml-0.1.1-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 |
65769899dabd5a8826436c1d0d07e5a265598612ec377fbf30be4ac32708c760
|
|
| MD5 |
420232df6400cf827fa43741e5007f35
|
|
| BLAKE2b-256 |
ade9839619767c88ca9acb21f634ca664b0aee269afcf258b3b2a3ce55b8e3ee
|