numcodecs 0.13.0__cp312-cp312-win_amd64.whl → 0.14.0__cp312-cp312-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of numcodecs might be problematic. Click here for more details.

Files changed (72) hide show
  1. numcodecs/__init__.py +11 -11
  2. numcodecs/_shuffle.cp312-win_amd64.pyd +0 -0
  3. numcodecs/abc.py +4 -5
  4. numcodecs/astype.py +2 -4
  5. numcodecs/bitround.py +2 -2
  6. numcodecs/blosc.cp312-win_amd64.pyd +0 -0
  7. numcodecs/bz2.py +1 -2
  8. numcodecs/categorize.py +5 -12
  9. numcodecs/checksum32.py +87 -13
  10. numcodecs/compat.py +4 -8
  11. numcodecs/compat_ext.cp312-win_amd64.pyd +0 -0
  12. numcodecs/delta.py +7 -4
  13. numcodecs/fixedscaleoffset.py +3 -9
  14. numcodecs/fletcher32.cp312-win_amd64.pyd +0 -0
  15. numcodecs/jenkins.cp312-win_amd64.pyd +0 -0
  16. numcodecs/json.py +7 -8
  17. numcodecs/lz4.cp312-win_amd64.pyd +0 -0
  18. numcodecs/lzma.py +7 -11
  19. numcodecs/msgpacks.py +2 -5
  20. numcodecs/ndarray_like.py +5 -5
  21. numcodecs/packbits.py +0 -4
  22. numcodecs/pcodec.py +2 -2
  23. numcodecs/pickles.py +1 -1
  24. numcodecs/quantize.py +4 -10
  25. numcodecs/registry.py +10 -9
  26. numcodecs/shuffle.py +4 -4
  27. numcodecs/tests/common.py +7 -11
  28. numcodecs/tests/test_astype.py +2 -4
  29. numcodecs/tests/test_base64.py +2 -5
  30. numcodecs/tests/test_bitround.py +3 -4
  31. numcodecs/tests/test_blosc.py +4 -7
  32. numcodecs/tests/test_bz2.py +3 -6
  33. numcodecs/tests/test_categorize.py +3 -5
  34. numcodecs/tests/test_checksum32.py +101 -24
  35. numcodecs/tests/test_compat.py +4 -3
  36. numcodecs/tests/test_delta.py +4 -5
  37. numcodecs/tests/test_entrypoints.py +1 -2
  38. numcodecs/tests/test_entrypoints_backport.py +4 -4
  39. numcodecs/tests/test_fixedscaleoffset.py +16 -11
  40. numcodecs/tests/test_gzip.py +3 -6
  41. numcodecs/tests/test_jenkins.py +33 -33
  42. numcodecs/tests/test_json.py +3 -3
  43. numcodecs/tests/test_lz4.py +3 -6
  44. numcodecs/tests/test_lzma.py +8 -7
  45. numcodecs/tests/test_msgpacks.py +8 -8
  46. numcodecs/tests/test_packbits.py +2 -4
  47. numcodecs/tests/test_pcodec.py +5 -6
  48. numcodecs/tests/test_pickles.py +2 -3
  49. numcodecs/tests/test_quantize.py +3 -6
  50. numcodecs/tests/test_registry.py +14 -12
  51. numcodecs/tests/test_shuffle.py +5 -8
  52. numcodecs/tests/test_vlen_array.py +5 -6
  53. numcodecs/tests/test_vlen_bytes.py +5 -6
  54. numcodecs/tests/test_vlen_utf8.py +5 -8
  55. numcodecs/tests/test_zarr3.py +248 -0
  56. numcodecs/tests/test_zarr3_import.py +13 -0
  57. numcodecs/tests/test_zfpy.py +8 -6
  58. numcodecs/tests/test_zlib.py +4 -7
  59. numcodecs/tests/test_zstd.py +6 -9
  60. numcodecs/version.py +2 -2
  61. numcodecs/vlen.cp312-win_amd64.pyd +0 -0
  62. numcodecs/zarr3.py +379 -0
  63. numcodecs/zfpy.py +13 -12
  64. numcodecs/zlib.py +1 -2
  65. numcodecs/zstd.cp312-win_amd64.pyd +0 -0
  66. {numcodecs-0.13.0.dist-info → numcodecs-0.14.0.dist-info}/METADATA +8 -6
  67. numcodecs-0.14.0.dist-info/RECORD +78 -0
  68. {numcodecs-0.13.0.dist-info → numcodecs-0.14.0.dist-info}/WHEEL +1 -1
  69. numcodecs-0.14.0.dist-info/entry_points.txt +22 -0
  70. numcodecs-0.13.0.dist-info/RECORD +0 -74
  71. {numcodecs-0.13.0.dist-info → numcodecs-0.14.0.dist-info}/LICENSE.txt +0 -0
  72. {numcodecs-0.13.0.dist-info → numcodecs-0.14.0.dist-info}/top_level.txt +0 -0
