Python-Ref > Object oriented programming > Real world example > Setting up the cookbook
 
 

^^->

Setting up the cookbook

The following code shows a simple implementation of a cookbook. It does not do much at present. It is capable of reading and writing xml files and creating new recipes using an object oriented approach.
Expand/Shrink
<cookbook>
  <recipe>
    <title>Cheese cake</title>
    <text>Here comes the description for preparation of a cheese cake.</text>
  </recipe>
  <recipe>
    <title>Rosted duck</title>
    <text>Here we describe how to prepare a roasted duck.</text>
  </recipe>
</cookbook>
Zdroj: (cookbook1-1.py)
  1   import sys
  2   import xml.dom.minidom as dom
  3   
  4   class Recipe( object):
  5   
  6     def __init__( self, title="", text=""):
  7       self.title = title
  8       self.text = text
  9   
 10     def __str__( self):
 11       return "Recipe: %s" % self.title
 12   
 13     def read_dom_element( self, e):
 14       titles = e.getElementsByTagName( "title")
 15       if not titles:
 16         raise ValueError( "Could not find title in the dom element")
 17       elif len( titles) > 1:
 18         print >> sys.stderr, "Warning: more than one title in dom element\n", titles
 19       self.title = get_all_text_from_element( titles[0])
 20       texts = e.getElementsByTagName( "text")
 21       if texts:
 22         self.text = get_all_text_from_element( texts[0])
 23   
 24     def get_dom_element( self, doc):
 25       el = doc.createElement( "recipe")
 26       title_el = doc.createElement( "title")
 27       title_el.appendChild( doc.createTextNode( self.title))
 28       el.appendChild( title_el)
 29       text_el = doc.createElement( "text")
 30       text_el.appendChild( doc.createTextNode( self.text))
 31       el.appendChild( text_el)
 32       return el
 33   
 34   
 35   class CookBook( object):
 36   
 37     def __init__( self):
 38       self.recipes = []
 39   
 40     def __str__( self):
 41       return "Cookbook: %d recipes" % len( self.recipes)
 42   
 43     def read_xml_file( self, filename):
 44       doc = dom.parse( filename)
 45       for el in doc.getElementsByTagName( "recipe"):
 46         rec = Recipe()
 47         rec.read_dom_element( el)
 48         self.recipes.append( rec)
 49   
 50     def write_xml_file( self, filename):
 51       doc = dom.Document()
 52       root = doc.createElement( "cookbook")
 53       doc.appendChild( root)
 54       for rec in self.recipes:
 55         e = rec.get_dom_element( doc)
 56         root.appendChild( e)
 57       f = file( filename, "w")
 58       f.write( doc.toxml())
 59   
 60   
 61   # help functions
 62   
 63   def get_all_text_from_element( el):
 64       text = ""
 65       for ch in el.childNodes:
 66           if isinstance( ch, dom.Element):
 67               text += get_all_text_from_element( ch)
 68           if isinstance( ch, dom.Text):
 69               text += ch.data
 70       return text
 71       
 72   # end of help functions
 73   
 74   
 75   if __name__ == "__main__":
 76     c = CookBook()
 77     c.read_xml_file( "infiles/cookbook1.xml")
 78     print c
 79     for rec in c.recipes:
 80       print rec
 81     new = Recipe( "Potato dumplings")
 82     new.text = "Preparation instructions for czech potato dumplings"
 83     c.recipes.append( new)
 84     c.write_xml_file( "cookbook1-2.xml")
 85     print c
stdout:
Cookbook: 2 recipes
Recipe: Cheese cake
Recipe: Rosted duck
Cookbook: 3 recipes
<?xml version="1.0" ?>
<cookbook><recipe><title>Cheese cake</title><text>Here comes the description for preparation of a cheese cake.</text></recipe><recipe><title>Rosted duck</title><text>Here we describe how to prepare a roasted duck.</text></recipe><recipe><title>Potato dumplings</title><text>Preparation instructions for czech potato dumplings</text></recipe></cookbook>
Doba běhu: 28.4 ms