From ad3af597e00eca5d9583aac6109be7e43d8ffdfd Mon Sep 17 00:00:00 2001 From: Andrea Zoppi Date: Fri, 22 Dec 2023 18:50:03 +0100 Subject: [PATCH] Memory.blocks() using memoryviews only Signed-off-by: Andrea Zoppi --- CHANGELOG.rst | 7 +++++++ LICENSE | 2 +- src/bytesparse/__init__.py | 4 ++-- src/bytesparse/base.py | 4 ++-- src/bytesparse/inplace.py | 13 +++++++------ src/bytesparse/io.py | 2 +- tests/_common.py | 2 +- tests/test_inplace.py | 2 +- tests/test_io.py | 2 +- 9 files changed, 23 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3ef5f76..adfdc49 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,13 @@ Changelog ========= +0.0.8 (2024-01-21) +------------------ + +* Added ``chop`` method. +* Minor fixes. + + 0.0.7 (2023-12-10) ------------------ diff --git a/LICENSE b/LICENSE index 4d48133..2c17d91 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ BSD 2-Clause License -Copyright (c) 2020-2023, Andrea Zoppi +Copyright (c) 2020-2024, Andrea Zoppi All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/src/bytesparse/__init__.py b/src/bytesparse/__init__.py index af5947f..06c00e5 100644 --- a/src/bytesparse/__init__.py +++ b/src/bytesparse/__init__.py @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2023, Andrea Zoppi. +# Copyright (c) 2020-2024, Andrea Zoppi. # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -112,7 +112,7 @@ """ -__version__ = '0.0.7' +__version__ = '0.0.8' from .inplace import Memory from .inplace import bytesparse diff --git a/src/bytesparse/base.py b/src/bytesparse/base.py index 79ef21b..d8bc72a 100644 --- a/src/bytesparse/base.py +++ b/src/bytesparse/base.py @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2023, Andrea Zoppi. +# Copyright (c) 2020-2024, Andrea Zoppi. # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -703,7 +703,7 @@ def blocks( self, start: Optional[Address] = None, endex: Optional[Address] = None, - ) -> Iterator[Tuple[Address, memoryview]]: + ) -> Iterator[Block]: r"""Iterates over blocks. Iterates over data blocks within an address range. diff --git a/src/bytesparse/inplace.py b/src/bytesparse/inplace.py index 2fe6fbf..3c052c1 100644 --- a/src/bytesparse/inplace.py +++ b/src/bytesparse/inplace.py @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2023, Andrea Zoppi. +# Copyright (c) 2020-2024, Andrea Zoppi. # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -856,12 +856,13 @@ def blocks( self, start: Optional[Address] = None, endex: Optional[Address] = None, - ) -> Iterator[Tuple[Address, Union[memoryview, bytearray]]]: + ) -> Iterator[Block]: blocks = self._blocks if blocks: if start is None and endex is None: # faster - yield from blocks + for block_start, block_data in blocks: + yield [block_start, memoryview(block_data)] else: block_index_start = 0 if start is None else self._block_index_start(start) block_index_endex = len(blocks) if endex is None else self._block_index_endex(endex) @@ -875,7 +876,7 @@ def blocks( if slice_start < slice_endex: slice_view = memoryview(block_data) slice_view = slice_view[(slice_start - block_start):(slice_endex - block_start)] - yield slice_start, slice_view + yield [slice_start, slice_view] def bound( self, @@ -993,7 +994,7 @@ def chop( start: Optional[Address] = None, endex: Optional[Address] = None, align: bool = False, - ) -> Iterator[Block]: + ) -> Iterator[Tuple[Address, memoryview]]: if width < 1: raise ValueError('invalid width') @@ -3091,7 +3092,7 @@ def blocks( self, start: Optional[Address] = None, endex: Optional[Address] = None, - ) -> Iterator[Tuple[Address, memoryview]]: + ) -> Iterator[Block]: start, endex = self._rectify_span(start, endex) yield from super().blocks(start=start, endex=endex) diff --git a/src/bytesparse/io.py b/src/bytesparse/io.py index 6fd6fa2..8829b0a 100644 --- a/src/bytesparse/io.py +++ b/src/bytesparse/io.py @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2023, Andrea Zoppi. +# Copyright (c) 2020-2024, Andrea Zoppi. # All rights reserved. # # Redistribution and use in source and binary forms, with or without diff --git a/tests/_common.py b/tests/_common.py index cdaf195..4a5be25 100644 --- a/tests/_common.py +++ b/tests/_common.py @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2023, Andrea Zoppi. +# Copyright (c) 2020-2024, Andrea Zoppi. # All rights reserved. # # Redistribution and use in source and binary forms, with or without diff --git a/tests/test_inplace.py b/tests/test_inplace.py index ddc3a0b..2090dd6 100644 --- a/tests/test_inplace.py +++ b/tests/test_inplace.py @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2023, Andrea Zoppi. +# Copyright (c) 2020-2024, Andrea Zoppi. # All rights reserved. # # Redistribution and use in source and binary forms, with or without diff --git a/tests/test_io.py b/tests/test_io.py index e0b1cac..04e64a0 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2023, Andrea Zoppi. +# Copyright (c) 2020-2024, Andrea Zoppi. # All rights reserved. # # Redistribution and use in source and binary forms, with or without