[Python-3000] encoding hell
Oleg Broytmann
phd at mail2.phd.pp.ru
Mon Sep 4 12:24:13 CEST 2006
On Sun, Sep 03, 2006 at 01:45:28PM -0700, Aahz wrote:
> On Sun, Sep 03, 2006, Marcin 'Qrczak' Kowalczyk wrote:
> > "tomer filiba" <tomerfiliba at gmail.com> writes:
> >>
> >> file("foo", "w+") ?
> >
> > What is a rationale of this operation for a text file?
>
> You want to be able to read the file and write data to it. That argues
> in favor of seek(0) and seek(-1) being the only supported behaviors,
> though.
Sometimes programs need tell() + seek(). Two examples (very similar,
really).
Example 1. I have a program, an email robot that receives email(s) and
marks email addresses in a "database" that is actually a text file:
--- email database file ---
phd at phd.pp.ru
phd at oper.med.ru
--- / ---
The program opens the file in "r+" mode, reads it line by line and
stores the positions of the first character in an every line using tell().
When it needs to mark an email it seek()'s to the stored position and write
'+' mark so the file looks like
--- email database file ---
+phd at phd.pp.ru
phd at oper.med.ru
--- / ---
Example 2. INN (the NNTP daemon) stores (at least stored when I was
using it) information about newsgroup in a text file database. It uses
another approach - it stores info using lines of equal length:
--- newsgroups ---
comp.lang.python 000001234567
comp.lang.python.announce 000000abcdef
--- / ---
Probably INN doesn't use tell() - it just calculates the position using
line length. But a python program needs tell() and seek() for such a file.
Oleg.
--
Oleg Broytmann http://phd.pp.ru/ phd at phd.pp.ru
Programmers don't die, they just GOSUB without RETURN.
More information about the Python-3000
mailing list