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
bagofholding/trie.py
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import random
|
|
2
|
+
from typing import cast
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
import pygtrie
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def decompose_stringtrie[ValueType](
|
|
9
|
+
trie: pygtrie.StringTrie, null_value: ValueType
|
|
10
|
+
) -> tuple[list[str], list[int], list[ValueType]]:
|
|
11
|
+
"""
|
|
12
|
+
While tries are a useful representation of path-like tree data, storing the
|
|
13
|
+
individual keys is not space-efficient.
|
|
14
|
+
|
|
15
|
+
This function decomposes the trie into a linked-list structure of individual path
|
|
16
|
+
segments and their upstream
|
|
17
|
+
components. Each segment then gets its associated value. Since not all individual
|
|
18
|
+
segments are necessarily valid
|
|
19
|
+
paths in the original tree, we also require a "null" value to keep all components
|
|
20
|
+
the same length.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
trie (pygtrie.StringTrie): The trie to decompose.
|
|
24
|
+
null_value (ValueType): What to store in lieu of a value for segments which do
|
|
25
|
+
not correspond to trie paths. Null values must implement a meaningful `==`
|
|
26
|
+
operator with the values of the trie.
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
list[str]: The path segments.
|
|
30
|
+
list[int]: Each segment's parent id in the segments list.
|
|
31
|
+
list[ValueType]: The value associated with each segment (taking the null value
|
|
32
|
+
whenever the segment has no associated path in the source trie).
|
|
33
|
+
"""
|
|
34
|
+
segments: list[str] = []
|
|
35
|
+
parents: list[int] = []
|
|
36
|
+
values: list[ValueType] = []
|
|
37
|
+
|
|
38
|
+
stack = [("", -1)]
|
|
39
|
+
while stack:
|
|
40
|
+
key, parent_idx = stack.pop()
|
|
41
|
+
idx = len(segments)
|
|
42
|
+
segment = key.split(trie._separator)[-1] if key else ""
|
|
43
|
+
segments.append(segment)
|
|
44
|
+
parents.append(parent_idx)
|
|
45
|
+
values.append(
|
|
46
|
+
trie.values(prefix=key, shallow=True)[0]
|
|
47
|
+
if trie.has_key(key)
|
|
48
|
+
else null_value
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
prefix = key
|
|
52
|
+
children = set()
|
|
53
|
+
for child_key in trie.keys(prefix=prefix):
|
|
54
|
+
remainder = child_key[len(prefix) :]
|
|
55
|
+
if remainder:
|
|
56
|
+
next_ = remainder.split(trie._separator)[1]
|
|
57
|
+
to_stack = prefix + trie._separator + next_
|
|
58
|
+
children.add(to_stack)
|
|
59
|
+
|
|
60
|
+
for child in children:
|
|
61
|
+
stack.append((child, idx))
|
|
62
|
+
|
|
63
|
+
return segments, parents, values
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def reconstruct_stringtrie[ValueType](
|
|
67
|
+
segments: list[str],
|
|
68
|
+
parents: list[int],
|
|
69
|
+
values: list[ValueType],
|
|
70
|
+
null_value: ValueType,
|
|
71
|
+
separator: str = "/",
|
|
72
|
+
) -> pygtrie.StringTrie:
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
While tries are a useful representation of path-like tree data, storing the
|
|
76
|
+
individual keys is not space-efficient.
|
|
77
|
+
|
|
78
|
+
This function reconstructs a trie based on a linked-list structure of individual
|
|
79
|
+
path segments and their upstream parent segments.
|
|
80
|
+
Since it is possible that not all segments are intended to be terminal paths in the
|
|
81
|
+
final trie, any segments whose value matches the provided "null" value will not
|
|
82
|
+
actually be added to the resulting trie.
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
segments (list[str]): The path segments.
|
|
86
|
+
parents (list[int]): The parent ids in the segments list.
|
|
87
|
+
values (list[ValueType]): The corresponding values to assign to the trie paths
|
|
88
|
+
(or do not add if equal to null values).
|
|
89
|
+
null_value (ValueType): The null value against which to compare values to see
|
|
90
|
+
if they should be excluded from the final trie. Null values must implement
|
|
91
|
+
a meaningful `==` operator with elements of the values list.
|
|
92
|
+
separator (str): The path separator.
|
|
93
|
+
|
|
94
|
+
Returns:
|
|
95
|
+
(pygtrie.StringTrie): The resulting trie.
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
trie = pygtrie.StringTrie(separator=separator)
|
|
99
|
+
keys = [""] * len(segments)
|
|
100
|
+
|
|
101
|
+
for i in range(len(segments)):
|
|
102
|
+
if parents[i] == -1:
|
|
103
|
+
keys[i] = ""
|
|
104
|
+
else:
|
|
105
|
+
p = keys[parents[i]]
|
|
106
|
+
keys[i] = p + ("" if p == "" else trie._separator) + segments[i]
|
|
107
|
+
|
|
108
|
+
for i, key in enumerate(keys):
|
|
109
|
+
if values[i] != null_value:
|
|
110
|
+
trie[trie._separator + key] = values[i]
|
|
111
|
+
|
|
112
|
+
return trie
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class Helper:
|
|
116
|
+
"""
|
|
117
|
+
Useful tools for building tries on which to test the decomposition of path-like
|
|
118
|
+
tree data.
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
@staticmethod
|
|
122
|
+
def compute_softmax_weights(
|
|
123
|
+
n: int, depth_propensity: float, temperature: float = 0.1
|
|
124
|
+
) -> list[float]:
|
|
125
|
+
pos = np.linspace(0, 1, n)
|
|
126
|
+
scores = (2 * depth_propensity - 1) * pos / temperature
|
|
127
|
+
exp_scores = np.exp(scores - np.max(scores)) # for numerical stability
|
|
128
|
+
return cast(list[float], (exp_scores / exp_scores.sum()).tolist())
|
|
129
|
+
|
|
130
|
+
@classmethod
|
|
131
|
+
def generate_paths(
|
|
132
|
+
cls,
|
|
133
|
+
n_paths: int,
|
|
134
|
+
depth_propensity: float = 0.5,
|
|
135
|
+
temperature: float = 0.1,
|
|
136
|
+
seed: int | None = None,
|
|
137
|
+
separator: str = "/",
|
|
138
|
+
) -> list[str]:
|
|
139
|
+
if seed is not None:
|
|
140
|
+
random.seed(seed)
|
|
141
|
+
|
|
142
|
+
paths: list[str] = []
|
|
143
|
+
next_id = 0
|
|
144
|
+
|
|
145
|
+
def new_segment() -> str:
|
|
146
|
+
nonlocal next_id
|
|
147
|
+
seg = f"seg{next_id}"
|
|
148
|
+
next_id += 1
|
|
149
|
+
return seg
|
|
150
|
+
|
|
151
|
+
frontier: list[list[str]] = [[]] # list of existing path prefixes
|
|
152
|
+
|
|
153
|
+
while len(paths) < n_paths:
|
|
154
|
+
# Choose existing prefix with bias towards depth
|
|
155
|
+
weights = cls.compute_softmax_weights(
|
|
156
|
+
len(frontier), depth_propensity, temperature
|
|
157
|
+
)
|
|
158
|
+
prefix = random.choices(frontier, weights=weights, k=1)[0]
|
|
159
|
+
|
|
160
|
+
new_path = prefix + [new_segment()]
|
|
161
|
+
paths.append(separator + separator.join(new_path))
|
|
162
|
+
|
|
163
|
+
# Allow extending this path later
|
|
164
|
+
frontier.append(new_path)
|
|
165
|
+
|
|
166
|
+
return paths
|
|
167
|
+
|
|
168
|
+
@classmethod
|
|
169
|
+
def make_stochastic_trie(
|
|
170
|
+
cls, n_paths: int, depth_propensity: float = 0.5, temperature: float = 0.1
|
|
171
|
+
) -> tuple[pygtrie.StringTrie, int]:
|
|
172
|
+
trie = pygtrie.StringTrie()
|
|
173
|
+
for i, k in enumerate(
|
|
174
|
+
cls.generate_paths(n_paths, depth_propensity, temperature)
|
|
175
|
+
):
|
|
176
|
+
trie[k] = i
|
|
177
|
+
null = -1 # real values are >0
|
|
178
|
+
return trie, null
|
bagofholding/widget.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
import ipytree
|
|
6
|
+
import traitlets
|
|
7
|
+
|
|
8
|
+
from bagofholding.content import Reducible
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from bagofholding.bag import Bag
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BagTree(ipytree.Tree): # type: ignore
|
|
15
|
+
# Silence complaints about subclassing `Any` because of upstream issues
|
|
16
|
+
"""
|
|
17
|
+
A widget for more convenient bag browsing inside notebooks.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
# pyiron_snippets.import_alarm.ImportAlarm.__call__ is not correctly passing on
|
|
21
|
+
# the hint
|
|
22
|
+
def __init__(self, bag: Bag) -> None:
|
|
23
|
+
super().__init__(multiple_selection=False)
|
|
24
|
+
self.bag = bag
|
|
25
|
+
self.root_path = bag.storage_root
|
|
26
|
+
self.selected_entry = None
|
|
27
|
+
self.observe(self._on_select, names=["selected_nodes"])
|
|
28
|
+
|
|
29
|
+
self.root = ipytree.Node("Bag", open=True, disabled=True, icon="shopping-bag")
|
|
30
|
+
self.add_node(self.root)
|
|
31
|
+
|
|
32
|
+
for key, value in bag.bag_info.field_items():
|
|
33
|
+
self.root.add_node(
|
|
34
|
+
ipytree.Node(f"{key}: {value}", disabled=True, icon="info")
|
|
35
|
+
)
|
|
36
|
+
self.object = self._create_node(self.root_path)
|
|
37
|
+
self.root.add_node(self.object)
|
|
38
|
+
|
|
39
|
+
def _create_node(self, path: str) -> ipytree.Node:
|
|
40
|
+
metadata = self.bag[path]
|
|
41
|
+
try:
|
|
42
|
+
subentries = tuple(k for k in self.bag.open_group(path))
|
|
43
|
+
except TypeError:
|
|
44
|
+
subentries = None
|
|
45
|
+
|
|
46
|
+
label_base = path.split("/")[-1]
|
|
47
|
+
truncated_content = metadata.content_type.lstrip(Reducible.__module__)
|
|
48
|
+
label = f"{label_base} ({truncated_content})"
|
|
49
|
+
|
|
50
|
+
icon = "file"
|
|
51
|
+
style = "default"
|
|
52
|
+
if metadata.content_type == f"{Reducible.__module__}.{Reducible.__qualname__}":
|
|
53
|
+
icon = "code"
|
|
54
|
+
style = "success"
|
|
55
|
+
elif subentries is not None:
|
|
56
|
+
icon = "folder"
|
|
57
|
+
style = "success" if len(subentries) > 0 else "warning"
|
|
58
|
+
|
|
59
|
+
node = ipytree.Node(
|
|
60
|
+
label,
|
|
61
|
+
[],
|
|
62
|
+
opened=False,
|
|
63
|
+
icon=icon,
|
|
64
|
+
open_icon_style=style,
|
|
65
|
+
close_icon_style="danger",
|
|
66
|
+
)
|
|
67
|
+
node.entry_metadata = metadata
|
|
68
|
+
node.entry_subentries = subentries
|
|
69
|
+
node.tree_metadata = {"path": path, "loaded": False}
|
|
70
|
+
|
|
71
|
+
# Placeholder for lazy loading subentries
|
|
72
|
+
if subentries and len(subentries) > 0:
|
|
73
|
+
node.add_node(ipytree.Node("Loading...", disabled=True))
|
|
74
|
+
node.observe(self._load_subentries, names=["opened"])
|
|
75
|
+
|
|
76
|
+
return node
|
|
77
|
+
|
|
78
|
+
def _load_subentries(self, change: traitlets.Bunch) -> None:
|
|
79
|
+
node = change["owner"]
|
|
80
|
+
|
|
81
|
+
if node.tree_metadata["loaded"]:
|
|
82
|
+
return
|
|
83
|
+
|
|
84
|
+
# Clear placeholder
|
|
85
|
+
node.nodes = []
|
|
86
|
+
|
|
87
|
+
if node.entry_metadata:
|
|
88
|
+
for key, value in node.entry_metadata.field_items():
|
|
89
|
+
if value is not None and value != "":
|
|
90
|
+
node.add_node(
|
|
91
|
+
ipytree.Node(f"{key}: {value}", disabled=True, icon="info")
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
subentries = node.entry_subentries
|
|
95
|
+
for sub in subentries:
|
|
96
|
+
sub_node = self._create_node(f"{node.tree_metadata['path']}/{sub}")
|
|
97
|
+
node.add_node(sub_node)
|
|
98
|
+
|
|
99
|
+
node.tree_metadata["loaded"] = True
|
|
100
|
+
|
|
101
|
+
def _on_select(self, change: traitlets.Bunch) -> None:
|
|
102
|
+
if change["new"]:
|
|
103
|
+
self.selected_entry = change["new"][0].tree_metadata["path"]
|
|
104
|
+
|
|
105
|
+
def load_selected(self) -> object:
|
|
106
|
+
if self.selected_entry is None:
|
|
107
|
+
raise ValueError("No entry selected")
|
|
108
|
+
print(f"Loading {self.selected_entry}")
|
|
109
|
+
return self.bag.load(self.selected_entry)
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bagofholding
|
|
3
|
+
Version: 0.post0.dev1
|
|
4
|
+
Summary: bagofholding - browsable, partially-reloadable serialization for pickleable python objects.
|
|
5
|
+
Author-email: Liam Huber <liamhuber@greyhavensolutions.com>
|
|
6
|
+
License: BSD 3-Clause License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025, Karlsruhe Institute of Technology - Institute of Nanotechnology
|
|
9
|
+
|
|
10
|
+
Redistribution and use in source and binary forms, with or without
|
|
11
|
+
modification, are permitted provided that the following conditions are met:
|
|
12
|
+
|
|
13
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
14
|
+
list of conditions and the following disclaimer.
|
|
15
|
+
|
|
16
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
17
|
+
this list of conditions and the following disclaimer in the documentation
|
|
18
|
+
and/or other materials provided with the distribution.
|
|
19
|
+
|
|
20
|
+
* Neither the name of the copyright holder nor the names of its
|
|
21
|
+
contributors may be used to endorse or promote products derived from
|
|
22
|
+
this software without specific prior written permission.
|
|
23
|
+
|
|
24
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
25
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
26
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
27
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
28
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
29
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
30
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
31
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
32
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
33
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
34
|
+
|
|
35
|
+
Project-URL: Homepage, https://pyiron.org/
|
|
36
|
+
Project-URL: Documentation, https://bagofholding.readthedocs.io
|
|
37
|
+
Project-URL: Repository, https://github.com/pyiron/bagofholding
|
|
38
|
+
Keywords: pyiron
|
|
39
|
+
Classifier: Development Status :: 3 - Alpha
|
|
40
|
+
Classifier: Topic :: Scientific/Engineering
|
|
41
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
42
|
+
Classifier: Intended Audience :: Science/Research
|
|
43
|
+
Classifier: Operating System :: OS Independent
|
|
44
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
45
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
46
|
+
Requires-Python: <3.14,>=3.12
|
|
47
|
+
Description-Content-Type: text/markdown
|
|
48
|
+
License-File: LICENSE
|
|
49
|
+
Requires-Dist: bidict==0.23.1
|
|
50
|
+
Requires-Dist: h5py<3.15.0,>=3.14.0
|
|
51
|
+
Requires-Dist: mpi4py<4.1.0,>=4.0.1
|
|
52
|
+
Requires-Dist: numpy<2.4.0,>=2.3.0
|
|
53
|
+
Requires-Dist: pygtrie<2.6.0,>=2.5.0
|
|
54
|
+
Requires-Dist: pyiron_snippets==0.2.0
|
|
55
|
+
Provides-Extra: widget
|
|
56
|
+
Requires-Dist: ipytree==0.2.2; extra == "widget"
|
|
57
|
+
Requires-Dist: traitlets==5.14.3; extra == "widget"
|
|
58
|
+
Dynamic: license-file
|
|
59
|
+
|
|
60
|
+
# bagofholding
|
|
61
|
+
|
|
62
|
+
`bagofholding` is designed to be an easy stand-in for `pickle` serialization for python object that is transparent, flexible, and suitable for long-term storage.
|
|
63
|
+
|
|
64
|
+
## Advantages
|
|
65
|
+
### Drop-in replacement
|
|
66
|
+
|
|
67
|
+
`bagofholding` stores (almost) any `pickle`-able python object, and can be easily used as a drop-in replacement for `pickle` serialization:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
>>> import bagofholding as boh
|
|
71
|
+
>>>
|
|
72
|
+
>>> boh.H5Bag.save(42, "file.h5")
|
|
73
|
+
>>> print(boh.H5Bag("file.h5").load())
|
|
74
|
+
42
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
### Browseable
|
|
80
|
+
|
|
81
|
+
The contents of stored objects can be browsed without actually re-instantiating any of the stored data.
|
|
82
|
+
In the example above, we saw that saving is a class-method, while loading is an instance method.
|
|
83
|
+
We can grab the "bag" instance and use it to peek at what's inside!
|
|
84
|
+
|
|
85
|
+
Let's use a slightly more complex object.
|
|
86
|
+
Readers familiar with `pickle` will be able to see that the "reduced" structure of the object is captured in the structure of the storage itself:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
>>> class MyThing:
|
|
90
|
+
... def __init__(self, answer: int, question: str):
|
|
91
|
+
... self.answer = answer
|
|
92
|
+
... self.question = question
|
|
93
|
+
>>>
|
|
94
|
+
>>> something = MyThing(42, "still computing...")
|
|
95
|
+
>>> boh.H5Bag.save(something, "something.h5")
|
|
96
|
+
>>> bag = boh.H5Bag("something.h5")
|
|
97
|
+
>>> bag.list_paths()
|
|
98
|
+
['object', 'object/args', 'object/args/i0', 'object/constructor', 'object/item_iterator', 'object/kv_iterator', 'object/state', 'object/state/answer', 'object/state/question']
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Item-access on the bag object gives access to metadata stored alongside the actual serialized information:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
>>> bag["object"]
|
|
106
|
+
Metadata(content_type='bagofholding.content.Reducible', qualname='MyThing', module='__main__', version=None, meta=None)
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
For Jupyter users, we power-up browsing capabilities with a widget under `bag.browse()` which lets you navigate the tree and see both metadata values and stored types:
|
|
111
|
+
|
|
112
|
+

|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
### Partial-loading
|
|
116
|
+
|
|
117
|
+
Stored objects can also be re-instantiated _in part_ by leveraging their storage path:
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
>>> bag.load("object/state/answer")
|
|
121
|
+
42
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Note that we didn't re-instantiate any part of the object other than this one integer!
|
|
126
|
+
|
|
127
|
+
This feature is incredibly useful for long-term storage and data transferability, as the loading environment does not need to fully match the saving environment -- only the environment required to load the actual piece of data desired matches.
|
|
128
|
+
Consider some complex object which, ultimately, contains important or expensive-to-calculate numeric data, e.g. in the form of numpy array.
|
|
129
|
+
With `bagofholding`, you can pass this data to a colleague running a different python environment, or come back to it years later.
|
|
130
|
+
With only `bagofholding` and `numpy` installed, the end user can browse through the stored object, access, and load only the valuable numeric data without re-installing the entire original environment.
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
### Version control
|
|
134
|
+
|
|
135
|
+
In the examples above, we saw that version (and of course package) information is part of the stored metadata.
|
|
136
|
+
This is useful post-facto for knowing what packages need to be installed to properly load your serialized data.
|
|
137
|
+
You can also specify at load-time how strict or relaxed `bagofholding` should be in re-instantiating data if a stored version does not match the currently installed version, thus protecting you from flawed re-instantiations.
|
|
138
|
+
|
|
139
|
+
`bagofholding` also provides tools to act on this data a-priori.
|
|
140
|
+
To increase the likelihood that stored data will be accessible in the future, you can outlaw any (sub)objects coming from particular modules:
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
import bagofholding.exception
|
|
144
|
+
>> > try:
|
|
145
|
+
...
|
|
146
|
+
boh.H5Bag.save(something, "will_fail.h5", forbidden_modules=("__main__",))
|
|
147
|
+
... except bagofholding.exception.ModuleForbiddenError as e:
|
|
148
|
+
...
|
|
149
|
+
print(e)
|
|
150
|
+
Module
|
|
151
|
+
'__main__' is forbidden as a
|
|
152
|
+
source
|
|
153
|
+
of
|
|
154
|
+
stored
|
|
155
|
+
objects.Change
|
|
156
|
+
the
|
|
157
|
+
`forbidden_modules` or move
|
|
158
|
+
this
|
|
159
|
+
object
|
|
160
|
+
to
|
|
161
|
+
an
|
|
162
|
+
allowed
|
|
163
|
+
module.
|
|
164
|
+
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
And/or demand that all objects have an identifiable version that:
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
import bagofholding.exception
|
|
171
|
+
>> > try:
|
|
172
|
+
...
|
|
173
|
+
boh.H5Bag.save(something, "will_fail.h5", require_versions=True)
|
|
174
|
+
... except bagofholding.exception.NoVersionError as e:
|
|
175
|
+
...
|
|
176
|
+
print(e)
|
|
177
|
+
Could
|
|
178
|
+
not find
|
|
179
|
+
a
|
|
180
|
+
version
|
|
181
|
+
for __main__.Either disable `require_versions`, use `version_scraping` to find an existing version for this package, or add versioning to the unversioned package.
|
|
182
|
+
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Of course, metadata for the bag itself is also stored.
|
|
186
|
+
We saw this in the GUI snapshot above, but it can also be accessed directly by code:
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
>>> boh.H5Bag.get_bag_info()
|
|
190
|
+
H5Info(qualname='H5Bag', module='bagofholding.h5.bag', version='...', libver_str='latest')
|
|
191
|
+
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
(In reality you will see a version code, it is omitted here because this example is executed automatically in the test suite.)
|
|
195
|
+
|
|
196
|
+
## Going further
|
|
197
|
+
|
|
198
|
+
For a more in-depth look at the above features and to explore other aspects of `bagofholding`, check out [the tutorial notebook](../notebooks/tutorial.ipynb).
|
|
199
|
+
|
|
200
|
+
Finally, `bagofholding` prioritizes transparency in what is stored and ease-of-use for both savers and loaders/browsers.
|
|
201
|
+
As such, the current hdf5-based implementation is likely to be significantly less performant than raw pickling, due to the creation of many small datasets that allow the h5 file to directly replicate the underlying structure of the python objects being saved.
|
|
202
|
+
For objects which contain large `numpy` arrays, this disadvantage is significantly alleviated as we benefit from the very efficient treatment of such arrays in hdf5 and `h5py`.
|
|
203
|
+
For all other objects, the current `bagofholding.H5Bag` is still an appropriate choice when the robustness of long term storage is more pressing than optimizing storage space.
|
|
204
|
+
Other bag types may be available in the future.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
bagofholding/__init__.py,sha256=hvDyj9JCFzE7RPrXZYkl50BTgbf8fnJ5aKKikLdUybg,1133
|
|
2
|
+
bagofholding/_version.py,sha256=vftjL1DH55hleHAYRKHgnGKIvLeaRHOm549wVXODfWc,24657
|
|
3
|
+
bagofholding/bag.py,sha256=tDoPJYTbRc-R5GKM8sHIVxkln-azYsiW23PKwBawi1Y,8626
|
|
4
|
+
bagofholding/content.py,sha256=ax1uGrrRbOSi3Anv1FeN44fSvKXpZuRryhAMWWM9qZs,25788
|
|
5
|
+
bagofholding/exceptions.py,sha256=5igfroaf1Z1TX_nQNusDnQsgKb-gIaZwA-r-L7iw5DA,948
|
|
6
|
+
bagofholding/metadata.py,sha256=F7Yv6RB1p_BbW3dKSodx7AcMoUWUFX8zWd9iz6owwMg,6866
|
|
7
|
+
bagofholding/retrieve.py,sha256=kKKUUl5n9O-yBAjf-vegWZmoV8Z_KC0KVNnno0YmDDM,1805
|
|
8
|
+
bagofholding/trie.py,sha256=6pOVkdNoxhgt4j6MToVylgkLbC6euc-Htl7aEOKP5aQ,5964
|
|
9
|
+
bagofholding/widget.py,sha256=DCsSKxZ855lt5dgoxhT4_YoHbyqygO7D2rJxPaKtyWg,3608
|
|
10
|
+
bagofholding/h5/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
+
bagofholding/h5/bag.py,sha256=Rt-L8vTr1acwBg3_9A6xfkOYlIWXXh3-Lf4m_TwR-f8,5801
|
|
12
|
+
bagofholding/h5/content.py,sha256=xAHfeMsm6c8ZgQ0RvkFrF1ooDA3BhMPSx9e6MfG8FLM,970
|
|
13
|
+
bagofholding/h5/context.py,sha256=JT70idJPUF0jEtIpZObTrQ8Xcf595dFSEGyss7dYKR8,1613
|
|
14
|
+
bagofholding/h5/dtypes.py,sha256=7WKR0U-RsM2IAMPWnxDFCKgg9WLabD0j9Py72-fF8MM,1206
|
|
15
|
+
bagofholding/h5/triebag.py,sha256=fyYKweuMLms3wSDFB7_DTVsoMv9I1y8kLwVxxztQcYk,12119
|
|
16
|
+
bagofholding-0.post0.dev1.dist-info/licenses/LICENSE,sha256=bNZX-3pFSnVGhRfng8SbI2FgEKLZb2nGO5jdsbwjEtk,1542
|
|
17
|
+
bagofholding-0.post0.dev1.dist-info/METADATA,sha256=sdj6dRokpkT-kg4gMNANUeRJEdnUGQB1guanpVVN-Ik,8712
|
|
18
|
+
bagofholding-0.post0.dev1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
19
|
+
bagofholding-0.post0.dev1.dist-info/top_level.txt,sha256=og78k5hgd6O7f63WxbB1MMFXxKhigf3JO_8fFYvVaH8,13
|
|
20
|
+
bagofholding-0.post0.dev1.dist-info/RECORD,,
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Karlsruhe Institute of Technology - Institute of Nanotechnology
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
* Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
bagofholding
|