forked from zarr-developers/zarr-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumcodec.py
More file actions
96 lines (77 loc) · 2.9 KB
/
numcodec.py
File metadata and controls
96 lines (77 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from typing import Any, Self, TypeGuard
from typing_extensions import Protocol
class Numcodec(Protocol):
"""
A protocol that models the ``numcodecs.abc.Codec`` interface.
This protocol should be considered experimental; expect the typing for `buf`, and `out` to become stricter.
"""
codec_id: str
def encode(self, buf: Any) -> Any: ...
"""Encode data in `buf`.
Parameters
----------
buf
Data to be encoded. May be any object supporting the new-style
buffer protocol.
Returns
-------
enc
Encoded data. May be any object supporting the new-style buffer
protocol.
"""
def decode(self, buf: Any, out: Any | None = None) -> Any: ...
"""
Decode data in `buf`.
Parameters
----------
buf : Any
Encoded data. May be any object supporting the new-style buffer
protocol.
out : Any
Writeable buffer to store decoded data. N.B. if provided, this buffer must
be exactly the right size to store the decoded data.
Returns
-------
dec : Any
Decoded data. May be any object supporting the new-style
buffer protocol.
"""
def get_config(self) -> Any: ...
"""
Return a dictionary holding configuration parameters for this
codec. Must include an 'id' field with the codec identifier. All
values must be compatible with JSON encoding.
"""
@classmethod
def from_config(cls, config: Any) -> Self: ...
"""
Instantiate codec from a configuration object.
"""
def _is_numcodec_cls(obj: object) -> TypeGuard[type[Numcodec]]:
"""
Check if the given object is a class implements the Numcodec protocol.
The @runtime_checkable decorator does not allow issubclass checks for protocols with non-method
members (i.e., attributes), so we use this function to manually check for the presence of the
required attributes and methods on a given object.
"""
return (
isinstance(obj, type)
and hasattr(obj, "codec_id")
and isinstance(obj.codec_id, str)
and hasattr(obj, "encode")
and callable(obj.encode)
and hasattr(obj, "decode")
and callable(obj.decode)
and hasattr(obj, "get_config")
and callable(obj.get_config)
and hasattr(obj, "from_config")
and callable(obj.from_config)
)
def _is_numcodec(obj: object) -> TypeGuard[Numcodec]:
"""
Check if the given object implements the Numcodec protocol.
The @runtime_checkable decorator does not allow issubclass checks for protocols with non-method
members (i.e., attributes), so we use this function to manually check for the presence of the
required attributes and methods on a given object.
"""
return _is_numcodec_cls(type(obj))