Menu

Tree [5a60b4] default tip /
 History

Read Only access


File Date Author Commit
 .hgtags 2023-05-02 Anthon van der Neut Anthon van der Neut [5a60b4] Added tag 0.1.1 for changeset 67b98a1fdfe2
 LICENSE 2023-05-02 Anthon van der Neut Anthon van der Neut [67b98a] added typing info, corrected README references ...
 README.rst 2023-05-02 Anthon van der Neut Anthon van der Neut [67b98a] added typing info, corrected README references ...
 _README.ryd 2023-05-02 Anthon van der Neut Anthon van der Neut [67b98a] added typing info, corrected README references ...
 __init__.py 2023-05-02 Anthon van der Neut Anthon van der Neut [67b98a] added typing info, corrected README references ...
 __plug_in__.py 2023-05-02 Anthon van der Neut Anthon van der Neut [67b98a] added typing info, corrected README references ...
 setup.py 2023-05-02 Anthon van der Neut Anthon van der Neut [67b98a] added typing info, corrected README references ...
 tox.ini 2023-05-02 Anthon van der Neut Anthon van der Neut [67b98a] added typing info, corrected README references ...

Read Me

ruamel.yaml.string

version:0.1.0
updated:2022-03-17

This plug-in adds a method dump_to_string (and its equivalent dumps) to the ruamel.yaml.YAML instance that returns the document as a Python string.

Installation

The module can be installed from PyPI using:

pip install ruamel.yaml.string

This module is dependent on ruamel.yaml, so you do not have to explicitly make your module depending on both.

Usage

import ruamel.yaml

yaml = ruamel.yaml.YAML(typ=['rt', 'string'])
data  = dict(abc=42, help=['on', 'its', 'way'])
print('retval', yaml.dump_to_string(data))
print('>>>> done')

which gives:

retval abc: 42
help:
- on
- its
- way
>>>> done

Please note that there is no final newline added to the string that is returned. That the >>>> done is on the next line is caused by the print() function adding a newline by default. (This is different from using PyYAML's dump, as e.g. the output of various print dump(data) examples in the documentation (2021) fail to clearly show the double newline at the end of the examples output. It is similar to json.dumps(data, indent=2) that returns a string without final newline.)

Alternatively the first call to print could be:

print('retval', yaml.dump_to_string(data, add_final_eol=True), end='')

with the same effect.

.dump_to_string() can be shortened to .dumps()

Directly writing to ``sys.stdout`` using ``yaml.dump(data, sys.stdout)`` is much more efficient than ``print``-ing the result of ``yaml.dumps(data)``

ChangeLog

NEXT:
  • typing added and readme corrections
0.1.0 (2022-03-17):
  • initial plug-in version
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.