0
# Groups and Navigation
1
2
PyTables uses a hierarchical group structure similar to a filesystem for organizing data nodes. Groups serve as containers that can hold other groups and data nodes (tables, arrays), providing logical organization and namespace management for complex datasets.
3
4
## Capabilities
5
6
### Group Creation and Management
7
8
```python { .api }
9
class Group:
10
def __init__(self, parentnode, name, title="", new=False, filters=None):
11
"""Group constructor (typically called via File.create_group)."""
12
13
def _f_walknodes(self, classname=None):
14
"""
15
Walk all nodes in this group and its subgroups.
16
17
Parameters:
18
- classname (str): Filter by node class name
19
20
Yields:
21
Node: Each node in traversal order
22
"""
23
24
def _f_list_nodes(self, classname=None):
25
"""
26
List immediate child nodes.
27
28
Parameters:
29
- classname (str): Filter by node class name
30
31
Returns:
32
list: Child nodes of specified class
33
"""
34
35
def __contains__(self, name):
36
"""
37
Check if child node exists.
38
39
Parameters:
40
- name (str): Child node name
41
42
Returns:
43
bool: True if child exists
44
"""
45
46
def __getitem__(self, name):
47
"""
48
Get child node by name.
49
50
Parameters:
51
- name (str): Child node name
52
53
Returns:
54
Node: Child node object
55
"""
56
57
def __iter__(self):
58
"""Iterate over immediate child nodes."""
59
```
60
61
### Navigation and Path Operations
62
63
```python { .api }
64
def get_node(file, where, name=None, classname=None):
65
"""
66
Retrieve node by path.
67
68
Parameters:
69
- file (File): PyTables file object
70
- where (str): Path to node or parent location
71
- name (str): Node name if where is parent
72
- classname (str): Expected class for validation
73
74
Returns:
75
Node: Retrieved node object
76
"""
77
78
def walk_nodes(file, where="/", classname=None):
79
"""
80
Walk all nodes in hierarchy.
81
82
Parameters:
83
- file (File): PyTables file object
84
- where (str): Starting location
85
- classname (str): Filter by class name
86
87
Yields:
88
Node: Each node in traversal order
89
"""
90
```
91
92
## Usage Examples
93
94
```python
95
import tables as tb
96
97
with tb.open_file("hierarchy.h5", "w") as h5file:
98
# Create nested group structure
99
exp_group = h5file.create_group("/", "experiment", "Main Experiment")
100
data_group = h5file.create_group(exp_group, "data", "Raw Data")
101
analysis_group = h5file.create_group(exp_group, "analysis", "Analysis Results")
102
103
# Create data in different groups
104
raw_table = h5file.create_table(data_group, "measurements", MyDescription)
105
results_array = h5file.create_array(analysis_group, "summary", [1, 2, 3])
106
107
# Navigate hierarchy
108
node = h5file.get_node("/experiment/data/measurements")
109
110
# List children
111
children = exp_group._f_list_nodes()
112
113
# Walk entire tree
114
for node in h5file.walk_nodes("/"):
115
print(f"Found: {node._v_pathname}")
116
```