Python vs. Perl, which is better to learn?
Aahz
aahz at pythoncraft.com
Thu May 2 00:01:25 EDT 2002
In article <mailman.1020301986.9521.python-list at python.org>,
Terry Hancock <hancock at anansispaceworks.com> wrote:
>
>I've got practical examples: I sometimes use Perl as a "super-sed".
>The original sed's REs are getting quite out of date, so it's kind of
>nice to use Perl's. Doing the same thing in Python is quite feasible,
>but will probably take about an hour longer to make work, because you
>have to get the import right, compile the regex and figure out how to
>get data in and out of the program. All easy stuff, but extra lines of
>code, and if you're thumbfingered with the RE module it takes some time
>to get right.
>
>If the project was going to take five minutes to write in Perl, then
>that's about 1200% overhead. Particularly nasty if the lifetime of use
>is another ten minutes.
My contention is that if something that takes you five minutes in Perl
takes you an hour in Python, you're doing something wrong. Here's a
grep.py that I wrote in ten minutes for the original version, plus
another ten minutes of fiddling to get the current version (I wrote it
for my OSCON tutorial):
from __future__ import generators
import sys, re
def grep(f, regex):
f = file(f)
regex = re.compile(regex)
for line in f:
if regex.search(line):
yield line
if __name__ == '__main__':
regex = re.compile(sys.argv[1])
if len(sys.argv) > 2:
flist = sys.argv[2:]
else:
flist = [sys.stdin]
for f in flist:
for line in grep(f, regex):
sys.stdout.write(line)
Now I've got a superstructure, and all I need is to add an appropriate
regex. Python's regexes are pretty damn similar to Perl's.
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
"I used to have a .sig but I found it impossible to please everyone..." --SFJ
More information about the Python-list
mailing list