Tuesday, May 1, 2012

Python, Data Structures, and XML


By: B. Y., SW Engineer @ G2
I recently encountered a problem where I wanted to deserialize XML into Python data structures to help parse host network and binary data.  Unfortunately every XML parser on the Internet was off just enough from what I needed that I realized I was going to have to "bite the bullet" and write my own version.  In doing so I created a very abstract class to help generate complex Python data structures that I thought I would share.
The jist of it revolves around the old CS stack.  If anyone ever had to implement one, you know there are two dominant methods; push and pop.  Lists in Python have the same concept with append and pop, but I wanted to incorporate values as data structures as well, plus the inclusion of dictionaries.  With that I created a class 'ds' that leverages the basic concept of a stack with added functionality of pushing and popping inner data structures.  It goes a little like this:
 $ i = ds()
 $ i.push(5)
 > [5]
 $ i.push(6)
 > [5, 6]
 $ i.pushl()
 > []
 $ i.push(7)
 > [7]
 $ i.drop()
 > [5, 6, [7]]
 $ i.push(8)
 > [5, 6, [7], 8]
 $ i.pop()
 > 8
 $ i.pop()
 > [7]
 Hopefully you can see with this little example it allows for inner lists (as well as dictionaries) to be created while maintaining the global data structure.  There are a few other methods (pushd, coalesce, and ret) that I didn't cover, but this gives a simplistic breakdown.
Anyways, I thought others who leverage Python could use this class so I've attached the source.  If you have any questions on it, or how I used this to deserialize XML, feel free to shoot us an email at recruiting@g2-inc.com.

No comments:

Post a Comment