Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your question does not ask for a Python solution, but here is one using <a href="https://github.com/yaml/pyyaml" rel="noreferrer">PyYAML</a>.</p> <p>PyYAML allows you to attach custom constructors (such as <code>!include</code>) to the YAML loader. I've included a root directory that can be set so that this solution supports relative and absolute file references.</p> <h1>Class-Based Solution</h1> <p>Here is a class-based solution, that avoids the global root variable of my original response.</p> <p>See this <a href="https://gist.github.com/joshbode/569627ced3076931b02f" rel="noreferrer">gist</a> for a similar, more robust Python 3 solution that uses a metaclass to register the custom constructor.</p> <pre class="lang-python prettyprint-override"><code>import yaml import os.path class Loader(yaml.SafeLoader): def __init__(self, stream): self._root = os.path.split(stream.name)[0] super(Loader, self).__init__(stream) def include(self, node): filename = os.path.join(self._root, self.construct_scalar(node)) with open(filename, 'r') as f: return yaml.load(f, Loader) Loader.add_constructor('!include', Loader.include) </code></pre> <p>An example:</p> <p><strong><code>foo.yaml</code></strong></p> <pre><code>a: 1 b: - 1.43 - 543.55 c: !include bar.yaml </code></pre> <p><strong><code>bar.yaml</code></strong></p> <pre><code>- 3.6 - [1, 2, 3] </code></pre> <p>Now the files can be loaded using:</p> <pre class="lang-python prettyprint-override"><code>&gt;&gt;&gt; with open('foo.yaml', 'r') as f: &gt;&gt;&gt; data = yaml.load(f, Loader) &gt;&gt;&gt; data {'a': 1, 'b': [1.43, 543.55], 'c': [3.6, [1, 2, 3]]} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload