Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python dictionaries into yaml documents using PyYaml

I have two python dictionaries which I want to write to a single yaml file, with two documents:

definitions = {"one" : 1, "two" : 2, "three" : 3}
actions = {"run" : "yes", "print" : "no", "report" : "maybe"}

The yaml file should look like:

--- !define
one: 1
two: 2
three: 3

-- !action
run: yes
print: no
report: maybe
...

Using PyYaml I did not find a clear way to do that. I'm sure there is a simple method, but digging into PyYaml documentation, only got me confused. Do I need a dumper, emitter, or what? And what type of output each of these types produces? Yaml text? yaml nodes? YAMLObject? Anyway I would be grateful for any clarifications.


Following unutbu's answer below, here is the most concise version I could come up with:

DeriveYAMLObjectWithTag is a function to create a new class, derived from YAMLObject with the required tag:

def DeriveYAMLObjectWithTag(tag):
    def init_DeriveYAMLObjectWithTag(self, **kwargs):
        """ __init__ for the new class """
        self.__dict__.update(kwargs)

    new_class = type('YAMLObjectWithTag_'+tag,
                    (yaml.YAMLObject,),
                    {'yaml_tag' : '!{n}'.format(n = tag),
                    '__init__' :  init_DeriveYAMLObjectWithTag})
    return new_class

And here is how to use DeriveYAMLObjectWithTag to get the required Yaml:

definitions = {"one" : 1, "two" : 2, "three" : 3, "four" : 4}
actions = {"run" : "yes", "print" : "no", "report" : "maybe"}
namespace = [DeriveYAMLObjectWithTag('define')(**definitions),
             DeriveYAMLObjectWithTag('action')(**actions)]

text = yaml.dump_all(namespace,
                     default_flow_style = False,
                     explicit_start = True)

Thanks to all those who answered. I seems there's a lack of functionality in PyYaml, and this is the most elegant way to overcome it.

like image 368
Periodic Maintenance Avatar asked Sep 10 '25 11:09

Periodic Maintenance


1 Answers

Well, I'm still looking into automatic comments (couldn't find the docs for that right away) but this should do the trick:

import yaml

definitions = {"one" : 1, "two" : 2, "three" : 3}
actions = {"run" : "yes", "print" : "no", "report" : "maybe"}

output = yaml.dump(actions, default_flow_style=False, explicit_start=True)
output += yaml.dump(definitions, default_flow_style=False, explicit_start=True)

print output

One word of caution, dictionaries are unordered, so the order of your resulting YAML is not guaranteed. If you want order in the house - look at OrderedDict.

like image 180
favoretti Avatar answered Sep 13 '25 01:09

favoretti