Skip to content

Instantly share code, notes, and snippets.

@sethwoodworth
Created February 19, 2013 03:10
Show Gist options
  • Save sethwoodworth/4982765 to your computer and use it in GitHub Desktop.
Save sethwoodworth/4982765 to your computer and use it in GitHub Desktop.
class Ebook():
def set_author(self, element):
self.author = element.text
def set_pubdate(self, element):
self.pubdate = element.text
lookup_table = {
'pg:author ...': self.set_author,
'pg:pubdate ...': self.set_pub_date
}
def parse_ebook(etree_book):
new_book = Ebook()
for child in etree_book.getchildren():
# get the signiture of the element, ie 'pg:author' via child.tag
tag = child.tag
# get the function out of the lookup_table that matches 'tag'
func = new_book.lookup_table[tag]
# call the function on the child element
func(child)
# or all in one line
new_book.lookup_table[child.tag](element)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment