[Tutor] Re: Python & XML - coding tips

Rex chidorex-pytutor at yahoo.com
Thu May 13 01:49:36 EDT 2004


Beautiful use of the dictionary to eliminate the endless elifs. Great
tip. Thanks Danny.

Rex


From: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
Subject: RE: [Tutor] Python & XML
To: justinstraube at charter.net
Cc: Tutor <tutor at python.org>
Message-ID:
	<Pine.LNX.4.44.0405111506380.15047-100000 at hkn.eecs.berkeley.edu>
Content-Type: TEXT/PLAIN; charset=US-ASCII



On Tue, 11 May 2004 justinstraube at charter.net wrote:


Hi Justin,


Code review time!  *grin* Here are some suggestions:

<snip some>


######
# dispatchTable is a mapping from the tagName we're looking for, to a
# triple (ud_offset, left_slice, right_slice)
dispatchTable = { 'name'         : (1, 6, -7),
                  'numresults'   : (2, 12, -13),
                  'cputime'      : (3, 9, -10),
                  'avecpu'       : (4, 8, -9),
                  'resultsperday': (5, 15, -16),
                  'lastresultime': (6, 16, -17),
                  'regdate'      : (7, 9, -10),
                  'usertime'     : (8, 10, -11),
                  }
for node in item.childNodes:
    if node.nodeType == node.ELEMENT_NODE:
        if node.tagName in dispatchTable:
            offset, left_slice, right_slice = 
dispatchTable[node.tagName]
            ud[offset] = node.toxml()[left_slice : right_slice]
            node.unlink()
        else:
            node.unlink()
######


We can get rid of the repetitive logic, and keep the thing that is 
really
changing

<snip the rest>






More information about the Tutor mailing list