Databases Reference
In-Depth Information
Visiting Items
Given the name supplied to your callback, you could retrieve the object by simply using
getitem on the group you're iterating over:
>>> def printobj ( name ):
... print grp [ name ]
But that's a pain; since the name argument supplied by visit is a relative path, your
function has to know in advance what group it'll be applied to. The previous example
will work properly only when applied to grp .
Thankfully, HDF5 provides a more general way to deal with this. The method visit
items supplies both the relative name and an instance of each object:
>>> def printobj2 ( name , obj ):
... print name , obj
>>> grp . visititems ( printobj2 )
hardlink_to_dataset <HDF5 dataset "hardlink_to_dataset": shape (), type "<f8">
subgroup_1 <HDF5 group "/top_group_1/subgroup_1" (1 members)>
Since each object has to be opened, there is some overhead involved. You're better off
using visititems only in the case where you really need access to each object; for
example, if you need to inspect attributes.
One way to make visit a little more generic is by using the built-in Python widget
functools.partial . For example, here's a trivial function that prints the absolute path
of each object in the group:
>>> import posixpath
>>> from functools import partial
>>> def print_abspath ( somegroup , name ):
... """ Print *name* as an absolute path
... somegroup: HDF5 base group (*name* is relative to this)
... name: Object name relative to *somegroup*
... """
... print posixpath . join ( somegroup . name , name )
>>> grp . visit ( partial ( print_abspath , grp ))
/top_group_1/hardlink_to_dataset
/top_group_1/subgroup_1
Using this technique, you can avoid “embedding” the group you intend to iterate over
in the function itself.
Canceling Iteration: A Simple Search Mechanism
There's a simple way to “bail out” when visiting items. You might notice that our print
name function has no explicit return value; in Python that means that the function re‐
Search WWH ::




Custom Search