Skip to content

Commit

Permalink
fix skip last l_end (temporarily)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Sep 14, 2024
1 parent 33257c5 commit f75abd3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/monty/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def reverse_readline(

buffer: str = ""
m_file.seek(0, 2)
eof_pos = m_file.tell() # Needed to skip first match
count = 0 # TODO: better way to skip first match

while True:
l_end_pos: int = buffer.rfind(l_end)
Expand All @@ -264,7 +264,8 @@ def reverse_readline(
buffer = buffer[:l_end_pos] # buffer doesn't include l_end

# Skip first match (the last line ending)
if l_end_pos != eof_pos:
if count != 0:
count += 1
yield line + l_end

# Line ending not in current buffer, load next block into the buffer
Expand Down
10 changes: 2 additions & 8 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,10 @@ def test_reverse_readline(self):
for idx, line in enumerate(reverse_readline(f)):
assert line == f"{str(self.NUMLINES - idx)}{os.linesep}"

def test_big_file(self):
def test_fake_big_file(self):
"""
Make sure that large text files are read properly,
by setting max_mem to a very small value.
TODO: rewrite test with a real big file
DEBUG: when max_mem = 0, the first item generated is "\n",
but the sequential items are correct.
"""
with (
open(
Expand All @@ -169,8 +164,7 @@ def test_read_bz2(self):
with zopen(os.path.join(TEST_DIR, "myfile_bz2.bz2"), "rb") as f:
for line in reverse_readline(f):
lines.append(line)
assert lines == ["\n", "HelloWorld.\n"] # test file has one empty line
assert all(isinstance(line, str) for line in lines)
assert lines == ["HelloWorld.\n"] # test file has one single line

def test_read_empty_file(self):
"""
Expand Down

0 comments on commit f75abd3

Please sign in to comment.