The clarity of Python vs. the cloud of Perl

I am learning the Python programming language right now, for many reasons. I need to hone in on a language so that I can write up a script in minutes to do pretty much anything that I need. Things ranging from text file processing, web CGI scripting to generate graphs from data, numerical analysis, and so on. This leaves me with a large number of options such as C++, Java, MATLAB, Python, Perl, and others.

While I am not a programming newbie, my skill level sits somewhere in the moderate area as I have been exposed to many different programming languages at the beginner level. So when I look for programming tutorials, it leaves me somewhere between the beginner books and websites that assume that you barely know how to “download your camera to your PC” and the other side of the spectrum of which the tutorial looks about as exciting as a book of log tables:

logTables
(Flickr user quimby)

That being said, that leaves us moderate programmers who want to learn – stuck at a good and bad part of our learning experience. The part where you need to practice about every day writing real-world scripts over and over and over. So, after many weeks of lagging through with Python and putting off script writing, I attacked my first real Python program. It seems really trivial and would probably be one of the first few homework problems assigned in a programming course – but I will document my learning process nonetheless for the other moderate programmers that are out there.

Why Python? I chose Python after dabbling in each language and reading way too much information on each one and finally just trying them out for myself. Which language would be able to match my high-level idealistic mind but still be practical enough to have some power and force behind it? Well, take a look at my example program below. I wanted a program that would take in values from a CSV (comma-separated value) file, loop through a template file, and output new text files with the data from the CSV file’s rows in each output file.

For the more visually oriented:

pythonrep.png

I actually had my roommate last summer help me out with a Perl version of this program, and for comparison, here it is:

#!/usr/bin/perl

if(@ARGV < 2)
{
print "Usage: extractData <csv> <template>\n";
exit;
}</template></csv>

my ($file, $templateFile) = @ARGV;
my $lastTest = "";
my $templateString = `cat $templateFile`;
open DF, "&lt; $file";

while(<df>)
{
my $line = $_;
next unless($line =~ m/(^IT)|(^\,)/);
my ($test, undef, undef, $tray, undef, $cabletype) = split /\,/, $line;
$test = $lastTest if($test eq "");
$test =~ s/IT//g;</df>

$lastTest = $test;

print "test: $test; tray: $tray.\n";

$test = sprintf("%02d", $test);

(my $toPrint = $templateString) =~ s/TEST/$test/g;
$toPrint =~ s/TRAY/$tray/g;
$toPrint =~ s/CABLETYPE/$cabletype/g;

my $outFile = "CAROLFIRE_IT_" . $test . "_Tray_" . $tray . ".fds";

open OF, "&gt; $outFile";
print OF $toPrint;
close OF;
}

close DF;

Then, here is the version that I wrote last night using Python:

"""Module docstring.
Usage: python fdscsv.py <csv> <template> <output>
"""</output></template></csv>

import csv, sys, os, re

arguments = sys.argv
input = csv.reader(open(arguments[1],"r"))
template = open(arguments[2], "r")
lines = template.readlines()
counter = 1

for i, j, k in input:
output = open(arguments[3] + str(counter) + ".fds", "w")
for line in lines:
output.write(line.replace("IREP,JREP,KREP",(str(i) + "," + str(j) + "," + str(k))))
counter += 1
output.close()

Now, I realize that the function of the scripts are slightly different while the primary CSV functionality that I illustrated above still remains. I am not going for a line-by-line comparison here. I do want you to just look over the code and see which one makes more sense to your mind. For me, the Python is so easy to read and almost natural to understand while the Perl takes some serious brainpower for me to decode.In conclusion, I just wanted to show where I am at in learning the Python language. It really is enjoyable at this point for me when compared to learning Perl, which was just painful for me. So I hope to add to the resounding praise of Python by posting these examples for other programmers who may be stuck in the intermediate phase of their learning and need a little push of motivation to continue on.

Finally, if you are interested in what the Python code is actually doing, here is my commented version. Thanks for reading.

"""Module docstring.
Usage: python fdscsv.py <csv> <template> <output>
"""</output></template></csv>

import csv, sys, os, re

# Reads the arguments into a list
arguments = sys.argv

# Reads in the input csv file using the module csv
input = csv.reader(open(arguments[1],"r"))

# Reads in the template file
template = open(arguments[2], "r")

# Splits the template file into lines
lines = template.readlines()

counter = 1

# Labels the columns for the data read from the csv and loops through the lines in the csv
for i, j, k in input:

# Opens a new file with the user-input name plus a counter and an fds extension
output = open(arguments[3] + str(counter) + ".fds", "w")

# Loops through each line in the template file
for line in lines:

# Replaces strings in the template file with numbers from the csv file
output.write(line.replace("IREP,JREP,KREP",(str(i) + "," + str(j) + "," + str(k))))

# Increments the counter for the filename
counter += 1

output.close()

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

  del.icio.us this!

2 Responses so far »

  1. 1

    brian d foy said,

    March 10, 2008 @ 7:53 pm

    It’s hardly a fair comparison to use a CSV library in Python and compare that to a Perl script which incorrectly does it without a module. You’ll find that the Python and Perl code to do the job are very similar since both would be using a library to handle the details. If you removed the libraries from Python and performed the task with just the core syntax, you’d find Python as mind-bending as the Perl code you show.

    You should note that, no matter the language, the skill is in using existing libraries and creating interfaces so you don’t see the gory details.

  2. 2

    Niyaz PK said,

    March 12, 2008 @ 6:40 am

    Very true. Too obvious.

Comment RSS

Say your words