perl to python

Ville Vainio ville at spammers.com
Wed May 12 08:44:50 EDT 2004


>>>>> "Kirk" == Kirk Job-Sluder <kirk at eyegor.jobsluder.net> writes:

    Kirk> And here is the fundamental question.  Why should I spend my
    Kirk> time writing a module in python to emulate another tool,
    Kirk> when I can simply use that other tool?  Why should I, as a

Perhaps you won't; but someone who isn't already proficient with the
tool may rest assured that learning the tool really isn't worth his
time. awk and sed fall into this category.

    Kirk> resarcher who must process large quantities of data, spend
    Kirk> my time and my employer's money reinventing the wheel?

You are not reinventing the wheel, you are refactoring it :). I don't
think your employer minds you spending 15 extra minutes creating some
tool infrastructure, if it allows you to drop awk/sed dependency that
your co-workers then won't need to learn.

    Kirk> I think you are missing a key step.  You are starting off
    Kirk> with a solution (python scripts and modules) and letting it
    Kirk> drive your needs analysis.  I don't get paid enough money to
    Kirk> write pythonic solutions to problems that have already been
    Kirk> fixed using other tools.

I find writing pythonic tools a relaxing deversion from my everyday
work (cranking out C++), so I don't really mind. As long as the time
spent is within 5 minutes - 1 hour range.

    Kirk> I'll be more specific about the challenge.  Using only stock
    Kirk> python with no added modules, give me a script that
    Kirk> pretty-prints a character-delimted file using one variable
    Kirk> assignment, and one function.

    Kirk> Here is the solution in awk:
    Kirk> BEGIN { FS="\t" } 
    Kirk> {printf("%s %s %s %s", $4, $3, $2, $1)}


for line in open("file.txt"):
  fields = line.strip().split("\t")
  print "%s %s %s" % (fields[2], fields[1], fields[0])

(untested code warning)

Time taken: 56 seconds, give or take. Roughly the same I would expect
writing your awk example took, and within the range I expect your
employer would afford ;-).

Technically it does two variable assignments, but I don't see the
problem (ditto with function calls - who cares?) Assignment is
conceptually cheap. It doesn't seem any less readable or elegant than
your awk example. I could have maybe lost a few seconds by using
shorter variable names.

-- 
Ville Vainio   http://tinyurl.com/2prnb



More information about the Python-list mailing list