Registry / serialization / oyaml
library1.0pypypi✓ verified 22d ago

oyaml is a drop-in replacement for PyYAML designed to preserve dictionary ordering during YAML loading and dumping. It supports both Python 2 and Python 3 by patching the underlying PyYAML library. The current version is 1.1, with releases occurring infrequently, primarily for compatibility updates with newer PyYAML versions or test suite improvements.

pip install oyaml
INSTALL
IMPORT
SIG · OYAML
O
oyaml
serializationpythonv1.0
Install
1.7s avg
Import
140ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.142s · 19.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.138s · 21MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

yaml
import oyaml as yaml
import yaml
oyaml is intended as a drop-in replacement. Aliasing it as 'yaml' ensures that code expecting PyYAML's interface benefits from oyaml's order-preserving features without modifications. Importing `yaml` directly would use PyYAML, not oyaml.

This quickstart demonstrates how to import oyaml as `yaml` and use its `safe_load` and `safe_dump` functions. It shows that `oyaml` preserves the order of keys when loading from a YAML string and when dumping an `OrderedDict` to a YAML string, by default, if `sort_keys=False` is used for `safe_dump` with standard dictionaries. For Python versions < 3.7, `safe_load` will return `OrderedDict` instances. For Python 3.7+ with standard `dict`s, while `safe_load` might return a standard `dict`, its order is insertion-ordered.

import oyaml as yaml from collections import OrderedDict yaml_data = """ key_c: 3 key_a: 1 key_b: 2 """ # Load YAML preserving order loaded_dict = yaml.safe_load(yaml_data) print(f"Loaded dictionary type: {type(loaded_dict)}") print(f"Loaded dictionary keys order: {list(loaded_dict.keys())}") # Dump a dictionary with explicit order ordered_data = OrderedDict([ ('first_key', 'value_1'), ('second_key', 'value_2'), ('third_key', 'value_3') ]) dumped_yaml = yaml.safe_dump(ordered_data, sort_keys=False) print(f"\nDumped YAML with order preserved:\n{dumped_yaml}")
Debug
Known issues
gotchaFor Python 3.7 and later, standard dictionaries (`dict`) inherently preserve insertion order. This reduces the primary benefit of `oyaml` for these Python versions when working with standard `dict`s, though `oyaml` still ensures `OrderedDict`s are used or order is strictly maintained across all operations and Python versions.
fix
Consider if `oyaml` is truly necessary for new code on Python 3.7+, or if direct `PyYAML` usage with `sort_keys=False` for dumping (if order matters for output) is sufficient. For cross-version compatibility or explicit `OrderedDict` requirements, `oyaml` remains useful.
affects: Python 3.7+
breakingIn `oyaml` v0.8, the behavior for `FullLoader` in CPython 3.6+ was changed to deserialize YAML maps into a standard Python `dict` instead of `collections.OrderedDict`. If your application relied on `FullLoader` returning `OrderedDict` on CPython 3.6+ specifically, this change is breaking.
fix
Explicitly cast the loaded dictionary to `OrderedDict` if the specific type is required, or ensure your code is robust to handling either `dict` or `OrderedDict` from `FullLoader`.
affects: v0.8+
gotchaLike PyYAML, it is unsafe to use `yaml.load()` with untrusted input due to the potential for arbitrary code execution. Always prefer `yaml.safe_load()` for security.
fix
Replace all instances of `yaml.load()` with `yaml.safe_load()` when processing input from untrusted sources.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'oyaml'
The 'oyaml' package is not installed in the Python environment where the code is being executed.
fix
Install the oyaml package using pip: `pip install oyaml`
KeyError: '<key_name>'
The code is attempting to access a key in the loaded YAML data that does not exist in the YAML structure.
fix
Ensure the YAML file contains the expected key, or use dictionary's `.get()` method with a default value to prevent the error, e.g., `data.get('key_name', default_value)`
AttributeError: module 'yaml' has no attribute 'FullLoader'
This error typically occurs when using an older version of PyYAML (prior to 5.1) which does not have the `FullLoader` attribute, or when `oyaml`'s patching doesn't correctly expose `FullLoader` from a compatible underlying PyYAML version.
fix
Ensure that PyYAML version 5.1 or newer is installed, as `FullLoader` was introduced in this version. If `oyaml` is imported as `import oyaml as yaml`, verify `oyaml` is up-to-date and compatible with the desired PyYAML version. Often, `yaml.safe_load()` is sufficient and a safer alternative.
Upgrade
Version history
1.0latest on PyPI · released Aug 18, 2020
Audit
Dependencies
PyYAMLrequiredoyaml acts as a patch/wrapper around PyYAML to provide ordered dictionary functionality, requiring PyYAML to be installed.
Agent activity
5 hits · last 30 days
node
4
Resources
oyaml — pip install oyaml · libregistry