# HG changeset patch # User digitalxero # Date 1298776275 18000 # Node ID 56c0fb214f3b9fe6cbbc14306212cb91eb766b1c # Parent 4479573a8b690e313de97185dd8b30683c48134a Update #968063 for 3.x Added islastline() to fileinput.py Added extra tests to test isfirstline and islastline diff -r 4479573a8b69 -r 56c0fb214f3b Lib/fileinput.py --- a/Lib/fileinput.py Sat Feb 26 21:28:41 2011 +0100 +++ b/Lib/fileinput.py Sat Feb 26 22:11:15 2011 -0500 @@ -15,7 +15,8 @@ Functions filename(), lineno() return the filename and cumulative line number of the line that has just been read; filelineno() returns its line number in the current file; isfirstline() returns true iff the -line just read is the first line of its file; isstdin() returns true +line just read is the first line of its file; islastline() returns true +iff the line just read is the last line of its file; isstdin() returns true iff the line was read from sys.stdin. Function nextfile() closes the current file so that the next iteration will read the first line from the next file (if any); lines not read from the file will not count @@ -45,8 +46,8 @@ character; otherwise lines are returned including the trailing newline. -Class FileInput is the implementation; its methods filename(), -lineno(), fileline(), isfirstline(), isstdin(), nextfile() and close() +Class FileInput is the implementation; its methods filename(), lineno(), +fileline(), isfirstline(), islastline(), isstdin(), nextfile() and close() correspond to the functions in the module. In addition it has a readline() method which returns the next input line, and a __getitem__() method which implements the sequence behavior. The @@ -82,7 +83,7 @@ import sys, os __all__ = ["input", "close", "nextfile", "filename", "lineno", "filelineno", - "isfirstline", "isstdin", "FileInput"] + "isfirstline", "islastline", "isstdin", "FileInput"] _state = None @@ -173,6 +174,15 @@ raise RuntimeError("no active input()") return _state.isfirstline() +def islastline(): + """ + Returns true if the line just read is the last line of its file, + otherwise returns false. + """ + if not _state: + raise RuntimeError("no active input()") + return _state.islastline() + def isstdin(): """ Returns true if the last line was read from sys.stdin, @@ -186,9 +196,9 @@ """class FileInput([files[, inplace[, backup[, mode[, openhook]]]]]) Class FileInput is the implementation of the module; its methods - filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(), - nextfile() and close() correspond to the functions of the same name - in the module. + filename(), lineno(), fileline(), isfirstline(), islastline(), isstdin(), + fileno(), nextfile() and close() correspond to the functions of the same + name in the module. In addition it has a readline() method which returns the next input line, and a __getitem__() method which implements the sequence behavior. The sequence must be accessed in strictly @@ -220,6 +230,7 @@ self._backupfilename = None self._buffer = [] self._bufindex = 0 + self._lastline = None # restrict mode argument to reading modes if mode not in ('r', 'rU', 'U', 'rb'): raise ValueError("FileInput opening mode must be one of " @@ -380,6 +391,9 @@ def isfirstline(self): return self._filelineno == 1 + def islastline(self): + return self._filelineno == len(self._buffer) + def isstdin(self): return self._isstdin @@ -414,6 +428,12 @@ for line in input(args, inplace=inplace, backup=backup): if line[-1:] == '\n': line = line[:-1] if line[-1:] == '\r': line = line[:-1] + if isfirstline(): + mark = "*" + elif islastline(): + mark = "%" + else: + mark = "" print("%d: %s[%d]%s %s" % (lineno(), filename(), filelineno(), isfirstline() and "*" or "", line)) print("%d: %s[%d]" % (lineno(), filename(), filelineno())) diff -r 4479573a8b69 -r 56c0fb214f3b Lib/test/test_fileinput.py --- a/Lib/test/test_fileinput.py Sat Feb 26 21:28:41 2011 +0100 +++ b/Lib/test/test_fileinput.py Sat Feb 26 22:11:15 2011 -0500 @@ -69,6 +69,7 @@ self.assertEqual(fi.lineno(), 21) self.assertEqual(fi.filelineno(), 6) self.assertFalse(fi.isfirstline()) + self.assertFalse(fi.islastline()) self.assertFalse(fi.isstdin()) if verbose: @@ -189,6 +190,54 @@ finally: remove_tempfiles(t1, t2) + def test_isfirstline(self): + t1 = t2 = t3 = None + try: + t1 = writeTmp(1, ["A\nB"]) + t2 = writeTmp(2, ["C\nD\nE"]) + t3 = writeTmp(3, ["F"]) + fi = FileInput(files=(t1, t2, t3)) + fi.readline() + self.assertTrue(fi.isfirstline()) + fi.readline() + self.assertFalse(fi.isfirstline()) + fi.readline() + self.assertTrue(fi.isfirstline()) + fi.readline() + self.assertFalse(fi.isfirstline()) + fi.readline() + self.assertFalse(fi.isfirstline()) + fi.readline() + self.assertTrue(fi.isfirstline()) + fi.close() + finally: + remove_tempfiles(t1, t2, t3) + + def test_islastline(self): + t1 = t2 = t3 = None + try: + t1 = writeTmp(1, ["A\nB"]) + t2 = writeTmp(2, ["C\nD\nE"]) + t3 = writeTmp(3, ["F"]) + fi = FileInput(files=(t1, t2, t3)) + fi.readline() + self.assertFalse(fi.islastline()) + fi.readline() + self.assertTrue(fi.islastline()) + fi.readline() + self.assertFalse(fi.islastline()) + fi.readline() + self.assertFalse(fi.islastline()) + fi.readline() + self.assertTrue(fi.islastline()) + fi.readline() + self.assertTrue(fi.isfirstline()) + self.assertTrue(fi.islastline()) + fi.close() + finally: + remove_tempfiles(t1, t2, t3) + + def test_opening_mode(self): try: # invalid mode, should raise ValueError