Skip to content

Make Checksum32 an ABC #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions numcodecs/checksum32.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import abc
import struct
import zlib
from collections.abc import Callable
from contextlib import suppress
from types import ModuleType
from typing import TYPE_CHECKING, Literal, Optional
from typing import Literal, Optional

import numpy as np
from typing_extensions import Buffer

from .abc import Codec
from .compat import ensure_contiguous_ndarray, ndarray_copy
Expand All @@ -15,15 +16,11 @@
with suppress(ImportError):
import crc32c as _crc32c # type: ignore[no-redef, unused-ignore]

if TYPE_CHECKING: # pragma: no cover
from typing_extensions import Buffer

CHECKSUM_LOCATION = Literal['start', 'end']


class Checksum32(Codec):
class Checksum32(Codec, abc.ABC):
# override in sub-class
checksum: Callable[["Buffer", int], int] | None = None
location: CHECKSUM_LOCATION = 'start'

def __init__(self, location: CHECKSUM_LOCATION | None = None):
Expand Down Expand Up @@ -67,6 +64,10 @@ def decode(self, buf, out=None):
)
return ndarray_copy(payload_view, out)

@staticmethod
@abc.abstractmethod
def checksum(data: Buffer, value: int) -> int: ...


class CRC32(Checksum32):
"""Codec add a crc32 checksum to the buffer.
Expand All @@ -78,9 +79,15 @@ class CRC32(Checksum32):
"""

codec_id = 'crc32'
checksum = zlib.crc32
location = 'start'

@staticmethod
def checksum(data: Buffer, value: int = 0) -> int:
"""
Thin wrapper around ``zlib.crc32``.
"""
return zlib.crc32(data, value)


class Adler32(Checksum32):
"""Codec add a adler32 checksum to the buffer.
Expand All @@ -92,9 +99,15 @@ class Adler32(Checksum32):
"""

codec_id = 'adler32'
checksum = zlib.adler32
location = 'start'

@staticmethod
def checksum(data: Buffer, value: int = 1) -> int:
"""
Thin wrapper around ``zlib.adler32``.
"""
return zlib.adler32(data, value)


class JenkinsLookup3(Checksum32):
"""Bob Jenkin's lookup3 checksum with 32-bit output
Comment on lines 112 to 113
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears the checksum method was not added here

Expand Down
7 changes: 2 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ name = "numcodecs"
description = """
A Python package providing buffer compression and transformation codecs \
for use in data storage and communication applications."""
readme = "README.rst"
dependencies = [
"numpy>=1.24",
"deprecated"
]
readme = "README.rst"
dependencies = ["numpy>=1.24", "deprecated", "typing_extensions"]
Copy link
Member

@jakirkham jakirkham Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not unwrap this

Edit: To clarify, having this broken out with 1 dependency per line makes it easier to see what was added/removed in the diff. This in turn helps when updating in other package metadata elsewhere (like conda-forge or Linux distros)

requires-python = ">=3.11"
dynamic = [
"version",
Expand Down
Loading