numcodecs/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- # flake8: noqa
1
+ # ruff: noqa: E402
2
2
  """Numcodecs is a Python package providing buffer compression and
3
3
  transformation codecs for use in data storage and communication
4
4
  applications. These include:
@@ -17,14 +17,13 @@ contribute code, please `raise an issue on GitHub
17
17
 
18
18
  """
19
19
 
20
- import multiprocessing
21
20
  import atexit
21
+ import multiprocessing
22
22
  from contextlib import suppress
23
23
 
24
-
25
- from numcodecs.version import version as __version__
26
- from numcodecs.registry import get_codec, register_codec
27
-
24
+ from numcodecs.registry import get_codec as get_codec
25
+ from numcodecs.registry import register_codec
26
+ from numcodecs.version import version as __version__ # noqa: F401
28
27
  from numcodecs.zlib import Zlib
29
28
 
30
29
  register_codec(Zlib)
@@ -57,13 +56,13 @@ with suppress(ImportError):
57
56
  atexit.register(blosc.destroy)
58
57
 
59
58
  with suppress(ImportError):
60
- from numcodecs import zstd
59
+ from numcodecs import zstd as zstd
61
60
  from numcodecs.zstd import Zstd
62
61
 
63
62
  register_codec(Zstd)
64
63
 
65
64
  with suppress(ImportError):
66
- from numcodecs import lz4
65
+ from numcodecs import lz4 as lz4
67
66
  from numcodecs.lz4 import LZ4
68
67
 
69
68
  register_codec(LZ4)
@@ -118,9 +117,10 @@ with suppress(ImportError):
118
117
 
119
118
  register_codec(MsgPack)
120
119
 
121
- from numcodecs.checksum32 import CRC32, Adler32, JenkinsLookup3
120
+ from numcodecs.checksum32 import CRC32, CRC32C, Adler32, JenkinsLookup3
122
121
 
123
122
  register_codec(CRC32)
123
+ register_codec(CRC32C)
124
124
  register_codec(Adler32)
125
125
  register_codec(JenkinsLookup3)
126
126
 
@@ -129,8 +129,8 @@ from numcodecs.json import JSON
129
129
  register_codec(JSON)
130
130
 
131
131
  with suppress(ImportError):
132
- from numcodecs import vlen
133
- from numcodecs.vlen import VLenUTF8, VLenBytes, VLenArray
132
+ from numcodecs import vlen as vlen
133
+ from numcodecs.vlen import VLenArray, VLenBytes, VLenUTF8
134
134
 
135
135
  register_codec(VLenUTF8)
136
136
  register_codec(VLenBytes)
