xreadlines (was Re: while true: !!!)

Neil Schemenauer nas at arctrix.com
Wed Dec 13 19:43:16 CET 2000


On Wed, Dec 13, 2000 at 11:57:45PM +0000, Jeff Epler wrote:
> In Python, there is an existing precedent for creating a
> special 'iteratable' object where the entire object would not
> fit in memory, the 'xrange' function and its associated object
> type.
> 
> I take this cue to suggest the idea of an 'xreadlines' object.

Its been done before (by me and by other people).  The best
"solution" I came up with was "repeatmodule":

    http://arctrix.com/nas/python/repeatmodule-1.0.tar.gz

Here is an example of its use:

    import sys
    from repeat import repeat
    for line in repeat(sys.stdin.readline):
            ...

and a little documentation on the function:

    repeat.repeat(<function>, [<until>]) -> RepeatObject

    RepeatObjects are sequences that only support the method
    __getitem__.  <function> is called on __getitem__.  If
    <until> is not specified a false value from <function> raises
    an IndexError.  If <until> is specified, if the value is
    equal to <until> then IndexError is raised.

I never did end up using it.  Usually speed doesn't matter that
much.  If would be interesting to see how quickly Python can chew
up a file with my repeat hack and AMK's recent glibc getline
optimization.  Another day perhaps.

  Neil




More information about the Python-list mailing list