[Patches] [ python-Patches-968063 ] Add fileinput.islastline()
SourceForge.net
noreply at sourceforge.net
Sun Jul 4 07:24:45 EDT 2004
Patches item #968063, was opened at 2004-06-07 09:45
Message generated for change (Comment added) made by dharriso
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=968063&group_id=5470
Category: Library (Lib)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
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: Dave Harrison (dharriso)
Date: 2004-07-04 11: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 18: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 12: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
More information about the Patches
mailing list