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.