[Patches] [ python-Patches-1677872 ] Efficient reverse line iterator

SourceForge.net noreply at sourceforge.net
Wed Apr 11 19:40:49 CEST 2007


Patches item #1677872, was opened at 2007-03-10 08:47
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1677872&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Library (Lib)
Group: Python 3000
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Mark Russell (mark_t_russell)
Assigned to: Nobody/Anonymous (nobody)
Summary: Efficient reverse line iterator 

Initial Comment:
This is an implementation of __reversed__ for the TextIOWrapper type from the new IO interface (see http://docs.google.com/Doc?id=dfksfvqd_1cn5g5m).  It is used as:

    import io
    for line in reversed(io.open(filename)):
          ...

It is efficient (only reads a block at a time) but can handle arbitrary length lines.  It is useful for scanning backwards through big log files, but my main reason for submitting it is as a demonstration of the usefulness of the new IO layers - it works by putting a new buffering layer round the RawIOBase object from the open() call.

It's just a proof of concept, but if there's interest in this I'm happy to write unit tests and documentation.

The patch also makes io.BufferedReader support buffering, and adds a very minimal implementation of io.TextIOBase and io.TextIOWrapper (needed to make io.open() work).



----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2007-04-11 12:40

Message:
Logged In: YES 
user_id=80475
Originator: NO

FWIW, I've wanted something like this for a long time (for scanning log
files in reverse).

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1677872&group_id=5470


More information about the Patches mailing list