Skip to content

Commit 1f207df

Browse files
committed
Add a "stream" method to PathPlus
1 parent 4890417 commit 1f207df

File tree

3 files changed

+452
-0
lines changed

3 files changed

+452
-0
lines changed

domdf_python_tools/paths.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,22 @@ def move(self: _PP, dst: PathLike) -> _PP:
938938
new_path = shutil.move(os.fspath(self), dst)
939939
return self.__class__(new_path)
940940

941+
def stream(self, chunk_size: int = 1024) -> Iterator[bytes]:
942+
"""
943+
Stream the file in ``chunk_size`` sized chunks.
944+
945+
:param chunk_size: The chunk size, in bytes
946+
947+
.. versionadded:: 3.2.0
948+
"""
949+
950+
with self.open("rb") as fp:
951+
while True:
952+
chunk = fp.read(chunk_size)
953+
if not chunk:
954+
break
955+
yield chunk
956+
941957

942958
class PosixPathPlus(PathPlus, pathlib.PurePosixPath):
943959
"""

tests/test_paths.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,3 +1031,9 @@ def test_dont_move_dir_in_itself(self, move_example_file: PathPlus):
10311031

10321032
with pytest.raises(shutil.Error):
10331033
move_example_file.parent.move(dst)
1034+
1035+
1036+
def test_stream(tmp_pathplus: PathPlus, advanced_data_regression: AdvancedDataRegressionFixture):
1037+
the_file = tmp_pathplus / "file.dat"
1038+
the_file.write_text("The quick brown fox jumps over the lazy dog" * 100)
1039+
advanced_data_regression.check(list(map(bytes.decode, the_file.stream(chunk_size=10))))

0 commit comments

Comments
 (0)