Skip to content

Instantly share code, notes, and snippets.

@perlpunk
Last active August 13, 2024 15:00
Show Gist options
  • Save perlpunk/be76e623963a83a905a3072eb4579ec1 to your computer and use it in GitHub Desktop.
Save perlpunk/be76e623963a83a905a3072eb4579ec1 to your computer and use it in GitHub Desktop.
pyyaml-include
import yaml
def include_constructor(loader, node):
"""Include content at node."""
v = open(node.value).read()
return yaml.load(v, Loader=loader.__class__)
yaml.add_constructor("!include", include_constructor, Loader=yaml.SafeLoader)
string = """
x: &x !include "abc.yaml"
y: *x
"""
data = yaml.safe_load(string)
print(data)
{'x': {'a': 1, 'b': 2}, 'y': {'a': 1, 'b': 2}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment