Slithering Through Python

Today I am pleased to add a new category to my blog – “Programming”.

I am a programmer. Naturally, I should occasionally write about programming. But there is something which I always found off-putting about doing that. I think, partly, that I have not had anything interesting to say. And partly, I didn’t like the idea of tediously collecting some relevant source code and then writing about it. After all, others who are better than I am have already covered every interesting topic in programming.

But this blog is different, as it was created for the self-serving purpose of helping me to work through and understand new ideas. Hence the name: Working Sandbox.

The idea is that the content, design, or whatever, of this blog could break at any time. And I would still march on with the publishing, ignoring the lapse in quality. (Of course, as a practical matter, I’m not really going to let a lot of things break in any significant way – but knowing that I can is quite empowering.

There was a bug in the previous paragraph. Can you spot it?

So, I recently decided to take up the programming language Python, for the following reasons:

  • It is cross-platform.
  • Everybody I trust seems to think it is a great language.
  • It is “mainstream” enough at this point that it is easy enough to get started without a lengthy research or tool-finding phase.
  • Dropbox was written in Python, and it works pretty darned good!
  • Dynamic Languages Strike Back has been nagging at me, ever since I read it nearly two years ago.

Beyond that, I discovered while writing my first “sandbox” program that the following brief code will pull in all the nodes from an XML file and load them into a Python data structure, ready for use:


from xml.etree.ElementTree import parse
nodes = parse('data.xml')
data = []
for node in nodes.findall('x/path/to/data/nodes'):
    data.append(node.text)    
# "data" is now a list containing the specified nodes from the XML file

Now, my guess is that there is a way to add all the node text to the list without using a for loop. Maybe not. In any event, this short bit strikes me as being simple, short, easily retrievable from memory, etc., etc..

Please note: I have not tested the exact code above – it was hand-typed with a few modifications from an existing Python program I am working on. May have made a typo, or two?

Finally, I realize that if I am going to start posting bits of source code, then I will need to do a better job than what I did here, which was to just wrap the code in the HTML “pre” tag. Any tips on a WordPress plugin that helps with this?

One Reply to “Slithering Through Python”

Comments are closed.