-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Efficient reverse line iterator #44691
Comments
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). |
FWIW, I've wanted something like this for a long time (for scanning log files in reverse). |
Some people like the feature, Guido isn't against the feature ... It Can you come up with a new patch and unit tests? The io module has |
Sure - I'll do an updated patch at the weekend. |
Here's an updated version of the patch. Changes:
The patch includes documentation for reversed() and __reversed__() (in the I also updated the expected output from test_profile and test_cProfile, |
I'd like to see the doc patches separated out and applied to 2.6 -- I like the idea, haven't had time to carefully review the code, but
immediately raises ValueError: I/O operation on closed file |
As Guido requested I've split off the generic reversed() and __reversed__() The I/O error from reversed(open("/etc/passwd")) was caused by the inner I think it's at least questionable that TextIOWrapper.close() is calling |
The OP has done everything asked of him. There are a lot of positive comments about this request. Snag is the patch is in python, I understand that io is now written in C. Could we at this late stage get this into 3.2, or even a minor release of 3.2, or will it have to be deferred until 3.3? |
Not quite. He split out the documentation part of his patch and it was accepted and committed. Guido raised an issue with the code. OP raised more questions. The code was never updated. Now the patch is out of date because io module has been reimplemented in C. The patch is still good as a prototype/proof of concept but needs to be updated to apply to _pyio. The idea is good, but the implementation is not ready. |
I'll do a C version of the patch (hopefully in the next week or so). |
Suggestions:
|
Attached is an implementation of BufferedReader.readprevline(), as suggested by Antoine. At this point, it seems to be working but I would like to improve the tests when a single result spans multiple chunks. I would particularly like to ensure correct behaviour when a newline ends up as the last byte of a new chunk. |
Last activity was 2014. Anyone interested in getting this over the finish line? |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
IOBase
[WIP] #120710The text was updated successfully, but these errors were encountered: