Python fun

Earlier I had mentioned how much fun coding in python is. So I recently needed to do a few standard deviation calculations by inputting numbers the first time, and reading other numbers, one line at a time from a file. I could do this by plugging in numbers in some calculating device, use excel, or … But instead I figured I’d write a few lines of code and have an excuse to try out Eric, a Python IDE. It seems to support ruby as well, and the save as option suggests support for editing Java, javascript, tex, sql, etc. etc. Overall, I found using Eric to be a pleasure. It’s got all the usual stuff, setting breakpoints, stepping through code, project management, and even built-in support for source control (Subversion and cvs). Of course, the very name Eric, is in keeping with the Python theme, being named after Eric Idle of Monty Python fame. Further keeping with the fame, there exists a code refactoring menu item labeled Bicycle Repairman.

So, here’s a little taste of some code I scribbled out in about 5-10 minutes in Python.


def standardDeviation(mean, numberArray):
""" Calculate the standard deviation
Expects the mean to have already been calculated
"""
sumOfSquares = 0.0
for num in numberArray:
sumOfSquares = sumOfSquares + num *num

rootMeanSquare = sumOfSquares/len(numberArray)
return math.sqrt(rootMeanSquare – mean*mean )

def readInput():
print “Enter a white space seperated series of numbers”
numbers = raw_input(“==>”)
return [int(n) for n in numbers.split()]

def readData(theFile):
try:
myfile = open(theFile, ‘r’)
except:
print “Could not open file”
numlist = []
for line in myfile:
try:
numlist.append(int(line))
except:
print “Invalid input in file”

myfile.close()
return numlist

(Having just done the preview, I’ve noticed that the WordPress formatters did not keep my indentations. Those of you who already know Python know what I’m talking about. For the rest, just keep in mind that every block of code, for example, everything after a def and contained within in it, should be indented with respect to the def label. Simply trying to tab or add spaces doesn’t seem to work right on this editor. I’ve got about 2 seconds before needing to drive to work so later on, if I get time, I’ll see if I can make it look right.)

The def label indicates the start of a function. The readData function opens up a file and we’re prepared to print a warning if the file doesn’t exist. We append each number to an array called numlist. Reading the file was trivial. The line “for line in file” does the trick. Each line is stored in the variable line each step through the loop. Not shown here is the fact that later on I use this numbers to calculate a mean and pass the results to the standardDeviation function.

Later on, if I get time and am motivated, I can prettify this up quite a bit. I could encapsulate a lot of this in a full blown python class and provide more statistical calculation functionality, throw in TK to get some graphics and charting capability, and generally create a nice little statistics package. Of course, a lot of that functionality already exists elsewhere, but its always fun to roll your own to see if you can find a different take on things.

Advertisements

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: