ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-list
python-list
Re: Help with optimisation
by Bruno Desthuilliers other posts by this author
Aug 13 2007 11:31AM messages near this date
Help with optimisation | Re: Help with optimisation
special_dragonfly a écrit :
>  Hello,
(snip)
>  The function doesn't return anything, but it's called often enough and 
>  depending on the optimisation I'll be able to use the same style in other 
>  areas of the program.
>  
>  previous code:
>  def CreatePerson(text_buffer):
>      dom=xml.dom.minidom.parseString(text_buffer)
>      reflist = dom.getElementsByTagName('Country')
>      Country = reflist[0].firstChild.nodeValue
>      reflist = dom.getElementsByTagName('Age')
>      Age = reflist[0].firstChild.nodeValue
>      reflist = dom.getElementsByTagName('Surname')
>      Surname = reflist[0].firstChild.nodeValue
>      reflist = dom.getElementsByTagName('Forename')
>      Forename = reflist[0].firstChild.nodeValue
>      cursor.execute('INSERT INTO Person VALUES(?,?,?)', (Forename + "-" + 
>  Surname, Age, Country))
>      connection.commit()
>  
>  I've changed it now to this:
>  def CreatePerson(text_buffer):
>      dom=xml.dom.minidom.parseString(text_buffer)
>      elements=['Country','Age','Surname','Forename']
>      Values=[]
>      for element in elements:
>          reflist=dom.getElementsByTagName(element)
>          Values.append(reflist[0].firstChild.nodeValue)
>          # I can get away with the above because I know the structure of the 
>  XML
>      cursor.execute('INSERT INTO Person 
>  VALUES(?,?,?)',(Forename+"-"+Surname,Age,Country))
>      connection.commit()

A common python optimisation trick is to stote local references to save 
on attribute lookup time, ie:

# local ref to parseString
import dom
dom_parseString=xml.dom.minidom.parseString

def CreatePerson(text_buffer):
     dom = dom_parseString(text_buffer)
     elements=['Country','Age','Surname','Forename']
     values=[]
     getElementByTagName = dom.getElementsByTagName
     for element in elements:
         reflist = getElementsByTagName(element)
         values.append(reflist[0].firstChild.nodeValue)


But as Alex already pointed out, you'd be better using (c)ElementTree.

>  They both seem ugly IMO (read: longer than intuitively necessary),

I'd say this is a common problem with XML :-/
-- 
http://mail.python.org/mailman/listinfo/python-list
Thread:
Special_dragonfly
Bruno Desthuilliers
Alex Martelli

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved