diff options
author | Helmut Grohne <helmut@subdivi.de> | 2021-02-19 22:05:54 +0100 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2021-02-19 22:05:54 +0100 |
commit | dfcfa63fd6b4269ea4ddf57c943258d1560ad2a4 (patch) | |
tree | da11fb3c78461f24a4ec1127068a7464ca4d5a6d | |
parent | 27cd6b51471a3a14cdaf20aade95ef72339c1b40 (diff) | |
download | crossqa-dfcfa63fd6b4269ea4ddf57c943258d1560ad2a4.tar.gz |
simplify common functions
-rw-r--r-- | common.py | 11 |
1 files changed, 3 insertions, 8 deletions
@@ -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"") |