Binary file
numcodecs/abc.py CHANGED
@@ -29,13 +29,14 @@ other and vice versa.
29
29
  """
30
30
 
31
31
  from abc import ABC, abstractmethod
32
+ from typing import Optional
32
33
 
33
34
 
34
35
  class Codec(ABC):
35
36
  """Codec abstract base class."""
36
37
 
37
38
  # override in sub-class
38
- codec_id = None
39
+ codec_id: Optional[str] = None
39
40
  """Codec identifier."""
40
41
 
41
42
  @abstractmethod
@@ -118,11 +119,9 @@ class Codec(ABC):
118
119
  # by default, assume all non-private members are configuration
119
120
  # parameters and valid keyword arguments to constructor function
120
121
 
121
- r = '%s(' % type(self).__name__
122
+ r = f'{type(self).__name__}('
122
123
  params = [
123
- '{}={!r}'.format(k, getattr(self, k))
124
- for k in sorted(self.__dict__)
125
- if not k.startswith('_')
124
+ f'{k}={getattr(self, k)!r}' for k in sorted(self.__dict__) if not k.startswith('_')
126
125
  ]
127
126
  r += ', '.join(params) + ')'
128
127
  return r
numcodecs/astype.py CHANGED
@@ -1,7 +1,7 @@
1
1
  import numpy as np
2
2
 
3
3
  from .abc import Codec
4
- from .compat import ndarray_copy, ensure_ndarray
4
+ from .compat import ensure_ndarray, ndarray_copy
5
5
 
6
6
 
7
7
  class AsType(Codec):
@@ -73,6 +73,4 @@ class AsType(Codec):
73
73
  }
74
74
 
75
75
  def __repr__(self):
76
- return '{}(encode_dtype={!r}, decode_dtype={!r})'.format(
77
- type(self).__name__, self.encode_dtype.str, self.decode_dtype.str
78
- )
76
+ return f'{type(self).__name__}(encode_dtype={self.encode_dtype.str!r}, decode_dtype={self.decode_dtype.str!r})'
numcodecs/bitround.py CHANGED
@@ -1,6 +1,5 @@
1
1
  import numpy as np
2
2
 
3
-
4
3
  from .abc import Codec
5
4
  from .compat import ensure_ndarray_like, ndarray_copy
6
5
 
@@ -60,7 +59,8 @@ class BitRound(Codec):
60
59
  return a
61
60
  if self.keepbits > bits:
62
61
  raise ValueError("Keepbits too large for given dtype")
63
- b = a.view(a_int_dtype)
62
+ b = a.copy()
63
+ b = b.view(a_int_dtype)
64
64
  maskbits = bits - self.keepbits
65
65
  mask = (all_set >> maskbits) << maskbits
66
66
  half_quantum1 = (1 << (maskbits - 1)) - 1
Binary file
numcodecs/bz2.py CHANGED
@@ -1,8 +1,7 @@
1
1
  import bz2 as _bz2
2
2
 
3
-
4
3
  from numcodecs.abc import Codec
5
- from numcodecs.compat import ndarray_copy, ensure_contiguous_ndarray
4
+ from numcodecs.compat import ensure_contiguous_ndarray, ndarray_copy
6
5
 
7
6
 
8
7
  class BZ2(Codec):
numcodecs/categorize.py CHANGED
@@ -1,9 +1,8 @@
1
- from .abc import Codec
2
- from .compat import ensure_ndarray, ndarray_copy, ensure_text
3
-
4
-
5
1
  import numpy as np
6
2
 
3
+ from .abc import Codec
4
+ from .compat import ensure_ndarray, ensure_text, ndarray_copy
5
+
7
6
 
8
7
  class Categorize(Codec):
9
8
  """Filter encoding categorical string data as integers.
@@ -41,7 +40,7 @@ class Categorize(Codec):
41
40
  def __init__(self, labels, dtype, astype='u1'):
42
41
  self.dtype = np.dtype(dtype)
43
42
  if self.dtype.kind not in 'UO':
44
- raise TypeError("only unicode ('U') and object ('O') dtypes are " "supported")
43
+ raise TypeError("only unicode ('U') and object ('O') dtypes are supported")
45
44
  self.labels = [ensure_text(label) for label in labels]
46
45
  self.astype = np.dtype(astype)
47
46
  if self.astype == np.dtype(object):
@@ -99,10 +98,4 @@ class Categorize(Codec):
99
98
  labels = repr(self.labels[:3])
100
99
  if len(self.labels) > 3:
101
100
  labels = labels[:-1] + ', ...]'
