ruamel-yaml-clib

0.2.15 · active · verified Sat Mar 28

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

Install

Imports

Quickstart

This quickstart demonstrates basic loading and dumping of YAML using `ruamel.yaml`. If `ruamel-yaml-clib` is installed, `ruamel.yaml` will use its C extensions for improved performance without requiring any changes to user code. To explicitly use the pure Python implementation, instantiate `YAML(pure=True)`.

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())

view raw JSON →