Tuesday, May 15, 2012

Python & XML

how to parse XML file
using built-in xml.parsers.expat
steps:
  1. create handlers. eg.: def start_element(name, attrs):
  2. create parser. eg.: p = ParserCreate('utf-8')
  3. link handlers. eg.: p.StartElementHandler = start_element
  4. open file eg.: f = open(filename)
  5. ParseFile(file)
Python IDE summary: Netbeans 6.x looks like better choice than Eclipse in terms of code completion. however, netbeans 7 does not support python plugin. currently using netbeans 6.9.1
no problem in using Netbeans with Panda3D
discover a bug. cannot debug properly in Netbeans 6.9.1. cannot step into a method for multiple PY files. breakpoints do not work in multiple files. can be quite troublesome for debugging.
No issue with Eclipse.

converting from String to int or float using int() or float()
attributes in StartElementHandler is stored in Dictionary. eg.: to access an attribute called x , use attrs['x']

thing to note when using classes and methods, have to keep using 'self' keyword in methods when accessing object properties or methods. same as "this" in java but if you omit it in python, it will default to global variable. 

interesting note: cannot use ( ) parenthesis in "for" statement. eg.: for (x in a): 
instead use "for x in a:"

another interesting note: cannot use the same instance to re-parse another file or string. has to create a new instance of parser to do so. ie. Step 2

No comments: