Unsung Python modules
Tim Hammerquist
tim at vegeta.ath.cx
Fri Dec 14 17:28:01 EST 2001
Skip Montanaro <skip at pobox.com> graced us by uttering:
>
> Tim> 1) readline (optional built-in)
> Tim> 2) xreadlines
>
> Tim> Two similarly named but unrelated modules have made my
> Tim> whole life much easier. =)
>
> I agree on the readline module (don't have a lot of need to text
> file processing that sys.stdin.readlines() won't handle).
Well, here's the evolution of the textfile iteration:
# Incarnation 1
while 1:
line = fh.readline()
if not line:
break
process(line)
# Incarnation 2
for line in fh.xreadlines(): # same interface as xreadlines mod
process(line)
# Incarnation 3 (py2.2+)
for line in fh:
process(line)
Python's textfile iteration structures improved by orders of
magnitude with each step. Sure, sys.stdin.readlines() will do until
I have to parse a daemon log or flat text db; then there's no good
reason or even advantage to loading the whole file in memory at once.
> It seems like I've travelled back to the dark ages whenever I have
> to use Python without readline or a "real" Bourne shell. Being
> able to recall a line or two from an interactive session from a few
> minutes before (or a few days before) makes day-to-day interaction
> with these tools so much easier. Readline gets slammed for being a
> bloated pig, but it is a highly useful bloated pig... ;-)
Agreed! =)
Tim Hammerquist
--
Tell me -- How's your love life? Killed any girlfriends
recently? Or sentenced any more of them to Hell?
-- Desire to Morpheus, The Sandman
More information about the Python-list
mailing list