102
- r = '%s(dtype=%r, astype=%r, labels=%s)' % (
103
- type(self).__name__,
104
- self.dtype.str,
105
- self.astype.str,
106
- labels,
107
- )
108
- return r
101
+ return f'{type(self).__name__}(dtype={self.dtype.str!r}, astype={self.astype.str!r}, labels={labels})'
numcodecs/checksum32.py CHANGED
@@ -1,44 +1,115 @@
1
+ import struct
1
2
  import zlib
2
-
3
+ from collections.abc import Callable
4
+ from typing import TYPE_CHECKING, Literal
3
5
 
4
6
  import numpy as np
5
- import struct
6
-
7
7
 
8
8
  from .abc import Codec
9
9
  from .compat import ensure_contiguous_ndarray, ndarray_copy
10
10
  from .jenkins import jenkins_lookup3
11
11
 
12
+ if TYPE_CHECKING:
13
+ from typing_extensions import Buffer
14
+
15
+ CHECKSUM_LOCATION = Literal['start', 'end']
16
+
12
17
 
13
18
  class Checksum32(Codec):
14
19
  # override in sub-class
15
- checksum = None
20
+ checksum: Callable[["Buffer", int], int] | None = None
21
+ location: CHECKSUM_LOCATION = 'start'
22
+
23
+ def __init__(self, location: CHECKSUM_LOCATION | None = None):
24
+ if location is not None:
25
+ self.location = location
26
+ if self.location not in ['start', 'end']:
27
+ raise ValueError(f"Invalid checksum location: {self.location}")
16
28
 
17
29
  def encode(self, buf):
18
30
  arr = ensure_contiguous_ndarray(buf).view('u1')
19
31
  checksum = self.checksum(arr) & 0xFFFFFFFF
20
32
  enc = np.empty(arr.nbytes + 4, dtype='u1')
21
- enc[:4].view('<u4')[0] = checksum
22
- ndarray_copy(arr, enc[4:])
33
+ if self.location == 'start':
34
+ checksum_view = enc[:4]
35
+ payload_view = enc[4:]
36
+ else:
37
+ checksum_view = enc[-4:]
38
+ payload_view = enc[:-4]
39
+ checksum_view.view('<u4')[0] = checksum
40
+ ndarray_copy(arr, payload_view)
23
41
  return enc
24
42
 
25
43
  def decode(self, buf, out=None):
44
+ if len(buf) < 4:
45
+ raise ValueError("Input buffer is too short to contain a 32-bit checksum.")
46
+ if out is not None:
47
+ ensure_contiguous_ndarray(out) # check that out is a valid ndarray
48
+
26
49
  arr = ensure_contiguous_ndarray(buf).view('u1')
27
- expect = arr[:4].view('<u4')[0]
28
- checksum = self.checksum(arr[4:]) & 0xFFFFFFFF
50
+ if self.location == 'start':
51
+ checksum_view = arr[:4]
52
+ payload_view = arr[4:]
53
+ else:
54
+ checksum_view = arr[-4:]
55
+ payload_view = arr[:-4]
56
+ expect = checksum_view.view('<u4')[0]
57
+ checksum = self.checksum(payload_view) & 0xFFFFFFFF
29
58
  if expect != checksum:
30
- raise RuntimeError('checksum failed')
31
- return ndarray_copy(arr[4:], out)
59
+ raise RuntimeError(
60
+ f"Stored and computed {self.codec_id} checksum do not match. Stored: {expect}. Computed: {checksum}."
61
+ )
62
+ return ndarray_copy(payload_view, out)
32
63
 
33
64
 
34
65
  class CRC32(Checksum32):
66
+ """Codec add a crc32 checksum to the buffer.
67
+
68
+ Parameters
69
+ ----------
70
+ location : 'start' or 'end'
71
+ Where to place the checksum in the buffer.
72
+ """
73
+
35
74
  codec_id = 'crc32'
36
75
  checksum = zlib.crc32
