Patches item #968063, was opened at 2004-06-07 11:45 Message generated for change (Comment added) made by sonderblade You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=305470&aid=968063&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 2.3 Status: Open Resolution: None Priority: 5 Private: No Submitted By: Relm Arrowny (relm) Assigned to: Nobody/Anonymous (nobody) Summary: Add fileinput.islastline() Initial Comment: You can test for the first line of a file with fileinput.isfirstline(), but there is no corresponding fileinput.islastline() to test for the last line of a file. Note that there is already an unapplied patch for this at request ID 776100, but it is faulty in that it only tests for end of buffer and not end of file. I was not sure how to append this patch to the existing request, so have started a new one. Hope this is okay. This patch also includes documentation and test updates. ---------------------------------------------------------------------- Comment By: Bj�rn Lindqvist (sonderblade) Date: 2007-03-14 02:25 Message: Logged In: YES user_id=51702 Originator: NO I have applied this patch and it works as specified. Although it does not apply cleanly and it contains an error; self._lastline = None should probably be in the __init__() method. Please reconsider this patch and see Relms attached wc program. It is definitely sensible and useful for fileinput to have a islastline method. In fact, every program that uses fileinput and wants to make a summary for each read file. ---------------------------------------------------------------------- Comment By: Ilya Sandler (isandler) Date: 2004-08-05 20:17 Message: Logged In: YES user_id=971153 An alternative to islastline() would be to add a single-line look-ahead capability: nextline() which would return a structure containing next line, and its file name (or have 2 functions nextline() and nextlinefile() ) This would cover most of the OP's needs but would also provide extra useful functionality....E.g. when processing files which have a header line [data line]* structure its very convenient to know in advance when section ends while processing the last line of section not when enountering the header line of the next section... Another use case would be processing of files with line groups: group1line1 group1line2 group1line3 group2line1 group2line2 (grouping could be for example on the value of a certian field in the line) again it would be very useful to know when a group ends while still processing this group ---------------------------------------------------------------------- Comment By: Dave Harrison (dharriso) Date: 2004-07-04 13:24 Message: Logged In: YES user_id=1076674 I would say that I'm -0.5 on this feature. After looking over the current code for fileinput.py in the cvs tree, it looks like the current code implements an iterator model. Since fileinput.py uses the next() call to move along its input, and raises StopIteration if there are no more lines to be returned, this function would have to be polled to find out if the final line had been reached - where a try/except block that caught StopIteration would seem more intuitive to me (Inkeeping with the current code). However it might be considered that the nextfile() function be changed to raise StopIteration after the last file had been completed to maintain consistency between the lines and files aspects of the class. ---------------------------------------------------------------------- Comment By: Relm Arrowny (relm) Date: 2004-06-15 20:42 Message: Logged In: YES user_id=1058331 The OP's use case was too simplified to convey the usefulness of fileinput.islastline(). It does not just save a couple of lines. I am attaching a "wc" or "word count" program to demonstrate. Note the following problems with the current work-around: 1. Instead of doing end-of-file processing at the end of a file, we must do it at the beginning of the *next* file. 2. Useful functions such as fileinput.filelineno() and fileinput.filename() now refer to the *wrong* file, so now we need to manually keep track of these values ourselves. 3. Perhaps a minor point, but this work-around only works at the *beginning* of the loop instead of the end. It feels somewhat awkward. 4. End-of-file processing must occur both in the loop and once outside. A function avoids code duplication, but to refer to variables in the loop, they must be passed into and out of the function (6 in, 2 out for the "wc" example... maybe keyword arguments would have helped). 5. Most important is readablility. It's not obvious that `fileinput.isfirstline() and fileinput.lineno() != 1` means we are the end of the (previous!) file. Using `fileinput.islastline()` would be much clearer. And not that it is a reason, but Perl and Ruby solve this with an `eof` function and `eof?` method, respectively. ---------------------------------------------------------------------- Comment By: Raymond Hettinger (rhettinger) Date: 2004-06-15 14:05 Message: Logged In: YES user_id=80475 I'm -0 on complicating this venerable interface. Also, I find it unnatural for the inside of a loop to be able to ascertain whether it is in its final iteration. The closest approximation is the for-else clause which is vary rarely used in normal python programming. While the OP was able to sketch a use case, it involved only saving a couple of lines over what can be done with the existing API. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=305470&aid=968063&group_id=5470
_______________________________________________ Patches mailing list Patches@python.org http://mail.python.org/mailman/listinfo/patches