GithubHelp home page GithubHelp logo

two files in one dict? about pyyaml-include HOT 8 CLOSED

tanbro avatar tanbro commented on August 10, 2024 1
two files in one dict?

from pyyaml-include.

Comments (8)

tanbro avatar tanbro commented on August 10, 2024

Thanks for the question.

I thinks, such a YAML:

foo:
  !include foo-1.yml
  !include foo-2.yml

is illegal, casue it's neither a mapping nor a sequence.

If want it to be a sequence, we shall write the YAML as:

foo:
  - !include foo-1.yml
  - !include foo-2.yml

it wall be loaded to Python dictionary object:

{
  "foo":
    [
        {"one": "a"},
        {"two": "b"}
    ]
}

from pyyaml-include.

grauschnabel avatar grauschnabel commented on August 10, 2024

But there "foo" is not a sequence, its a list of sequences. That is exactly my problem.
The result I want is

{
   "foo":
      {
         "one": "a",
         "two": "b"
      }
}

from pyyaml-include.

tanbro avatar tanbro commented on August 10, 2024

@grauschnabel Do you mean that:

we have two YAML files to be included:

  • file foo-1.yml:

    one: a
  • file foo-2.yml:

    two: b

and when we write :

foo:
  !include foo-1.yml
  !include foo-2.yml

the excepted data is:

{
   "foo":
      {
         "one": "a",
         "two": "b"
      }
}

Is that?

But the YAML including constructor can not union two mappings into one.

Because the constructor will first parse a including file into a YAML node, then PyYAML puts the node into the doc tree.

So, the including parts of above example are two separated mappings, and can not be joined into one mapping as what they are in the included files literally

i think they will be parsed to something like:

{
   "foo":
      {"one": "a"}
      {"two": "b"}
}

That's illegal.

What's more, PyYAML takes the fragment

!include foo-1.yml
!include foo-2.yml

like:

include("foo-1.yml !include foo-2.yml")

i did not expect that.

from pyyaml-include.

grauschnabel avatar grauschnabel commented on August 10, 2024

I think that

foo:
   bar: 1
   baz: 2

is absolutly legal in yaml. But now I understand how you have built this tool and the I think that it's a bit like a design issue. Maybe a colon could solve the problem, but that would possibly break compatibility to like its now. I thought about

foo:
   !include: bar.yml
   !include: baz.yml

Would do the trick to make pyyaml parse it. Otherwise

include("foo-1.yml !include foo-2.yml")

Could be reparsed. by str.split("!include").

In both cases: We could load bar and baz and then combine it via dict.update() so that it is clear that later loaded keys overwrite earlyer definitions of the same key.

What do you think about that?

from pyyaml-include.

tanbro avatar tanbro commented on August 10, 2024

Thanks for @grauschnabel

foo:
   bar: 1
   baz: 2

is a valid YAML.

i'll try to avoid pyyaml parsing

foo:
   !include: bar.yml
   !include: baz.yml

to include("foo-1.yml !include foo-2.yml")

...

from pyyaml-include.

smurfix avatar smurfix commented on August 10, 2024

You should be able to do it with merging, if you can tolerate some cruft lying around.

one: &one
    !include bar.yml
two: &two
    !include baz.yml
foo:
    <<: [ *one, *two ]

Disclaimer: untested.

from pyyaml-include.

tanbro avatar tanbro commented on August 10, 2024

Thanks @smurfix

But i think that won't work.
Because what returns by the !include constructor is a python object, not a YAML mapping, and can't inherit

from pyyaml-include.

tanbro avatar tanbro commented on August 10, 2024

I think it's not easy to do such a thing in PyYAML's constructor.
Because we can't make it return a document literally.

Perhaps a template engine (eg: Jinja2) is better.

For this issue, we can write a main YAML file with Jinja2 templates in it, and include other files using the template engine.

Here we use Jinja2 together with jinjyaml:

  • main.yml:

    foo: !j2 |
        {% include "child-1.yml" %}
        {% include "child-2.yml" %}
  • child-1.yml:

    '1.1': one
    '1.2': two
  • child-2.yml:

    '2.1':
      2.1.1: three
      2.1.2: four

then parse and render it:

from pprint import pprint
import jinja2
import yaml
from jinjyaml import JinjyamlConstructor, jinjyaml_render

TAG = 'j2'
j2_env = jinja2.Environment(
    loader=jinja2.FileSystemLoader('path/of/the/yaml/files')
)
constructor = JinjyamlConstructor(env=j2_env)
yaml.add_constructor('!{}'.format(TAG), constructor)

with open('main.yml') as fp:
    doc = yaml.load(fp, yaml.Loader)
print(doc)

data = jinjyaml_render(doc, yaml.Loader)
pprint(data)

we'll get

{'foo': <jinjyaml.tagobject.JinjyamlObject object at 0x0000027AB80F0250>}
{'foo': {'1.1': 'one',
         '1.2': 'two',
         '2.1': {'2.1.1': 'three', '2.1.2': 'four'}}}

from pyyaml-include.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.