76
+ location = 'start'
77
+
78
+
79
+ class CRC32C(Checksum32):
80
+ """Codec add a crc32c checksum to the buffer.
81
+
82
+ Parameters
83
+ ----------
84
+ location : 'start' or 'end'
85
+ Where to place the checksum in the buffer.
86
+ """
87
+
88
+ codec_id = 'crc32c'
89
+
90
+ def checksum(self, buf):
91
+ try:
92
+ from crc32c import crc32c as crc32c_
93
+
94
+ return crc32c_(buf)
95
+ except ImportError: # pragma: no cover
96
+ raise ImportError("crc32c must be installed to use the CRC32C checksum codec.")
97
+
98
+ location = 'end'
37
99
 
38
100
 
39
101
  class Adler32(Checksum32):
102
+ """Codec add a adler32 checksum to the buffer.
103
+
104
+ Parameters
105
+ ----------
106
+ location : 'start' or 'end'
107
+ Where to place the checksum in the buffer.
108
+ """
109
+
40
110
  codec_id = 'adler32'
41
111
  checksum = zlib.adler32
112
+ location = 'start'
42
113
 
43
114
 
44
115
  class JenkinsLookup3(Checksum32):
@@ -52,9 +123,12 @@ class JenkinsLookup3(Checksum32):
52
123
  the data portion and compared with the four-byte checksum, raising
53
124
  RuntimeError if inconsistent.
54
125
 
55
- Attributes:
56
- initval: initial seed passed to the hash algorithm, default: 0
57
- prefix: bytes prepended to the buffer before evaluating the hash, default: None
126
+ Parameters
127
+ ----------
128
+ initval : int
129
+ initial seed passed to the hash algorithm, default: 0
130
+ prefix : int
131
+ bytes prepended to the buffer before evaluating the hash, default: None
58
132
  """
59
133
 
60
134
  checksum = jenkins_lookup3
numcodecs/compat.py CHANGED
@@ -1,9 +1,5 @@
1
- # flake8: noqa
2
- import functools
3
- import sys
4
- import codecs
5
1
  import array
6
- from functools import reduce
2
+ import codecs
7
3
 
8
4
  import numpy as np
9
5
 
@@ -104,7 +100,7 @@ def ensure_contiguous_ndarray_like(buf, max_buffer_size=None, flatten=True) -> N
104
100
 
105
101
  # check for datetime or timedelta ndarray, the buffer interface doesn't support those
106
102
  if arr.dtype.kind in "Mm":
107
- arr = arr.view(np.int64)
103
+ arr = arr.view(np.int64) # type: ignore[arg-type]
108
104
 
109
105
  # check memory is contiguous, if so flatten
110
106
  if arr.flags.c_contiguous or arr.flags.f_contiguous:
@@ -115,13 +111,13 @@ def ensure_contiguous_ndarray_like(buf, max_buffer_size=None, flatten=True) -> N
115
111
  raise ValueError("an array with contiguous memory is required")
116
112
 
117
113
  if max_buffer_size is not None and arr.nbytes > max_buffer_size:
118
- msg = "Codec does not support buffers of > {} bytes".format(max_buffer_size)
114
+ msg = f"Codec does not support buffers of > {max_buffer_size} bytes"
119
115
  raise ValueError(msg)
120
116
 
121
117
  return arr
122
118
 
123
119
 
124
- def ensure_contiguous_ndarray(buf, max_buffer_size=None, flatten=True) -> np.array:
120
+ def ensure_contiguous_ndarray(buf, max_buffer_size=None, flatten=True) -> np.ndarray:
125
121
  """Convenience function to coerce `buf` to a numpy array, if it is not already a
126
122
  numpy array. Also ensures that the returned value exports fully contiguous memory,
127
123
  and supports the new-style buffer interface. If the optional max_buffer_size is
Binary file
numcodecs/delta.py CHANGED
@@ -1,6 +1,5 @@
1
1
  import numpy as np
2
2
 
3
-
4
3
  from .abc import Codec
5
4
  from .compat import ensure_ndarray, ndarray_copy
6
5
 
@@ -64,7 +63,11 @@ class Delta(Codec):
64
63
  enc[0] = arr[0]
65
64
 
