pycontribs / ruyaml

ruyaml is a openly maintained fork of elusive ruamel-yaml package that can be used as as a drop-in replacement.
https://pypi.org/project/ruyaml/
MIT License
73 stars 15 forks source link

feature: Allow dumping to a string without using streams #27

Open lyz-code opened 3 years ago

lyz-code commented 3 years ago

I want to be able to use .dump() to save the output as a string for later processing.

Right now, you need to work with streams. Where the simplest code is:

# Configure YAML formatter
yaml = YAML()
string_stream = StringIO()
yaml.dump({'products': ['item 1', 'item 2']}, string_stream)
source_code = string_stream.getvalue()
string_stream.close()

It would be really nice to be able to do:

yaml = YAML()
source_code = yaml.dump({'products': ['item 1', 'item 2']}, string_stream)

Thanks

pelson commented 1 year ago

Personally I would expect this to be named consistently with the std-lib JSON library, and instead be called dumps (and loads for the load equivalent).