SaneYAML

0.6.1 · active · verified Fri Apr 17

SaneYAML (version 0.6.1) is a lightweight wrapper around PyYAML designed to provide a safer and more predictable experience when reading and writing configuration files. It focuses on preserving dictionary order and preventing common PyYAML footguns such as unwanted implicit type conversions for strings like 'yes', 'no', dates, or numbers. Its release cadence is sporadic, focusing on stability and essential bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `saneyaml.load` and `saneyaml.dump` to process YAML data. It highlights SaneYAML's core features: preserving order of keys and preventing implicit type conversions for values like 'yes', 'no', or date strings, which are often problematic in default PyYAML usage. Notice how 'No' and '2023-10-27' are loaded as strings, not booleans or datetime objects.

from saneyaml import load, dump
import io

# Example YAML data
config_data = {
    'name': 'My App',
    'version': '1.0.0',
    'settings': {
        'debug': 'No',  # saneyaml keeps this as string, not bool
        'log_level': 'INFO',
        'features': ['alpha', 'beta']
    },
    'last_updated': '2023-10-27' # saneyaml keeps this as string, not datetime
}

# Dump to a YAML string (preserving order and types)
output_stream = io.StringIO()
dump(config_data, output_stream)
yaml_string = output_stream.getvalue()
print("\n--- Dumped YAML ---\n" + yaml_string)

# Load from a YAML string (preventing implicit conversions)
loaded_data = load(yaml_string)

print("\n--- Loaded Data ---")
print(f"Debug setting type: {type(loaded_data['settings']['debug'])} (value: {loaded_data['settings']['debug']})")
print(f"Last updated type: {type(loaded_data['last_updated'])} (value: {loaded_data['last_updated']})")
print(f"Loaded data equality: {loaded_data == config_data}")

# Demonstrate order preservation (requires Python 3.7+ for dicts)
# For older Pythons, saneyaml handles order internally.
original_keys = list(config_data.keys())
loaded_keys = list(loaded_data.keys())
print(f"Original top-level keys order: {original_keys}")
print(f"Loaded top-level keys order: {loaded_keys}")

view raw JSON →