Peeking into the cache in the "linecache" module

Eddie and Babs kca17 at dial.pipex.com
Tue Apr 3 12:45:56 EDT 2001


I am implementing a class which allows you to subscript a text file object
(so you can write "textfile[3]" and it returns line number 3 from the file
as a string). This class is really just a wrapper around the "linecache"
module.

For consistency with other sequences, I should allow negative indices, for
which I will need to know the size (ie, number of lines in the file). This
information IS stored by the "linecache" module, but there is no method to
access it.

Does anyone know if "linecache" deliberately hides the details of the cache,
or are you expected to access them directly (eg, by saying
"len(linecache.cache[filename][2])" to get the number of lines from the line
list)?

Also, why does linecache.getline index lines from 1 rather than 0 ? It
subtracts 1 from the line number you supply and uses that as an index into a
list (which is indexed from zero, of course). Seems inconsistent to me...

Thanks,
&.




More information about the Python-list mailing list