Skip to content

Commit

Permalink
update warn msg upon empty file
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Sep 10, 2024
1 parent a03b301 commit 138b756
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
23 changes: 12 additions & 11 deletions src/monty/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def _get_line_ending(
"\r\n": Windows line ending.
Raises:
ValueError: If line ending is unknown.
ValueError: If line ending is unknown, likely the file is
missing a terminating character.
Warnings:
If file is empty, "\n" would be used as default.
Expand All @@ -93,7 +94,7 @@ def _get_line_ending(

# Return Unix "\n" line ending as default if file is empty
if not first_line:
warnings.warn("File empty, use default line ending \n.", stacklevel=2)
warnings.warn("File is empty, return Unix line ending \n.", stacklevel=2)
return "\n"

if first_line.endswith(b"\r\n"):
Expand Down Expand Up @@ -162,22 +163,22 @@ def reverse_readline(
max_mem: int = 4000000,
) -> Iterator[str]:
"""
Generator function to read a file line-by-line, but backwards.
This allows one to efficiently get data at the end of a file.
Read a file line-by-line, but backwards. This allows one to
efficiently get data from the end of a file.
Read file forwards and reverse in memory for files smaller than the
max_mem parameter, or for gzip files where reverse seeks are not supported.
max_mem parameter, or for Gzip files where reverse seeks are not supported.
Files larger than max_mem are dynamically read backwards.
Reference:
Based on code by Peter Astrand <[email protected]>, using modifications
by Raymond Hettinger and Kevin German.
http://code.activestate.com/recipes/439045-read-a-text-file-backwards
-yet-another-implementat/
Based on code by Peter Astrand <[email protected]>, using
modifications by Raymond Hettinger and Kevin German.
http://code.activestate.com/recipes/439045-read-a-text-
file-backwards-yet-another-implementat/
Args:
m_file (File): File stream to read (backwards)
m_file (File): File stream to read (backwards).
blk_size (int): The buffer size in bytes. Defaults to 4096.
max_mem (int): The maximum amount of memory to involve in this
operation. This is used to determine when to reverse a file
Expand All @@ -202,7 +203,7 @@ def reverse_readline(
file_size = max_mem + 1

# If the file size is within desired RAM limit, just reverse it in memory.
# GZip files must use this method because there is no way to negative seek.
# Gzip files must use this method because there is no way to negative seek.
# For windows, we also read the whole file.
if (
platform.system() == "Windows"
Expand Down
34 changes: 17 additions & 17 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_empty_file(self):
test_file = "empty_file.txt"
open(test_file, "w").close()

with pytest.warns(match="File empty, use default line ending \n"):
with pytest.warns(match="File is empty, return Unix line ending \n"):
assert _get_line_ending(test_file) == "\n"

def test_unknown_line_ending(self):
Expand Down Expand Up @@ -128,7 +128,7 @@ def test_empty_file(self):
Make sure an empty file does not throw an error when reverse_readline
is called, which was a problem with an earlier implementation.
"""
with pytest.warns(match="File empty, use default line ending \n."):
with pytest.warns(match="File is empty, return Unix line ending \n."):
with open(os.path.join(TEST_DIR, "empty_file.txt"), encoding="utf-8") as f:
for _line in reverse_readline(f):
pytest.fail("No error should be thrown.")
Expand All @@ -149,23 +149,23 @@ def test_file_with_empty_lines(self, l_end):
revert_contents = tuple(reverse_readline(file))
assert revert_contents[::-1] == contents

# Test gzip file
gzip_filename = f"{filename}.gz"
with gzip.open(gzip_filename, "w") as file_out:
for line in contents:
file_out.write(line.encode())
# # Test gzip file
# gzip_filename = f"{filename}.gz"
# with gzip.open(gzip_filename, "w") as file_out:
# for line in contents:
# file_out.write(line.encode())

revert_contents_gzip = tuple(reverse_readline(gzip_filename))
assert revert_contents_gzip[::-1] == contents
# revert_contents_gzip = tuple(reverse_readline(gzip_filename))
# assert revert_contents_gzip[::-1] == contents

# Test bzip2 file
bz2_filename = f"{filename}.bz2"
with bz2.open(bz2_filename, "w") as file_out:
for line in contents:
file_out.write(line.encode())
# # Test bzip2 file
# bz2_filename = f"{filename}.bz2"
# with bz2.open(bz2_filename, "w") as file_out:
# for line in contents:
# file_out.write(line.encode())

revert_contents_bz2 = tuple(reverse_readline(bz2_filename))
assert revert_contents_bz2[::-1] == contents
# revert_contents_bz2 = tuple(reverse_readline(bz2_filename))
# assert revert_contents_bz2[::-1] == contents

@pytest.mark.parametrize("l_end", ["\n", "\r\n"])
def test_line_ending(self, l_end):
Expand Down Expand Up @@ -222,7 +222,7 @@ def test_empty_file(self):
Make sure an empty file does not throw an error when reverse_readline
is called, which was a problem with an earlier implementation.
"""
with pytest.warns(match="File empty, use default line ending \n."):
with pytest.warns(match="File is empty, return Unix line ending \n."):
for _line in reverse_readfile(os.path.join(TEST_DIR, "empty_file.txt")):
pytest.fail("No error should be thrown.")

Expand Down

0 comments on commit 138b756

Please sign in to comment.