Registry / serialization / ruamel-yaml-string

ruamel-yaml-string

JSON →
library0.1.1pypypi✓ verified 87d ago

ruamel.yaml.string is a plugin for the `ruamel.yaml` YAML parser/emitter library. It extends the `ruamel.yaml.YAML` instance by adding `dump_to_string` (and its alias `dumps`) methods, allowing users to serialize YAML documents directly into a Python string instead of writing to a file-like object. The current version is 0.1.1, last updated in May 2023, and its release cadence is tied to `ruamel.yaml` and bug fixes.

pip install ruamel.yaml.string
INSTALL
IMPORT
SIG · RUAMEL-YAML-STRING
R
ruamel-yaml-string
serializationpythonv0.1.1
Install
1.9s avg
Import
42ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.1 · 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.920 runs
installs and imports cleanly · install 0.0s · import 0.045s · 18.9MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.9s · import 0.039s · 19MB
17MB installed
● package 17MB
Code
Verified usage

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

YAML
from ruamel.yaml import YAML
from ruamel.yaml.string import YAML
The `YAML` class is part of the main `ruamel.yaml` library. The `ruamel.yaml.string` plugin adds methods to its instances, but does not provide its own `YAML` class for direct import.

This quickstart demonstrates how to initialize `ruamel.yaml.YAML` with `typ=['rt', 'string']` to enable the `dump_to_string` and `dumps` methods, which return the YAML document as a Python string. It also shows the `add_final_eol` parameter for controlling the trailing newline behavior.

from ruamel.yaml import YAML import sys # Instantiate YAML with the 'string' type to enable dump_to_string/dumps yaml = YAML(typ=['rt', 'string']) data = { 'name': 'John Doe', 'age': 30, 'hobbies': ['reading', 'hiking', 'coding'], 'address': { 'street': '123 Main St', 'city': 'Anytown' } } # Dump to string using dump_to_string yaml_string = yaml.dump_to_string(data) print('YAML as string (dump_to_string):') print(yaml_string) # Dump to string using dumps (alias for dump_to_string) yaml_string_alias = yaml.dumps(data, add_final_eol=True) print('\nYAML as string (dumps with final EOL):') print(yaml_string_alias)
Debug
Known issues
gotchaThe `dump_to_string` and `dumps` methods do not add a final newline to the output string by default. This differs from `PyYAML.dump` and `print()` behavior, which often add a newline.
fix
To add a final newline, use `yaml.dump_to_string(data, add_final_eol=True)` or `yaml.dumps(data, add_final_eol=True)`.
affects: 0.1.0+
gotchaFor maximum efficiency when dumping YAML, especially for large documents, directly writing to a stream (e.g., `sys.stdout` or a file handle) using `yaml.dump(data, sys.stdout)` is more efficient than first generating a string with `yaml.dumps(data)` and then printing it.
fix
Prefer `yaml.dump(data, output_stream)` for performance-critical scenarios where the output stream is known.
affects: 0.1.0+
gotchaThe `dump_to_string` and `dumps` methods are only available on a `ruamel.yaml.YAML` instance when initialized with `typ=['rt', 'string']`. If `typ` is not specified or does not include `'string'`, these methods will not be available.
fix
Ensure your `YAML` instance is created as `yaml = YAML(typ=['rt', 'string'])` to enable the string dumping functionality.
affects: 0.1.0+
Errors
Common errors & fixes
AttributeError: 'YAML' object has no attribute 'dump_to_string'
The `ruamel.yaml.YAML` instance was not initialized with `typ=['rt', 'string']` to enable the plugin's methods.
fix
Initialize your YAML object with `yaml = YAML(typ=['rt', 'string'])`.
TypeError: dump() takes 2 positional arguments but 3 were given
Attempting to pass a `data` object and a stream *and* expecting a string return. The standard `yaml.dump()` method writes to a stream and returns `None`, while `dump_to_string`/`dumps` return a string.
fix
If you want a string, use `yaml_string = yaml.dumps(data)`. If you want to dump to a file, use `yaml.dump(data, file_handle)`.
Upgrade
Version history
0.1.1latest on PyPI · released May 2, 2023
Audit
Dependencies
ruamel.yamlrequiredThis library is a plugin for ruamel.yaml and depends on its core functionality.
Agent activity
6 hits · last 30 days
node
6
Resources
ruamel-yaml-string — pip install ruamel-yaml-string · libregistry