FAQ
Hi,

Currently I m writing an XML parser that processes an xml file using sax,
and I have it working, however I want to make the code of my parser less
cluttered and more readable by other people (including myself). However it
is quite messy at the moment. The main reason is that Python doesnt have a
switch statement.

def startElement(self,name,attributes):
if name == "sbml":
s = Sbml(attributes['xmlns'], attributes['version'],
attributes['level'])
self.sbmlDict['sbml'] = s
elif name == "model":
m = Model(attributes['id'], attributes['name'])
self.sbmlDict['model'] = m
elif name == "listOfCompartments":
self.inListOfCompartments = bool(1)
elif name == "compartment" and self.inListOfCompartments:
c = Compartment(attributes['id'],
attributes['name'])
self.tempList.append(c)
.......................................snip

I would use a dictionary for this, but this would require the use of many
extra methods for each tag name, and this would lead to clutter aswell. Does
anyone have any suggestions for reducing the number of lines and making my
code look neater than a large amount of methods or elif statements.

Many Thanks

Nathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-list/attachments/20061023/d24d5318/attachment.htm

Search Discussions

  • Fredrik Lundh at Oct 23, 2006 at 2:58 pm

    Nathan Harmston wrote:

    However it is quite messy at the moment. The main reason is that Python
    doesnt have a switch statement.
    a switch statement wouldn't save you a single line, so I find it a bit hard
    to believe that it's the main reason...
    def startElement(self,name,attributes):
    if name == "sbml":
    s = Sbml(attributes['xmlns'], attributes['version'],
    attributes['level'])
    self.sbmlDict['sbml'] = s
    elif name == "model":
    m = Model(attributes['id'], attributes['name'])
    self.sbmlDict['model'] = m
    elif name == "listOfCompartments":
    self.inListOfCompartments = bool(1)
    elif name == "compartment" and self.inListOfCompartments:
    c = Compartment(attributes['id'],
    attributes['name'])
    self.tempList.append(c)
    .......................................snip

    I would use a dictionary for this, but this would require the use of many
    extra methods for each tag name, and this would lead to clutter aswell. Does
    anyone have any suggestions for reducing the number of lines and making my
    code look neater than a large amount of methods or elif statements.
    forget about SAX and silly state machines, and use a modern XML library;
    here's some ElementTree inspiration:

    http://effbot.org/zone/element-iterparse.htm#incremental-decoding

    there are several other newer libraries that provide built-in data binding support,
    including lxml.objectify and Amara.

    </F>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedOct 23, '06 at 2:34p
activeOct 23, '06 at 2:58p
posts2
users2
websitepython.org

2 users in discussion

Fredrik Lundh: 1 post Nathan Harmston: 1 post

People

Translate

site design / logo © 2022 Grokbase