-
-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5807cba
commit 5949610
Showing
5 changed files
with
154 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import io | ||
|
||
from zarr.core.group import AsyncGroup | ||
|
||
try: | ||
import rich | ||
import rich.console | ||
import rich.tree | ||
except ImportError as e: | ||
raise ImportError("'rich' is required for Group.tree") from e | ||
|
||
|
||
class TreeRepr: | ||
def __init__(self, tree: rich.tree.Tree) -> None: | ||
self.tree = tree | ||
|
||
def __repr__(self) -> str: | ||
console = rich.console.Console(file=io.StringIO()) | ||
console.print(self.tree) | ||
return str(console.file.getvalue()) | ||
|
||
|
||
async def group_tree_async(group: AsyncGroup, max_depth: int | None = None) -> TreeRepr: | ||
tree = rich.tree.Tree(label=f"[b]{group.name}[/b]") | ||
nodes = {"": tree} | ||
members = sorted([x async for x in group.members(max_depth=max_depth)]) | ||
|
||
for key, node in members: | ||
if key.count("/") == 0: | ||
parent_key = "" | ||
else: | ||
parent_key = key.rsplit("/", 1)[0] | ||
parent = nodes[parent_key] | ||
|
||
# We want what the spec calls the node "name", the part excluding all leading | ||
# /'s and path segments. But node.name includes all that, so we build it here. | ||
name = key.rsplit("/")[-1] | ||
if isinstance(node, AsyncGroup): | ||
label = f"[b]{name}[/b]" | ||
else: | ||
label = f"[b]{name}[/b] {node.shape} {node.dtype}" | ||
nodes[key] = parent.add(label) | ||
|
||
return TreeRepr(tree) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import textwrap | ||
from typing import Any | ||
|
||
import pytest | ||
|
||
import zarr | ||
|
||
|
||
@pytest.mark.parametrize("root_name", [None, "root"]) | ||
def test_tree(root_name: Any) -> None: | ||
g = zarr.group(path=root_name) | ||
A = g.create_group("A") | ||
B = g.create_group("B") | ||
C = B.create_group("C") | ||
D = C.create_group("C") | ||
|
||
A.create_array(name="x", shape=(2), dtype="float64") | ||
A.create_array(name="y", shape=(0,), dtype="int8") | ||
B.create_array(name="x", shape=(0,)) | ||
C.create_array(name="x", shape=(0,)) | ||
D.create_array(name="x", shape=(0,)) | ||
|
||
result = repr(g.tree()) | ||
root = root_name or "" | ||
|
||
expected = textwrap.dedent(f"""\ | ||
/{root} | ||
├── A | ||
│ ├── x (2,) float64 | ||
│ └── y (0,) int8 | ||
└── B | ||
├── C | ||
│ ├── C | ||
│ │ └── x (0,) float64 | ||
│ └── x (0,) float64 | ||
└── x (0,) float64 | ||
""") | ||
|
||
assert result == expected | ||
|
||
result = repr(g.tree(level=0)) | ||
expected = textwrap.dedent(f"""\ | ||
/{root} | ||
├── A | ||
└── B | ||
""") | ||
|
||
assert result == expected | ||
|
||
|
||
def test_expand_not_implemented() -> None: | ||
g = zarr.group() | ||
with pytest.raises(NotImplementedError): | ||
g.tree(expand=True) |