66
65
  # compute differences
67
- enc[1:] = np.diff(arr)
66
+ # using np.subtract for in-place operations
67
+ if arr.dtype == bool:
68
+ np.not_equal(arr[1:], arr[:-1], out=enc[1:])
69
+ else:
70
+ np.subtract(arr[1:], arr[:-1], out=enc[1:])
68
71
 
69
72
  return enc
70
73
 
@@ -91,8 +94,8 @@ class Delta(Codec):
91
94
  return dict(id=self.codec_id, dtype=self.dtype.str, astype=self.astype.str)
92
95
 
93
96
  def __repr__(self):
94
- r = '{}(dtype={!r}'.format(type(self).__name__, self.dtype.str)
97
+ r = f'{type(self).__name__}(dtype={self.dtype.str!r}'
95
98
  if self.astype != self.dtype:
96
- r += ', astype=%r' % self.astype.str
99
+ r += f', astype={self.astype.str!r}'
97
100
  r += ')'
98
101
  return r
@@ -1,6 +1,5 @@
1
1
  import numpy as np
2
2
 
3
-
4
3
  from .abc import Codec
5
4
  from .compat import ensure_ndarray, ndarray_copy
6
5
 
@@ -15,7 +14,7 @@ class FixedScaleOffset(Codec):
15
14
  ----------
16
15
  offset : float
17
16
  Value to subtract from data.
18
- scale : int
17
+ scale : float
19
18
  Value to multiply by data.
20
19
  dtype : dtype
21
20
  Data type to use for decoded data.
@@ -126,13 +125,8 @@ class FixedScaleOffset(Codec):
126
125
  )
127
126
 
128
127
  def __repr__(self):
129
- r = '%s(scale=%s, offset=%s, dtype=%r' % (
130
- type(self).__name__,
131
- self.scale,
132
- self.offset,
133
- self.dtype.str,
134
- )
128
+ r = f'{type(self).__name__}(scale={self.scale}, offset={self.offset}, dtype={self.dtype.str!r}'
135
129
  if self.astype != self.dtype:
136
- r += ', astype=%r' % self.astype.str
130
+ r += f', astype={self.astype.str!r}'
137
131
  r += ')'
138
132
  return r
Binary file
Binary file
numcodecs/json.py CHANGED
@@ -1,10 +1,8 @@
1
1
  import json as _json
2
2
  import textwrap
3
3
 
4
-
5
4
  import numpy as np
6
5
 
7
-
8
6
  from .abc import Codec
9
7
  from .compat import ensure_text
10
8
 
@@ -97,12 +95,13 @@ class JSON(Codec):
97
95
  return config
98
96
 
99
97
  def __repr__(self):
100
- params = ['encoding=%r' % self._text_encoding]
98
+ params = [f'encoding={self._text_encoding!r}']
101
99
  for k, v in sorted(self._encoder_config.items()):
102
- params.append('{}={!r}'.format(k, v))
100
+ params.append(f'{k}={v!r}')
103
101
  for k, v in sorted(self._decoder_config.items()):
104
- params.append('{}={!r}'.format(k, v))
102
+ params.append(f'{k}={v!r}')
105
103
  classname = type(self).__name__
106
- r = '{}({})'.format(classname, ', '.join(params))
107
- r = textwrap.fill(r, width=80, break_long_words=False, subsequent_indent=' ')
108
- return r
104
+ params = ', '.join(params)
105
+ return textwrap.fill(
106
+ f'{classname}({params})', width=80, break_long_words=False, subsequent_indent=' '
107
+ )
Binary file
numcodecs/lzma.py CHANGED
@@ -1,16 +1,19 @@
1
- _lzma = None
1
+ from types import ModuleType
2
+ from typing import Optional
3
+
4
+ _lzma: Optional[ModuleType] = None
2
5
  try:
3
6
  import lzma as _lzma
4
7
  except ImportError: # pragma: no cover
5
8
  try:
6
- from backports import lzma as _lzma
9
+ from backports import lzma as _lzma # type: ignore[no-redef]
7
10
  except ImportError:
