[Python-ideas] Add peekline(), peeklines(n) and optional maxlines argument to readlines()
g.rodola at gmail.com
Fri Sep 30 11:42:31 CEST 2011
+0.5 about f.readlines(maxlines=10); it might be a good addition since the
sizehint parameter does not allow an actual prediction of how many lines
will be returned.
On the other side the two arguments are mutually exclusive, therefore I'm
not sure what expect in case both are specified (maybe ValueError, but such
kind of APIs always leave me a little skeptical).
+0 about f.peeklines(10) as it only saves one line of code:
...or 2 in case you're not at the beginning of the file.
before = f.tell()
Not a great advantage vs. the fact of introducing (and remembering) a new
function, in my opinion.
2011/9/30 John O'Connor <jxo6948 at rit.edu>
> It seems there could be a cleaner way of reading the first n lines of
> a file and additionally not seeking past those lines (ie peek). This
> is obviously a trivial task for 1 line ie...
> but one that I think would make sense to add to the IO implementation,
> given that we already have readline, readlines, and peek I think
> peekline() or peeklines(n) is only a natural addition. The argument
> for doing so (in 3.3 of course), is primarily readability but also
> that the maintenance burden *seems* like it would be low. This
> addition would also be helpful and more concise where n > 1.
> I think readlines() should also take an optional argument for a max
> number of lines to read. It seems more common/helpful to me than
> 'hint' for max bytes. In n>1 case one could do...
> or for the 'peek' case
> I also didn't find any of the answers from
> to be very compelling.
> I am more than willing to propose a patch if the idea(s) are supported.
> - John
> Python-ideas mailing list
> Python-ideas at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas