Databases Reference
In-Depth Information
>>> path in grp
True
Multilevel Iteration with the Visitor Pattern
Basic iteration works fine for the contents of a single group. But what if you want to
iterate over every single object in the file? Or all objects “below” a certain group?
In the HDF5 world, this is accomplished by visitor iteration. Rather than HDF5 sup‐
plying you with an iterable, you provide a callable and HDF5 calls it with an argument
or two for every object.
Visit by Name
Your entry point is the visit method on the Group class. Let's create a simple file to test
it out:
>>> f = h5py . File ( 'visit_test.hdf5' , 'w' )
>>> f . create_dataset ( 'top_dataset' , data = 1.0 )
>>> f . create_group ( 'top_group_1' )
>>> f . create_group ( 'top_group_1/subgroup_1' )
>>> f . create_dataset ( 'top_group_1/subgroup_1/sub_dataset_1' , data = 1.0 )
>>> f . create_group ( 'top_group_2' )
>>> f . create_dataset ( 'top_group_2/sub_dataset_2' , data = 1.0 )
We can supply any callable to visit , which takes one argument, the object name:
>>> def printname ( name ):
... print name
>>> f . visit ( printname )
top_dataset
top_group_1
top_group_1/subgroup_1
top_group_1/subgroup_1/sub_dataset_1
top_group_2
top_group_2/sub_dataset_2
No particular order is guaranteed, except that when visit enters a subgroup, all the
members will be visited before moving on to the next subgroup. For example, everything
under top_group_1 is listed together, and so is everything under top_group_2 .
You're not required to visit the entire file; visit works just fine on subgroups:
>>> grp = f [ 'top_group_1' ]
>>> grp . visit ( printname )
subgroup_1
subgroup_1/sub_dataset_1
Search WWH ::




Custom Search