Thanks! This is a very interesting idea, I'd like to keep this around somehow.

I also see that you noticed a problem with text I/O in the current
design; there's no easy way to implement readline() efficiently. I
want readline() to be as efficient as possible -- "for line in <file>"
should *scream*, like it does in 2.x.

--Guido

On 3/10/07, Mark Russell <[EMAIL PROTECTED]> wrote:
> As an experiment with the new IO library (http://docs.google.com/Doc?
> id=dfksfvqd_1cn5g5m), I added support for walking through the lines
> of a file backwards using the reversed() iterator:
>
>      for line in reversed(open(path)):
>           ...
>
> It works by scanning backwards through the file a block at a time
> (using seek()) but will handle arbitrary length lines.
>
> The patch is at http://www.python.org/sf/1677872.  The code is
> currently directly in Lib/io.py, but in fact the only internal access
> it needs is adding the __reversed__ hook to TextIOWrapper.
>
> It's useful for scanning backwards through large log files, but it's
> also IMHO a nice example of the benefits of the new library.  The
> version of this that I used under python 2.5 had to use os.seek() etc
> on file descriptors, whereas now it just wraps a new buffering class
> (io.TextLineReverser) around the raw IO object.  Among other things
> it makes unit tests simpler - instead of messing around with
> temporary files the tests can do things like:
>
>      b = io.BytesIO(b'one\ntwo\nthree\n')
>      assert list(io.TextLineReverser(b)) == [ 'three\n', 'two\n', 'one
> \n' ]
>
> Mark Russell
>
> _______________________________________________
> Python-3000 mailing list
> Python-3000@python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: 
> http://mail.python.org/mailman/options/python-3000/guido%40python.org
>


-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
_______________________________________________
Python-3000 mailing list
Python-3000@python.org
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to