-1

I am writing a python application. I am trying to dump my python object into yaml using PyYaml. I am using Python 2.6 and running Ubuntu Lucid 10.04. I am using the PyYAML package in Ubuntu Package: http://packages.ubuntu.com/lucid/python/python-yaml

My object has 3 text variables and a list of objects. Roughly it is something like this:

ClassToDump:

   #3 text variables
   text_variable_1
   text_variable_2
   text_variable_3
   #a list of AnotherObjectsClass instances
   list_of_another_objects = [object1,object2,object3]


AnotherObjectsClass:
   text_variable_1
   text_variable_2
   text_variable_3

The class that I want to dump contains a list of AnotherObjectClass instances. This class has a few text variables.

PyYaml somehow does not dump the collections in AnotherObjectClass instance. PyYAML does dump text_variable_1, text_variable_2, and text_variable_3.

I am using the following pyYaml API to dump ClassToDump instance:

classToDump = ClassToDump();
yaml.dump(ClassToDump,yaml_file_to_dump)

Does any one has any experience with dumping a list of objects into YAML ?

Here is the actual full code snippet:

def write_config(file_path,class_to_dump):    
    config_file = open(file_path,'w');  
    yaml.dump(class_to_dump,config_file);

def dump_objects():

rule = Miranda.Rule();
rule.rule_condition = Miranda.ALL
rule.rule_setting = ruleSetting
rule.rule_subjects.append(rule1)
rule.rule_subjects.append(rule2)
rule.rule_verb = ruleVerb

write_config(rule ,'./config.yaml');

This is the output :

!!python/object:Miranda.Rule rule_condition: ALL rule_setting: !!python/object:Miranda.RuleSetting {confirm_action: true, description: My Configuration, enabled: true, recursive: true, source_folder: source_folder} rule_verb: !!python/object:Miranda.RuleVerb {compression: true, dest_folder: /home/zainul/Downloads, type: Move File}

zfranciscus
  • 16,175
  • 10
  • 47
  • 53
  • "PyYaml somehow does not dump the collections in AnotherObjectClass instance" -- what collections? So far all that you have said that it contains is three text variables. Please consider showing a small concrete example (executable script) plus its output plus what the expected output is. – John Machin Jul 13 '10 at 03:34
  • I think there is a typo in my original post. I amended my question. – zfranciscus Jul 13 '10 at 03:49
  • Your code snippet is NOT executable; the following names are not defined: yaml, Miranda, and 4 x rule*. You have provided neither actual nor expected output. Do you really expect answerers to know what a Miranda.Rule instance looks like? Try substituting a small class which can be spelled out in an executable Miranda-free script which reproduces the problem. Don't forget actual/expected output. – John Machin Jul 13 '10 at 04:41
  • -1 Still no executable script, no expected output. – John Machin Jul 13 '10 at 07:28

1 Answers1

2

The PyYaml module takes care of the details for you, hopefully the following snippet will help

import sys
import yaml

class AnotherClass:
    def __init__(self):
        pass

class MyClass:
    def __init__(self):
        self.text_variable_1 = 'hello'
        self.text_variable_2 = 'world'
        self.text_variable_3 = 'foobar'
        self.list_of_another_objects = [
            AnotherClass(),
            AnotherClass(),
            AnotherClass()
        ]

obj = MyClass()
yaml.dump(obj, sys.stdout)

The output of that code is:

!!python/object:__main__.MyClass
list_of_another_objects:
- !!python/object:__main__.AnotherClass {}
- !!python/object:__main__.AnotherClass {}
- !!python/object:__main__.AnotherClass {}
text_variable_1: hello
text_variable_2: world
text_variable_3: foobar
Andrew Walker
  • 40,984
  • 8
  • 62
  • 84