aspy-yaml

1.3.0 · active · verified Thu Apr 16

aspy-yaml is a Python library providing a few extensions to PyYAML, primarily focused on preserving dictionary order during YAML loading and dumping. It offers `ordered_load` and `ordered_dump` functions to maintain insertion order for mappings, which is not guaranteed by standard `yaml.load` in older Python versions. The library is currently at version 1.3.0, released in May 2019, and is maintained with a low release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `ordered_load` to parse a YAML string into a Python object, ensuring that dictionary keys maintain their original order. It then shows how to use `ordered_dump` to serialize a Python object back into a YAML string, again preserving the order of keys. This is particularly useful for configuration files where order matters or for consistent output.

from aspy.yaml import ordered_load, ordered_dump

yaml_str = '''
key1: value1
key2: value2
list_key:
  - itemA
  - itemB
'''

# Load YAML while preserving dictionary order
data = ordered_load(yaml_str)
print(f"Loaded data (order preserved): {data}")

# Verify order
assert list(data.keys()) == ['key1', 'key2', 'list_key']

# Modify data and dump it back, preserving order
data['new_key'] = 'new_value'
output_yaml = ordered_dump(data)
print(f"\nDumped YAML (order preserved):\n{output_yaml}")

view raw JSON →