From: Sean <sma...@gm...> - 2010-03-16 17:09:17
|
I apologize if this is posted multiple times. I had a little trouble with the mailing list and google groups. Hopefully I have it figured out now. I have a simple python class containing 2 dicts. I'd like to > represent an instance of the class as two yaml documents, with the --- > document start token separating the 2 dicts. I think I need to > redefine to_yaml to do this, but I'm not really sure how to get both > documents into the stream. > > import yaml > class Info(yaml.YAMLObject): > yaml_tag = u'Info' > def __init__(self): > self.firstDoc={1: 'one', 2: 'two', 3: 'three'} > self.secondDoc={4: 'four', 5: 'five'} > > @classmethod > def to_yaml(cls,dumper,data): > return dumper.represent_mapping(tag=u'tag:yaml.org, > 2002:map',mapping=data.firstDoc) > > if __name__ == "__main__": > print yaml.dump(Info(),default_flow_style=False) > > > The output from the above is: > 1: one > 2: two > 3: three > > > I would like output like: > --- > 1: one > 2: two > 3: three > --- > 4: four > 5: five > > Any ideas or pointers? > > Thanks, > Sean |
From: Kirill S. <xi...@ga...> - 2010-03-16 18:25:07
|
Sean wrote: > > > I have a simple python class containing 2 dicts. I'd like to > represent an instance of the class as two yaml documents, with the --- > document start token separating the 2 dicts. I think I need to > redefine to_yaml to do this, but I'm not really sure how to get both > documents into the stream. > > import yaml > class Info(yaml.YAMLObject): > yaml_tag = u'Info' > def __init__(self): > self.firstDoc={1: 'one', 2: 'two', 3: 'three'} > self.secondDoc={4: 'four', 5: 'five'} > > @classmethod > def to_yaml(cls,dumper,data): > return dumper.represent_mapping(tag=u'tag:yaml.org > <http://yaml.org>, > 2002:map',mapping=data.firstDoc) > > if __name__ == "__main__": > print yaml.dump(Info(),default_flow_style=False) > You don't need to inherit your class from YAMLObject to do that. Just call yaml.dump_all() and pass it a list of the dictionaries: info = Info() print yaml.dump_all([info.firstDoc, info.secondDoc], default_flow_style=False) Thanks, Kirill |
From: Sean <sma...@gm...> - 2010-03-16 19:32:58
|
Thanks for your reply, Kirill. I guess what I am really looking for is a more detailed example of subclassing YAMLObject than what is available in the docs, to get a better idea of what I can do there. Thanks, Sean On Tue, Mar 16, 2010 at 12:08 PM, Kirill Simonov <xi...@ga...> wrote: > Sean wrote: > > >> >> I have a simple python class containing 2 dicts. I'd like to >> represent an instance of the class as two yaml documents, with the --- >> document start token separating the 2 dicts. I think I need to >> redefine to_yaml to do this, but I'm not really sure how to get both >> documents into the stream. >> >> import yaml >> class Info(yaml.YAMLObject): >> yaml_tag = u'Info' >> def __init__(self): >> self.firstDoc={1: 'one', 2: 'two', 3: 'three'} >> self.secondDoc={4: 'four', 5: 'five'} >> >> @classmethod >> def to_yaml(cls,dumper,data): >> return dumper.represent_mapping(tag=u'tag:yaml.org >> <http://yaml.org>, >> >> 2002:map',mapping=data.firstDoc) >> >> if __name__ == "__main__": >> print yaml.dump(Info(),default_flow_style=False) >> >> > You don't need to inherit your class from YAMLObject to do that. Just call > yaml.dump_all() and pass it a list of the dictionaries: > > info = Info() > print yaml.dump_all([info.firstDoc, info.secondDoc], > default_flow_style=False) > > > > Thanks, > Kirill > |
From: Kirill S. <xi...@ga...> - 2010-03-16 19:39:20
|
Sean wrote: > Thanks for your reply, Kirill. I guess what I am really looking for is > a more detailed example of subclassing YAMLObject than what is available > in the docs, to get a better idea of what I can do there. > Well, YAMLObject could be useful if you want to represent your object as a node in a YAML document. For instance, if the object is included to a dictionary or other complex structure and you want to convert the whole structure to a single YAML document, you can use YAMLObject. However your use case seems to be different. Thanks, Kirill |