From dfcfa63fd6b4269ea4ddf57c943258d1560ad2a4 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Fri, 19 Feb 2021 22:05:54 +0100 Subject: simplify common functions --- common.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'common.py') diff --git a/common.py b/common.py index 3b106b6..1d32d10 100644 --- a/common.py +++ b/common.py @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0+ +import functools import typing ByteIterable = typing.Iterable[bytes] @@ -22,17 +23,11 @@ def decompress_stream(iterable: ByteIterable, decompressor) -> ByteIterable: """Decompress an iterable of bytes using the given decompressor into another (decompressed) iterable of bytes. The decompressor can be a bz2.BZ2Decompressor or lzma.LZMADecompressor instance.""" - for data in iterable: - data = decompressor.decompress(data) - yield data + yield from map(decompressor.decompress, iterable) if hasattr(decompressor, "flush"): yield decompressor.flush() def yield_chunks(filelike, chunksize=65536) -> ByteIterable: """Read the given file in chunks of the given size. Returns an itrable of contents. If the file is binary, it yields bytes, otherwise str.""" - while True: - data = filelike.read(chunksize) - if not data: - break - yield data + return iter(functools.partial(filelike.read, chunksize), b"") -- cgit v1.2.3