bagofholding 0.post0.dev1__py3-none-any.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.
- bagofholding/__init__.py +20 -0
- bagofholding/_version.py +716 -0
- bagofholding/bag.py +266 -0
- bagofholding/content.py +897 -0
- bagofholding/exceptions.py +44 -0
- bagofholding/h5/__init__.py +0 -0
- bagofholding/h5/bag.py +183 -0
- bagofholding/h5/content.py +29 -0
- bagofholding/h5/context.py +58 -0
- bagofholding/h5/dtypes.py +73 -0
- bagofholding/h5/triebag.py +322 -0
- bagofholding/metadata.py +197 -0
- bagofholding/retrieve.py +52 -0
- bagofholding/trie.py +178 -0
- bagofholding/widget.py +109 -0
- bagofholding-0.post0.dev1.dist-info/METADATA +204 -0
- bagofholding-0.post0.dev1.dist-info/RECORD +20 -0
- bagofholding-0.post0.dev1.dist-info/WHEEL +5 -0
- bagofholding-0.post0.dev1.dist-info/licenses/LICENSE +28 -0
- bagofholding-0.post0.dev1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""
|
|
2
|
+
A centralized location for exceptions.
|
|
3
|
+
|
|
4
|
+
Using custom exceptions allows us to be much more specific in the test suite.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class BagOfHoldingError(Exception):
|
|
11
|
+
"""A base class for raising bagofholding-related exceptions"""
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BagMismatchError(BagOfHoldingError, ValueError): ...
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class EnvironmentMismatchError(BagOfHoldingError, ModuleNotFoundError): ...
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class FileAlreadyOpenError(BagOfHoldingError): ...
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class FileNotOpenError(BagOfHoldingError): ...
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class FilepathError(BagOfHoldingError, FileExistsError): ...
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class InvalidMetadataError(BagOfHoldingError, ValueError): ...
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ModuleForbiddenError(BagOfHoldingError, ValueError): ...
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class NotAGroupError(BagOfHoldingError, TypeError): ...
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class NoVersionError(BagOfHoldingError, ValueError): ...
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class PickleProtocolError(BagOfHoldingError, ValueError): ...
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class StringReductionNotImportableError(BagOfHoldingError): ...
|
|
File without changes
|
bagofholding/h5/bag.py
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import dataclasses
|
|
4
|
+
import pathlib
|
|
5
|
+
from typing import Any, ClassVar, Self, cast
|
|
6
|
+
|
|
7
|
+
import h5py
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
from bagofholding.bag import Bag, BagInfo
|
|
11
|
+
from bagofholding.content import BespokeItem
|
|
12
|
+
from bagofholding.exceptions import NotAGroupError
|
|
13
|
+
from bagofholding.h5.content import Array, ArrayPacker, ArrayType
|
|
14
|
+
from bagofholding.h5.context import HasH5FileContext
|
|
15
|
+
from bagofholding.h5.dtypes import H5PY_DTYPE_WHITELIST
|
|
16
|
+
from bagofholding.metadata import Metadata, VersionScrapingMap, VersionValidatorType
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclasses.dataclass(frozen=True)
|
|
20
|
+
class H5Info(BagInfo):
|
|
21
|
+
libver_str: str = "latest"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class H5Bag(Bag, HasH5FileContext, ArrayPacker):
|
|
25
|
+
"""
|
|
26
|
+
A bag using HDF5 files based on `h5py`.
|
|
27
|
+
|
|
28
|
+
The underlying file structure is directly representative of the structure of the
|
|
29
|
+
decomposed object being stored, and `attrs` are used to store metadata.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
_content_key: ClassVar[str] = "content_type"
|
|
33
|
+
|
|
34
|
+
@classmethod
|
|
35
|
+
def get_bag_info(cls) -> BagInfo:
|
|
36
|
+
return H5Info(
|
|
37
|
+
qualname=cls.__qualname__,
|
|
38
|
+
module=cls.__module__,
|
|
39
|
+
version=cls.get_version(),
|
|
40
|
+
libver_str=cls.libver_str,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
@classmethod
|
|
44
|
+
def _bag_info_class(cls) -> type[BagInfo]:
|
|
45
|
+
return H5Info
|
|
46
|
+
|
|
47
|
+
def __init__(
|
|
48
|
+
self, filepath: str | pathlib.Path, *args: object, **kwargs: Any
|
|
49
|
+
) -> None:
|
|
50
|
+
self._file = None
|
|
51
|
+
self._context_depth = 0
|
|
52
|
+
super().__init__(filepath)
|
|
53
|
+
|
|
54
|
+
def _write(self) -> None:
|
|
55
|
+
self.close()
|
|
56
|
+
|
|
57
|
+
def _pack_bag_info(self) -> None:
|
|
58
|
+
self.open("w")
|
|
59
|
+
super()._pack_bag_info()
|
|
60
|
+
|
|
61
|
+
def _unpack_bag_info(self) -> BagInfo:
|
|
62
|
+
with self:
|
|
63
|
+
info = super()._unpack_bag_info()
|
|
64
|
+
return info
|
|
65
|
+
|
|
66
|
+
def load(
|
|
67
|
+
self,
|
|
68
|
+
path: str = Bag.storage_root,
|
|
69
|
+
version_validator: VersionValidatorType = "exact",
|
|
70
|
+
version_scraping: VersionScrapingMap | None = None,
|
|
71
|
+
) -> Any:
|
|
72
|
+
with self:
|
|
73
|
+
unpacked = super().load(
|
|
74
|
+
path=path,
|
|
75
|
+
version_validator=version_validator,
|
|
76
|
+
version_scraping=version_scraping,
|
|
77
|
+
)
|
|
78
|
+
return unpacked
|
|
79
|
+
|
|
80
|
+
def __getitem__(self, path: str) -> Metadata:
|
|
81
|
+
with self:
|
|
82
|
+
return super().__getitem__(path)
|
|
83
|
+
|
|
84
|
+
def list_paths(self) -> list[str]:
|
|
85
|
+
"""A list of all available content paths."""
|
|
86
|
+
paths: list[str] = []
|
|
87
|
+
with self:
|
|
88
|
+
self.file.visit(paths.append)
|
|
89
|
+
return paths
|
|
90
|
+
|
|
91
|
+
def __enter__(self) -> Self:
|
|
92
|
+
self._context_depth += 1
|
|
93
|
+
if self._file is None:
|
|
94
|
+
self.open("r")
|
|
95
|
+
return self
|
|
96
|
+
|
|
97
|
+
def _pack_field(self, path: str, key: str, value: str) -> None:
|
|
98
|
+
self.file[path].attrs[key] = value
|
|
99
|
+
|
|
100
|
+
def _unpack_field(self, path: str, key: str) -> str | None:
|
|
101
|
+
try:
|
|
102
|
+
return self.maybe_decode(self.file[path].attrs[key])
|
|
103
|
+
except KeyError:
|
|
104
|
+
return None
|
|
105
|
+
|
|
106
|
+
def pack_empty(self, path: str) -> None:
|
|
107
|
+
self.file.create_dataset(path, data=h5py.Empty(dtype="f"))
|
|
108
|
+
|
|
109
|
+
def pack_string(self, obj: str, path: str) -> None:
|
|
110
|
+
self.file.create_dataset(
|
|
111
|
+
path, data=obj, dtype=h5py.string_dtype(encoding="utf-8")
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
def unpack_string(self, path: str) -> str:
|
|
115
|
+
return cast(str, self._unpack_raw(path).decode("utf-8"))
|
|
116
|
+
|
|
117
|
+
def _pack_raw(self, obj: bytearray | bool | int | float, path: str) -> None:
|
|
118
|
+
self.file.create_dataset(path, data=obj)
|
|
119
|
+
|
|
120
|
+
def _unpack_raw(self, path: str) -> Any:
|
|
121
|
+
return self.file[path][()]
|
|
122
|
+
|
|
123
|
+
def pack_bool(self, obj: bool, path: str) -> None:
|
|
124
|
+
return self._pack_raw(obj, path)
|
|
125
|
+
|
|
126
|
+
def unpack_bool(self, path: str) -> bool:
|
|
127
|
+
return bool(self._unpack_raw(path))
|
|
128
|
+
|
|
129
|
+
def pack_long(self, obj: int, path: str) -> None:
|
|
130
|
+
return self._pack_raw(obj, path)
|
|
131
|
+
|
|
132
|
+
def unpack_long(self, path: str) -> int:
|
|
133
|
+
return int(self._unpack_raw(path))
|
|
134
|
+
|
|
135
|
+
def pack_float(self, obj: float, path: str) -> None:
|
|
136
|
+
return self._pack_raw(obj, path)
|
|
137
|
+
|
|
138
|
+
def unpack_float(self, path: str) -> float:
|
|
139
|
+
return float(self._unpack_raw(path))
|
|
140
|
+
|
|
141
|
+
def pack_complex(self, obj: complex, path: str) -> None:
|
|
142
|
+
self.file.create_dataset(path, data=np.array([obj.real, obj.imag]))
|
|
143
|
+
|
|
144
|
+
def unpack_complex(self, path: str) -> complex:
|
|
145
|
+
data = self._unpack_raw(path)
|
|
146
|
+
return complex(data[0], data[1])
|
|
147
|
+
|
|
148
|
+
def pack_bytes(self, obj: bytes, path: str) -> None:
|
|
149
|
+
self.file.create_dataset(path, data=np.void(obj))
|
|
150
|
+
|
|
151
|
+
def unpack_bytes(self, path: str) -> bytes:
|
|
152
|
+
return bytes(self._unpack_raw(path))
|
|
153
|
+
|
|
154
|
+
def pack_bytearray(self, obj: bytearray, path: str) -> None:
|
|
155
|
+
return self._pack_raw(obj, path)
|
|
156
|
+
|
|
157
|
+
def unpack_bytearray(self, path: str) -> bytearray:
|
|
158
|
+
return bytearray(self._unpack_raw(path))
|
|
159
|
+
|
|
160
|
+
def create_group(self, path: str) -> None:
|
|
161
|
+
self.file.create_group(path)
|
|
162
|
+
|
|
163
|
+
def open_group(self, path: str) -> list[str]:
|
|
164
|
+
with self:
|
|
165
|
+
group = self.file[path]
|
|
166
|
+
if not isinstance(group, h5py.Group):
|
|
167
|
+
raise NotAGroupError(f"Asked a group at {path}, got {type(group)}")
|
|
168
|
+
subcontent_names = list(group)
|
|
169
|
+
return subcontent_names
|
|
170
|
+
|
|
171
|
+
# def get_bespoke_content_class(self, obj: object) -> type[BespokeItem[Any, Self]] | None:
|
|
172
|
+
def get_bespoke_content_class(
|
|
173
|
+
self, obj: object
|
|
174
|
+
) -> type[BespokeItem[Any, Self]] | None:
|
|
175
|
+
if type(obj) is np.ndarray and obj.dtype in H5PY_DTYPE_WHITELIST:
|
|
176
|
+
return cast(type[BespokeItem[Any, Self]], Array)
|
|
177
|
+
return None
|
|
178
|
+
|
|
179
|
+
def pack_array(self, obj: ArrayType, path: str) -> None:
|
|
180
|
+
self.file.create_dataset(path, data=obj)
|
|
181
|
+
|
|
182
|
+
def unpack_array(self, path: str) -> ArrayType:
|
|
183
|
+
return cast(ArrayType, self.file[path][()])
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Designed and intended for use with H5 implementations, which treat arrays very nicely
|
|
2
|
+
# but since bags merely follow a protocol, there is nothing intrinsically-h5 here
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Protocol, TypeAlias
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
from bagofholding.content import BespokeItem, Packer, UnpackingArguments
|
|
10
|
+
from bagofholding.h5.dtypes import H5DtypeAlias
|
|
11
|
+
|
|
12
|
+
ArrayType: TypeAlias = np.ndarray[tuple[int, ...], H5DtypeAlias]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ArrayPacker(Packer, Protocol):
|
|
16
|
+
def pack_array(self, obj: ArrayType, path: str) -> None: ...
|
|
17
|
+
def unpack_array(self, path: str) -> ArrayType: ...
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Array(BespokeItem[ArrayType, ArrayPacker]):
|
|
21
|
+
@classmethod
|
|
22
|
+
def _pack_item(cls, obj: ArrayType, packer: ArrayPacker, path: str) -> None:
|
|
23
|
+
packer.pack_array(obj, path)
|
|
24
|
+
|
|
25
|
+
@classmethod
|
|
26
|
+
def unpack(
|
|
27
|
+
cls, packer: ArrayPacker, path: str, unpacking: UnpackingArguments
|
|
28
|
+
) -> ArrayType:
|
|
29
|
+
return packer.unpack_array(path)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import pathlib
|
|
2
|
+
from types import TracebackType
|
|
3
|
+
from typing import ClassVar, Literal
|
|
4
|
+
|
|
5
|
+
import h5py
|
|
6
|
+
|
|
7
|
+
from bagofholding.exceptions import FileAlreadyOpenError, FileNotOpenError
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class HasH5FileContext:
|
|
11
|
+
"""
|
|
12
|
+
A mixin class for context management with an :class:`h5py.File` object.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
libver_str: ClassVar[str] = "latest"
|
|
16
|
+
|
|
17
|
+
filepath: pathlib.Path
|
|
18
|
+
_file: h5py.File | None
|
|
19
|
+
_context_depth: int
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
def file(self) -> h5py.File:
|
|
23
|
+
if self._file is None:
|
|
24
|
+
raise FileNotOpenError(f"{self.filepath} is not open; use `open` or `with`")
|
|
25
|
+
return self._file
|
|
26
|
+
|
|
27
|
+
@file.setter
|
|
28
|
+
def file(self, new_file: h5py.File | None) -> None:
|
|
29
|
+
self._file = new_file
|
|
30
|
+
|
|
31
|
+
def open(self, mode: Literal["r", "r+", "w", "w-", "x", "a"]) -> h5py.File:
|
|
32
|
+
if self._file is None:
|
|
33
|
+
self.file = h5py.File(self.filepath, mode, libver=self.libver_str)
|
|
34
|
+
return self.file
|
|
35
|
+
else:
|
|
36
|
+
raise FileAlreadyOpenError(f"The bag at {self.filepath} is already open")
|
|
37
|
+
|
|
38
|
+
def __exit__(
|
|
39
|
+
self,
|
|
40
|
+
exc_type: type[BaseException] | None,
|
|
41
|
+
exc_val: BaseException | None,
|
|
42
|
+
exc_tb: TracebackType | None,
|
|
43
|
+
) -> None:
|
|
44
|
+
self._context_depth -= 1
|
|
45
|
+
if self._context_depth == 0:
|
|
46
|
+
self.close()
|
|
47
|
+
|
|
48
|
+
def close(self) -> None:
|
|
49
|
+
if self._file is not None:
|
|
50
|
+
self.file.close()
|
|
51
|
+
self._file = None
|
|
52
|
+
|
|
53
|
+
def __del__(self) -> None:
|
|
54
|
+
self.close()
|
|
55
|
+
|
|
56
|
+
@staticmethod
|
|
57
|
+
def maybe_decode(attr: str | bytes) -> str:
|
|
58
|
+
return attr if isinstance(attr, str) else attr.decode("utf-8")
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""
|
|
2
|
+
h5py supports efficient treatment of a subset of possible array.
|
|
3
|
+
|
|
4
|
+
Here, we whitelist these types.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import TypeAlias
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
H5PY_DTYPE_WHITELIST = (
|
|
12
|
+
np.int8,
|
|
13
|
+
np.int16,
|
|
14
|
+
np.int32,
|
|
15
|
+
np.int64,
|
|
16
|
+
np.uint8,
|
|
17
|
+
np.uint16,
|
|
18
|
+
np.uint32,
|
|
19
|
+
np.uint64,
|
|
20
|
+
np.float16,
|
|
21
|
+
np.float32,
|
|
22
|
+
np.float64,
|
|
23
|
+
np.complex64,
|
|
24
|
+
np.complex128,
|
|
25
|
+
np.bool_,
|
|
26
|
+
np.bytes_,
|
|
27
|
+
np.str_,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
H5Scalar: TypeAlias = (
|
|
32
|
+
np.int8
|
|
33
|
+
| np.int16
|
|
34
|
+
| np.int32
|
|
35
|
+
| np.int64
|
|
36
|
+
| np.uint8
|
|
37
|
+
| np.uint16
|
|
38
|
+
| np.uint32
|
|
39
|
+
| np.uint64
|
|
40
|
+
| np.float16
|
|
41
|
+
| np.float32
|
|
42
|
+
| np.float64
|
|
43
|
+
| np.complex64
|
|
44
|
+
| np.complex128
|
|
45
|
+
| np.bool_
|
|
46
|
+
| np.bytes_
|
|
47
|
+
| np.str_
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
IntTypesAlias: TypeAlias = (
|
|
52
|
+
np.dtype[np.int8]
|
|
53
|
+
| np.dtype[np.int16]
|
|
54
|
+
| np.dtype[np.int32]
|
|
55
|
+
| np.dtype[np.int64]
|
|
56
|
+
| np.dtype[np.uint8]
|
|
57
|
+
| np.dtype[np.uint16]
|
|
58
|
+
| np.dtype[np.uint32]
|
|
59
|
+
| np.dtype[np.uint64]
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
H5DtypeAlias: TypeAlias = (
|
|
64
|
+
IntTypesAlias
|
|
65
|
+
| np.dtype[np.float16]
|
|
66
|
+
| np.dtype[np.float32]
|
|
67
|
+
| np.dtype[np.float64]
|
|
68
|
+
| np.dtype[np.complex64]
|
|
69
|
+
| np.dtype[np.complex128]
|
|
70
|
+
| np.dtype[np.bool_]
|
|
71
|
+
| np.dtype[np.bytes_]
|
|
72
|
+
| np.dtype[np.str_]
|
|
73
|
+
)
|