[Python-ideas] Add peekline(), peeklines(n) and optional maxlines argument to readlines()

Giampaolo Rodolà 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:

f.peeklines(10)
f.seek(0)

...or 2 in case you're not at the beginning of the file.

before = f.tell()
f.peeklines(10)
f.seek(before)

Not a great advantage vs. the fact of introducing (and remembering) a new
function, in my opinion.

Regards,

--- Giampaolo
http://code.google.com/p/pyftpdlib/
http://code.google.com/p/psutil/ <http://code.google.com/p/pyftpdlib/>

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...
> f.readline()
> f.seek(0)
> 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...
>
> f.readlines(maxlines=10)
>
> or for the 'peek' case
>
> f.peeklines(10)
>
>
> I also didn't find any of the answers from
>
> http://stackoverflow.com/questions/1767513/read-first-n-lines-of-a-file-in-python
> 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
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110930/18489d59/attachment.html>


More information about the Python-ideas mailing list