Skip to content

Commit c9deed3

Browse files
committed
Add examples of CTable integration with TreeStore in docstrings
1 parent f2a867a commit c9deed3

1 file changed

Lines changed: 112 additions & 3 deletions

File tree

src/blosc2/tree_store.py

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ class TreeStore(DictStore):
128128
129129
Examples
130130
--------
131+
Store plain arrays in a hierarchy:
132+
131133
>>> tstore = TreeStore(localpath="my_tstore.b2z", mode="w")
132-
>>> # Create a hierarchy. Data is stored in leaf nodes.
133-
>>> # Structural nodes like /child0 and /child0/child1 are created automatically.
134+
>>> # Data lives in leaf nodes; structural nodes are created automatically.
134135
>>> tstore["/child0/leaf1"] = np.array([1, 2, 3])
135136
>>> tstore["/child0/child1/leaf2"] = np.array([4, 5, 6])
136137
>>> tstore["/child0/child2"] = np.array([7, 8, 9])
@@ -146,6 +147,27 @@ class TreeStore(DictStore):
146147
>>> sorted(list(subtree.keys()))
147148
['/child1/leaf2', '/child2', '/leaf1']
148149
150+
Mix NDArrays and CTables in the same bundle:
151+
152+
>>> import dataclasses
153+
>>> @dataclasses.dataclass
154+
... class Row:
155+
... x: int = 0
156+
... y: float = 0.0
157+
>>> table = blosc2.CTable(Row)
158+
>>> _ = table.append(Row(x=1, y=1.5))
159+
>>> _ = table.append(Row(x=2, y=3.0))
160+
>>> with blosc2.TreeStore("bundle.b2z", mode="w") as ts:
161+
... ts["/data/array"] = blosc2.arange(5)
162+
... ts["/data/table"] = table
163+
>>> with blosc2.open("bundle.b2z", mode="r") as ts:
164+
... print(sorted(ts.keys()))
165+
... arr = ts["/data/array"]
166+
... tbl = ts["/data/table"]
167+
... print(type(tbl).__name__, len(tbl))
168+
['/data', '/data/array', '/data/table']
169+
CTable 2
170+
149171
"""
150172

151173
# For some reason, we had to revert the explicit parametrisation of the
@@ -328,6 +350,25 @@ def __setitem__(
328350
If key doesn't follow hierarchical structure rules, if trying to
329351
assign to a structural path that already has children, or if trying
330352
to add a child to a path that already contains data.
353+
354+
Examples
355+
--------
356+
Store an NDArray and a CTable together:
357+
358+
>>> import dataclasses
359+
>>> @dataclasses.dataclass
360+
... class Row:
361+
... x: int = 0
362+
>>> t = blosc2.CTable(Row)
363+
>>> _ = t.append(Row(x=10))
364+
>>> with blosc2.TreeStore("store.b2z", mode="w") as ts:
365+
... ts["/arr"] = blosc2.zeros(5, dtype="i4")
366+
... ts["/table"] = t # CTable stored inline
367+
368+
Replacing an existing object root requires an explicit delete first::
369+
370+
del ts["/table"]
371+
ts["/table"] = new_table
331372
"""
332373
key = self._validate_key(key)
333374

@@ -412,6 +453,25 @@ def __getitem__(
412453
If the key is a registered object root (e.g. CTable) returns that object.
413454
If the key is a structural intermediate path returns a subtree view.
414455
If the key is a leaf returns the stored array/schunk.
456+
457+
Examples
458+
--------
459+
>>> import dataclasses
460+
>>> @dataclasses.dataclass
461+
... class Row:
462+
... x: int = 0
463+
>>> t = blosc2.CTable(Row)
464+
>>> _ = t.append(Row(x=42))
465+
>>> with blosc2.TreeStore("store.b2z", mode="w") as ts:
466+
... ts["/arr"] = blosc2.zeros(3, dtype="i4")
467+
... ts["/group/val"] = blosc2.ones(2, dtype="f4")
468+
... ts["/table"] = t
469+
>>> with blosc2.open("store.b2z", mode="r") as ts:
470+
... arr = ts["/arr"] # NDArray leaf
471+
... sub = ts["/group"] # TreeStore subtree view
472+
... tbl = ts["/table"] # CTable object
473+
... print(type(arr).__name__, type(sub).__name__, type(tbl).__name__)
474+
NDArray TreeStore CTable
415475
"""
416476
key = self._validate_key(key)
417477
if self._is_vlmeta_key(key):
@@ -447,6 +507,21 @@ def __delitem__(self, key: str) -> None:
447507
If *key* is a registered object root, all its physical leaves and the
448508
registry entry are removed. If *key* has children, all descendants are
449509
removed recursively. Object internals cannot be deleted directly.
510+
511+
Examples
512+
--------
513+
>>> import dataclasses
514+
>>> @dataclasses.dataclass
515+
... class Row:
516+
... x: int = 0
517+
>>> t = blosc2.CTable(Row)
518+
>>> _ = t.append(Row(x=1))
519+
>>> with blosc2.TreeStore("store.b2z", mode="w") as ts:
520+
... ts["/arr"] = blosc2.zeros(3, dtype="i4")
521+
... ts["/table"] = t
522+
... del ts["/table"] # removes all CTable leaves + registry entry
523+
... print("/table" in ts)
524+
False
450525
"""
451526
key = self._validate_key(key)
452527

@@ -512,7 +587,26 @@ def _delete_object_subtree(self, full_key: str) -> None:
512587
self._modified = True
513588

514589
def __contains__(self, key: str) -> bool:
515-
"""Check if a key exists (includes object roots)."""
590+
"""Check if a key exists (includes object roots, excludes object internals).
591+
592+
Examples
593+
--------
594+
>>> import dataclasses
595+
>>> @dataclasses.dataclass
596+
... class Row:
597+
... x: int = 0
598+
>>> t = blosc2.CTable(Row)
599+
>>> _ = t.append(Row(x=7))
600+
>>> with blosc2.TreeStore("store.b2z", mode="w") as ts:
601+
... ts["/arr"] = blosc2.zeros(2, dtype="i4")
602+
... ts["/table"] = t
603+
... print("/table" in ts) # object root: True
604+
... print("/table/_meta" in ts) # internal key: False
605+
... print("/arr" in ts) # normal leaf: True
606+
True
607+
False
608+
True
609+
"""
516610
try:
517611
key = self._validate_key(key)
518612
if self._is_vlmeta_key(key):
@@ -529,6 +623,21 @@ def keys(self):
529623
530624
Object root keys (e.g. CTable) are included as single entries.
531625
Object-internal keys are hidden from normal traversal.
626+
627+
Examples
628+
--------
629+
>>> import dataclasses
630+
>>> @dataclasses.dataclass
631+
... class Row:
632+
... x: int = 0
633+
>>> t = blosc2.CTable(Row)
634+
>>> _ = t.append(Row(x=1))
635+
>>> with blosc2.TreeStore("store.b2z", mode="w") as ts:
636+
... ts["/arr"] = blosc2.zeros(3, dtype="i4")
637+
... ts["/group/val"] = blosc2.ones(2, dtype="f4")
638+
... ts["/table"] = t
639+
... print(sorted(ts.keys()))
640+
['/arr', '/group', '/group/val', '/table']
532641
"""
533642
if not self.subtree_path:
534643
all_keys = set(super().keys())

0 commit comments

Comments
 (0)