8
11
  pass
9
12
 
10
13
 
11
14
  if _lzma:
12
15
  from .abc import Codec
13
- from .compat import ndarray_copy, ensure_contiguous_ndarray
16
+ from .compat import ensure_contiguous_ndarray, ndarray_copy
14
17
 
15
18
  # noinspection PyShadowingBuiltins
16
19
  class LZMA(Codec):
@@ -66,11 +69,4 @@ if _lzma:
66
69
  return ndarray_copy(dec, out)
67
70
 
68
71
  def __repr__(self):
69
- r = '%s(format=%r, check=%r, preset=%r, filters=%r)' % (
70
- type(self).__name__,
71
- self.format,
72
- self.check,
73
- self.preset,
74
- self.filters,
75
- )
76
- return r
72
+ return f'{type(self).__name__}(format={self.format!r}, check={self.check!r}, preset={self.preset!r}, filters={self.filters!r})'
numcodecs/msgpacks.py CHANGED
@@ -1,6 +1,5 @@
1
- import numpy as np
2
1
  import msgpack
3
-
2
+ import numpy as np
4
3
 
5
4
  from .abc import Codec
6
5
  from .compat import ensure_contiguous_ndarray
@@ -84,6 +83,4 @@ class MsgPack(Codec):
84
83
  )
85
84
 
86
85
  def __repr__(self):
87
- return 'MsgPack(raw={!r}, use_bin_type={!r}, use_single_float={!r})'.format(
88
- self.raw, self.use_bin_type, self.use_single_float
89
- )
86
+ return f'MsgPack(raw={self.raw!r}, use_bin_type={self.use_bin_type!r}, use_single_float={self.use_single_float!r})'
numcodecs/ndarray_like.py CHANGED
@@ -1,7 +1,7 @@
1
- from typing import Any, Dict, Optional, Protocol, Tuple, Type, runtime_checkable
1
+ from typing import Any, ClassVar, Optional, Protocol, runtime_checkable
2
2
 
3
3
 
4
- class _CachedProtocolMeta(Protocol.__class__):
4
+ class _CachedProtocolMeta(Protocol.__class__): # type: ignore[name-defined]
5
5
  """Custom implementation of @runtime_checkable
6
6
 
7
7
  The native implementation of @runtime_checkable is slow,
@@ -11,7 +11,7 @@ class _CachedProtocolMeta(Protocol.__class__):
11
11
  isinstance checks using the object's class as the cache key.
12
12
  """
13
13
 
14
- _instancecheck_cache: Dict[Tuple[Type, Type], bool] = {}
14
+ _instancecheck_cache: ClassVar[dict[tuple[type, type], bool]] = {}
15
15
 
16
16
  def __instancecheck__(self, instance):
17
17
  key = (self, instance.__class__)
@@ -39,8 +39,8 @@ class FlagsObj(Protocol, metaclass=_CachedProtocolMeta):
39
39
  @runtime_checkable
40
40
  class NDArrayLike(Protocol, metaclass=_CachedProtocolMeta):
41
41
  dtype: DType
42
- shape: Tuple[int, ...]
43
- strides: Tuple[int, ...]
42
+ shape: tuple[int, ...]
43
+ strides: tuple[int, ...]
44
44
  ndim: int
45
45
  size: int
46
46
  itemsize: int
numcodecs/packbits.py CHANGED
@@ -1,6 +1,5 @@
1
1
  import numpy as np
2
2
 
3
-
4
3
  from .abc import Codec
5
4
  from .compat import ensure_ndarray, ndarray_copy
6
5
 
@@ -30,9 +29,6 @@ class PackBits(Codec):
30
29
 
31
30
  codec_id = 'packbits'
32
31
 
33
- def __init__(self):
34
- pass
35
-
36
32
  def encode(self, buf):
37
33
  # normalise input
38
34
  arr = ensure_ndarray(buf).view(bool)
numcodecs/pcodec.py CHANGED
@@ -1,11 +1,11 @@
1
- from typing import Optional, Literal
1
+ from typing import Literal, Optional
2
2
 
