title: Python / YAML: How to initialize additional objects not just from the YAML file, within loadConfig?
body: <p>I have what I think is a small misconception with loading some YAML objects. I defined the class below.</p>
  
  <p>What I want to do is load some objects with the overridden <code>loadConfig</code> function for <code>YAMLObjects</code>. Some of these come from my <code>.yaml</code> file, but others should be built out of objects loaded from the YAML file.</p>
  
  <p>For instance, in the class below, I load a member object named "keep" which is a string naming some items to keep in the region. But I want to also parse this into a list and have the list stored as a member object too. And I don't want the user to have to give both the string and list version of this parameter in the YAML.</p>
  
  <p>My current work around has been to override the <code>__getattr__</code> function inside <code>Region</code> and make it create the defaults if it looks and doesn't find them. But this is clunky and more complicated than needed for just initializing objects.</p>
  
  <p>What convention am I misunderstanding here. Why doesn't the <code>loadConfig</code> method create additional things not found in the YAML?</p>
  
  <pre><code>import yaml, pdb
  
  class Region(yaml.YAMLObject):
      yaml_tag = u'!Region'
  
      def __init__(self, name, keep, drop):
          self.name = name
          self.keep = keep
          self.drop = drop
  
          self.keep_list = self.keep.split("+")
          self.drop_list = self.drop.split("+")
          self.pattern = "+".join(self.keep_list) + "-" + "-".join(self.drop_list)
      ###
  
      def loadConfig(self, yamlConfig):
          yml = yaml.load_all(file(yamlConfig))
          for data in yml:
  
              # These get created fine
              self.name = data["name"]
              self.keep = data["keep"]
              self.drop = data["drop"]
  
              # These do not get created.
              self.keep_list = self.keep.split("+")
              self.drop_list = self.drop.split("+")
              self.pattern = "+".join(self.keep_list) + "-" + "-".join(self.drop_list)
      ###  
  ### End Region
  
  if __name__ == "__main__":
      my_yaml = "/home/path/to/test.yaml"
      region_iterator = yaml.load_all(file(my_yaml))
  
      # Set a debug breakpoint to play with region_iterator and
      # confirm the extra stuff isn't created.
      pdb.set_trace()
  </code></pre>
  
  <p>And here is <code>test.yaml</code> so you can run all of this and see what I mean:</p>
  
  <pre><code> Regions:
  
     # Note: the string conventions below are for an
     # existing system. This is a shortened, representative
     # example.
  
     Market1:  
      !Region                 
          name: USAndGB
          keep: US+GB
          drop: !!null 
  
     Market2:
      !Region
          name: CanadaAndAustralia
          keep: CA+AU
          drop: !!null 
  </code></pre>
  
  <p>And here, for example, is what it looks like for me when I run this in an IPython shell and explore the loaded object:</p>
  
  <pre><code>In [57]: %run "/home/espears/testWorkspace/testRegions.py"
  --Return--
  &gt; /home/espears/testWorkspace/testRegions.py(38)&lt;module&gt;()-&gt;None
  -&gt; pdb.set_trace()
  (Pdb) region_iterator
  &lt;generator object load_all at 0x1139d820&gt;
  (Pdb) tmp = region_iterator.next()
  (Pdb) tmp
  {'Regions': {'Market2': &lt;__main__.Region object at 0x1f858550&gt;, 'Market1': &lt;__main__.Region object at 0x11a91e50&gt;}}
  (Pdb) us = tmp['Regions']['Market1']
  (Pdb) us
  &lt;__main__.Region object at 0x11a91e50&gt;
  (Pdb) us.name
  'USAndGB'
  (Pdb) us.keep
  'US+GB'
  (Pdb) us.keep_list
  *** AttributeError: 'Region' object has no attribute 'keep_list'
  </code></pre>