ruamel-yaml-clib
ruamel-yaml-clib is a C-based reader, parser, and emitter for the ruamel.yaml library, derived from libyaml. It enhances the performance of ruamel.yaml by providing C extensions for core YAML processing operations. This package was split from ruamel.yaml to facilitate the creation of universal wheels and streamline platform-specific compilation. The current version is 0.2.15.
Warnings
- breaking Starting with `ruamel.yaml` version 0.19.1, the library no longer automatically installs `ruamel-yaml-clib` as a direct dependency. To ensure the C-accelerated backend is installed, users must explicitly install `ruamel.yaml` with the `libyaml` extra, e.g., `pip install ruamel.yaml[libyaml]`.
- gotcha Compilation issues may occur with newer `clang` versions (e.g., >= 16) when building `ruamel-yaml-clib` from source. This typically manifests in environments like FreeBSD where `clang` is the default compiler.
- gotcha When `ruamel-yaml-clib` is active (using the C-based SafeLoader/SafeDumper within `ruamel.yaml`), the `yaml.indent` setting applies uniformly to both mappings and sequences. The pure Python implementation of `ruamel.yaml` offers more granular control over indentation styles.
- deprecated The maintainer has indicated that `ruamel.yaml.clib` will not receive fixes for free-threading issues. `ruamel.yaml.clibz` is the designated replacement for future free-threading support. While `ruamel.yaml.clib` remains functional, this signals a future transition.
Install
-
pip install ruamel-yaml-clib
Imports
- YAML
from ruamel.yaml import YAML
Quickstart
import sys
from ruamel.yaml import YAML
# ruamel.yaml will automatically use ruamel-yaml-clib if available
# For pure Python implementation, use: yaml = YAML(pure=True)
yaml = YAML()
data = {
'name': 'Alice',
'age': 30,
'details': {
'city': 'New York',
'occupation': 'Engineer'
}
}
# Dump to string
import io
string_stream = io.StringIO()
yaml.dump(data, string_stream)
yaml_string = string_stream.getvalue()
print('--- Dumped YAML ---')
print(yaml_string)
# Load from string
loaded_data = yaml.load(yaml_string)
print('--- Loaded Data ---')
print(loaded_data['name'])
print(loaded_data['details']['city'])
# Demonstrate round-trip preservation (if ruamel.yaml is configured for it)
config_yaml_str = """
# My config
key: value # inline comment
list:
- item1
- item2
"""
rt_yaml = YAML()
rt_data = rt_yaml.load(config_yaml_str)
rt_data['list'].append('item3')
output_stream = io.StringIO()
rt_yaml.dump(rt_data, output_stream)
print('--- Round-trip preserved ---')
print(output_stream.getvalue())