3
3
  import numcodecs
4
4
  import numcodecs.abc
5
5
  from numcodecs.compat import ensure_contiguous_ndarray
6
6
 
7
7
  try:
8
- from pcodec import standalone, ChunkConfig, PagingSpec, ModeSpec
8
+ from pcodec import ChunkConfig, ModeSpec, PagingSpec, standalone
9
9
  except ImportError: # pragma: no cover
10
10
  standalone = None
11
11
 
numcodecs/pickles.py CHANGED
@@ -52,4 +52,4 @@ class Pickle(Codec):
52
52
  return dict(id=self.codec_id, protocol=self.protocol)
53
53
 
54
54
  def __repr__(self):
55
- return 'Pickle(protocol=%s)' % self.protocol
55
+ return f'Pickle(protocol={self.protocol})'
numcodecs/quantize.py CHANGED
@@ -1,9 +1,7 @@
1
1
  import math
2
2
 
3
-
4
3
  import numpy as np
5
4
 
6
-
7
5
  from .abc import Codec
8
6
  from .compat import ensure_ndarray, ndarray_copy
9
7
 
@@ -65,12 +63,12 @@ class Quantize(Codec):
65
63
 
66
64
  # apply scaling
67
65
  precision = 10.0**-self.digits
68
- exp = math.log(precision, 10)
66
+ exp = math.log10(precision)
69
67
  if exp < 0:
70
68
  exp = int(math.floor(exp))
71
69
  else:
72
70
  exp = int(math.ceil(exp))
73
- bits = math.ceil(math.log(10.0**-exp, 2))
71
+ bits = math.ceil(math.log2(10.0**-exp))
74
72
  scale = 2.0**bits
75
73
  enc = np.around(scale * arr) / scale
76
74
 
@@ -95,12 +93,8 @@ class Quantize(Codec):
95
93
  )
96
94
 
97
95
  def __repr__(self):
98
- r = '%s(digits=%s, dtype=%r' % (
99
- type(self).__name__,
100
- self.digits,
101
- self.dtype.str,
102
- )
96
+ r = f'{type(self).__name__}(digits={self.digits}, dtype={self.dtype.str!r}'
103
97
  if self.astype != self.dtype:
104
- r += ', astype=%r' % self.astype.str
98
+ r += f', astype={self.astype.str!r}'
105
99
  r += ')'
106
100
  return r
numcodecs/registry.py CHANGED
@@ -1,12 +1,14 @@
1
1
  """The registry module provides some simple convenience functions to enable
2
2
  applications to dynamically register and look-up codec classes."""
3
3
 
4
- from importlib.metadata import entry_points
5
4
  import logging
5
+ from importlib.metadata import EntryPoints, entry_points
6
+
7
+ from numcodecs.abc import Codec
6
8
 
7
9
  logger = logging.getLogger("numcodecs")
8
- codec_registry = {}
9
- entries = {}
10
+ codec_registry: dict[str, Codec] = {}
11
+ entries: dict[str, "EntryPoints"] = {}
10
12
 
11
13
 
12
14
  def run_entrypoints():
@@ -42,14 +44,13 @@ def get_codec(config):
42
44
  config = dict(config)
43
45
  codec_id = config.pop('id', None)
44
46
  cls = codec_registry.get(codec_id)
45
- if cls is None:
46
- if codec_id in entries:
47
- logger.debug("Auto loading codec '%s' from entrypoint", codec_id)
48
- cls = entries[codec_id].load()
49
- register_codec(cls, codec_id=codec_id)
47
+ if cls is None and codec_id in entries:
48
+ logger.debug("Auto loading codec '%s' from entrypoint", codec_id)
49
+ cls = entries[codec_id].load()
50
+ register_codec(cls, codec_id=codec_id)
50
51
  if cls:
51
52
  return cls.from_config(config)
52
- raise ValueError('codec not available: %r' % codec_id)
53
+ raise ValueError(f'codec not available: {codec_id!r}')
53
54
 
54
55
 
55
56
  def register_codec(cls, codec_id=None):