From 3d66ec6c8400bc54862b7756373c3e44ad34537b Mon Sep 17 00:00:00 2001 From: Ryan May Date: Mon, 24 May 2021 19:37:28 -0600 Subject: [PATCH] MNT: Some minor simplifications for the IO code It turns out that += for bytearray is implemented efficiently (unlike strings)--originally this was even faster than extend(). Also update one last use of Struct() to unpack a single integer. --- src/metpy/io/_tools.py | 2 +- src/metpy/io/nexrad.py | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/metpy/io/_tools.py b/src/metpy/io/_tools.py index de5af3fe8a2..eefd8beb161 100644 --- a/src/metpy/io/_tools.py +++ b/src/metpy/io/_tools.py @@ -343,7 +343,7 @@ def zlib_decompress_all_frames(data): while data: decomp = zlib.decompressobj() try: - frames.extend(decomp.decompress(data)) + frames += decomp.decompress(data) data = decomp.unused_data log.debug('Decompressed zlib frame. %d bytes remain.', len(data)) except zlib.error: diff --git a/src/metpy/io/nexrad.py b/src/metpy/io/nexrad.py index ce65edf8255..35f678a38d1 100644 --- a/src/metpy/io/nexrad.py +++ b/src/metpy/io/nexrad.py @@ -60,11 +60,10 @@ def bzip_blocks_decompress_all(data): frames = bytearray() offset = 0 while offset < len(data): - size_bytes = data[offset:offset + 4] + block_cmp_bytes = abs(int.from_bytes(data[offset:offset + 4], 'big', signed=True)) offset += 4 - block_cmp_bytes = abs(Struct('>l').unpack(size_bytes)[0]) try: - frames.extend(bz2.decompress(data[offset:offset + block_cmp_bytes])) + frames += bz2.decompress(data[offset:offset + block_cmp_bytes]) offset += block_cmp_bytes except OSError: # If we've decompressed any frames, this is an error mid-stream, so warn, stop