Databases Reference
In-Depth Information
turns None . If you return anything else, the visit or visititems method will immedi‐
ately stop and return that value .
Let's suppose that we want to find a dataset that has an attribute with a particular value:
>>> f [ 'top_group_2/sub_dataset_2' ] . attrs [ 'special' ] = 42
Here's a function that will find such an object, when supplied to visititems :
>>> def findspecial ( name , obj ):
... if obj . attrs . get ( 'special' ) == 42 :
... return obj
>>> out = f . visititems ( findspecial )
>>> out
<HDF5 dataset "sub_dataset_2": shape (), type "<f8">
Copying Objects
HDF5 includes built-in facilities for copying objects from one place to another, freeing
you from the tedious job of recursively walking the HDF5 tree, checking for duplicate
links, copying over attributes, etc.
Single-File Copying
Let's create a simple file to test this, with two groups and a dataset:
>>> f = h5py . File ( 'copytest' , 'w' )
>>> f . create_group ( 'mygroup' )
>>> f . create_group ( 'mygroup/subgroup' )
>>> f . create_dataset ( 'mygroup/apples' , ( 100 ,))
Copying a dataset is straightforward, and results in a brand-new dataset, not a reference
or link to the old one:
>>> f . copy ( '/mygroup/apples' , '/oranges' )
>>> f [ 'oranges' ] == f [ 'mygroup/apples' ]
False
The great thing about the built-in HDF5 copy() is that it correctly handles recursively
copying groups:
>>> f . copy ( 'mygroup' , 'mygroup2' )
>>> f . visit ( printname )
oranges
mygroup
mygroup/apples
mygroup/subgroup
mygroup2
mygroup2/apples
mygroup2/subgroup
Search WWH ::




Custom Search