Databases Reference
In-Depth Information
You can take the length of a group—note that this measures the number of objects
directly attached to the group rather than all the objects in nested subgroups:
>>> len ( f )
5
>>> len ( f [ "SubGroup" ])
2
Pythonic iteration is also supported using the familiar iteritems() and friends (see
“Iteration and Containership” on page 65 ).
Special Properties
There are a few widgets attached to groups (and datasets) that are very useful when
working with the hierarchy in a file.
First is the .file property. Attached to every object, this gives you a handy way to
retrieve a File object for the file in which your object resides:
>>> f = h5py . File ( 'propdemo.hdf5' , 'w' )
>>> grp = f . create_group ( 'hello' )
>>> grp . file == f
True
This is great when you want to check whether a file is read/write, or just get the filename.
Second is the .parent property. This returns the Group object that contains your object:
>>> grp . parent
<HDF5 group "/" (1 members)>
With these two properties, you can avoid most of the path-formatting headaches asso‐
ciated with filesystem work.
Working with Links
What does it mean to give an object a name in the file? From the preceding examples,
you might think that the name is part of the object, in the same way that the dtype or
shape are part of a dataset.
But this isn't the case. There's a layer between the group object and the objects that are
its members. The two are related by the concept of links .
Hard Links
Links in HDF5 are handled in much the same way as in modern filesystems. Objects
like datasets and groups don't have an intrinsic name; rather, they have an address (byte
offset) in the file that HDF5 has to look up. When you assign an object to a name in a
Search WWH ::




Custom Search