EdgePenguin Title

Trying Out Python

Posted by Pete Hague on 08 Mar 2012

For most of my life, I’ve been a devoted C coder. I have experience with a wide variety of languages, but often I find myself thinking that what I am doing could be done faster/easier/better in C. I’ve known that in many cases, C isn’t the ideal solution - but I am a creature of habit. Recently, however, I’ve decided to delve into Python. Unlike C, Python is an interpreted language. This inevitably incurs a performance penalty, but blistering run speed is not always the top priority.

I had a simple task to do - grab a bunch of data from some files, and save it into a new set of .csv files. The data sets are not large - I’d previously done this by hand in a spreadsheet - but I wanted to have some code to do this so that if I came back to this project after some time (or someone else tried to replicate my work) I would have an exact record of the process. Speed was not a priority, but accuracy and readability of the code was.

One of the first things I noticed about Python was the lack of block delimiters (the ‘{‘ and ‘}’ characters in C.) Instead, code blocks are delimited by indentation. I found this quite disconcerting, as it felt as if I were not in control of the layout of the code. This is not a major criticism, and I suspect it is something I can adapt to reasonably quickly.

As is the case most of the time when I’m learning to do something new, I frequently searched the Internet to find tutorials and bits of code, Guardian reporter style - although I like to think I have a bit more understanding of what is actually going on than her. The online documentation of Python isn’t ideal as far as I’m concerned - for C/C++ matters, for instance, I usually visit the excellent http://www.cplusplus.com - but I could find no such equivalent for Python. I may need to go and purchase a book.

Here is part of the code I produced, and I would welcome constructive criticism::

def parseFile(inName, outName, pos0, pos1, rSplit, errOn, errName, epos0, epos1):
    pos0=(pos0*2)-1
    pos1=(pos1*2)-1
    inputFile = open(inName, "r")
    outputFile = open(outName, "w")
    if errOn>0
        errorFile=open(errName, "w")
    for line in inputFile:
        if not line.startswith("!"):
            tokens = rSplit.split(line),
            outputFile.write('{0},{1}\n'.format(tokens[0][pos0],tokens[0][pos1]))
            if errOn>0:
                errorFile.write('{0},{1}\n'.format(tokens[0][epos0],tokens[0][epos1]))

rSplit is a regular expression I defined globally to extract numbers from the file.

Even though this was my first attempt at this language, I found that I wasn’t producing many errors. A lot of things I tried (even things I guessed rather than looked up online) worked first time, which seems to indicate the language is reasonably intuitive.

As for my final verdict - well, yesterday I decided to add RSS to the blog, and based on my experience with my first Python program I decided to use the language again to produce an .xml file from all my blog entries, and I plan to convert all the static page generation I use to Python.


1 Comment

Posted by Casey on 14 Apr 2012

The people in #telepathy nchanel on freenode turned out to be kind and helpful. They pointed me to telepathy-butterfly, which is implemented in python. Now, that's not a tutorial, but it could be nice sample code to give a look to.If you're on ubuntu, downloading the source is as simple as apt-get source telepathy-butterfly



Name (required):
Email (required):
URL (optional):
Enter the most central three letters of the word "spambot"
Comment: