Skip to content

Instantly share code, notes, and snippets.

@ashiklom
Created June 12, 2025 16:52
Show Gist options
  • Save ashiklom/922d5eb384d3be5a0497ff6762f769fc to your computer and use it in GitHub Desktop.
Save ashiklom/922d5eb384d3be5a0497ff6762f769fc to your computer and use it in GitHub Desktop.
pyyaml vs ruamel behavior
#!/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