Created
June 12, 2025 16:52
-
-
Save ashiklom/922d5eb384d3be5a0497ff6762f769fc to your computer and use it in GitHub Desktop.
pyyaml vs ruamel behavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from io import StringIO | |
import sys | |
import yaml | |
import ruamel.yaml | |
yaml_example = """ | |
common: &shared_list | |
- 1 | |
- 2 | |
- 3 | |
- 4 | |
shared_desc: &shared_desc "A long description string shared by multiple items" | |
objects: | |
first_object: | |
description: *shared_desc | |
some_list: *shared_list | |
second_object: | |
description: *shared_desc | |
some_list: *shared_list | |
""" | |
dat_yaml = yaml.safe_load(StringIO(yaml_example)) | |
rml = ruamel.yaml.YAML() | |
dat_ruamel = rml.load(StringIO(yaml_example)) | |
yaml.dump(dat_yaml, sys.stdout) | |
# common: &id001 | |
# - 1 | |
# - 2 | |
# - 3 | |
# - 4 | |
# objects: | |
# first_object: | |
# description: A long description string shared by multiple items | |
# some_list: *id001 | |
# second_object: | |
# description: A long description string shared by multiple items | |
# some_list: *id001 | |
# shared_desc: A long description string shared by multiple items | |
rml.dump(dat_ruamel, sys.stdout) | |
# common: &shared_list | |
# - 1 | |
# - 2 | |
# - 3 | |
# - 4 | |
# | |
# | |
# shared_desc: &shared_desc A long description string shared by multiple items | |
# objects: | |
# first_object: | |
# description: *shared_desc | |
# some_list: *shared_list | |
# second_object: | |
# description: *shared_desc | |
# some_list: *shared_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment