perl to python

Kirk Job-Sluder kirk at eyegor.jobsluder.net
Wed May 12 05:50:03 EDT 2004


On 2004-05-12, Ville Vainio <ville at spammers.com> wrote:
>>>>>> "Kirk" == Kirk Job-Sluder <kirk at eyegor.jobsluder.net> writes:
>
>    Kirk> I've not found this to be the case due to Python's emphasis
>    Kirk> on being explicit rather than implicit.  My emulation of
>    Kirk> "perl -pi -e" was about 24 lines in length.  Even with the
>    Kirk> improvement there is still 10 times as many statements where
>    Kirk> things can go wrong.
>
> That's when you create a module which does the implicit looping. Or a
> python script that evals the passed expression string in the loop.

Except now you've just eliminated portability, one of the main arguments
for using python in the first place. 

And here is the fundamental question.  Why should I spend my time
writing a module in python to emulate another tool, when I can simply
use that other tool?  Why should I, as a resarcher who must process
large quantities of data, spend my time and my employer's money
reinventing the wheel?  


>    Kirk> It is really hard to be more trivial than a complete program in one
>    Kirk> command line.
>
> As has been stated elsewhere, you can do the trick on the command
> line. The effort to create the required tools only needs to be paid
> once.

One can do the trick one one command line in python.  However that
command line is an ugly inelegant hack that eliminates the most
important advantage of python: clear, easy to understand code.  In
addition, that example still required 8 python statements compared to
two in awk.

> However, many times it won't matter whether the whole program fits on
> the command line. I always do a script into a file and then execute
> it. I just prefer a real editor to command history editing if
> something goes wrong.

Which is what I do as well.  The question is, why should I write 8
python statements to perform a task that I can do in two using awk, or
sed?  Why should I spend 30 minutes writing, testing and debugging a
python script that takes 5 minutes to write in awk, or sed taking
advantage of the implicit loops and record splitting.

> I think one should just analyze the need, implement the requisite
> module(s) and the script to invoke the stuff in modules. The needs
> have the habit of repeating themselves, and having a bit more
> structure in the solution will pay off.

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

> The virtual of the implicitness is still arguable.

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

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





More information about the Python-list mailing list