Databases Reference
In-Depth Information
If we were to create a hard link in the root group to the dataset, it would always point
to that particular object, even if the dataset were moved or unlinked from
mygroup
:
>>>
f
[
'hardlink'
]
=
dset
>>>
f
[
'hardlink'
]
==
grp
[
'dataset'
]
True
>>>
grp
.
move
(
'dataset'
,
'new_dataset_name'
)
>>>
f
[
'hardlink'
]
==
grp
[
'new_dataset_name'
]
True
Let's move the dataset back, and then create a
soft
link that points to the
path
/mygroup/dataset
. To tell HDF5 that we want to create a soft link, assign an
instance of the class
h5py.SoftLink
to a name in the file:
>>>
grp
.
move
(
'new_dataset_name'
,
'dataset'
)
>>>
f
[
'softlink'
]
=
h5py
.
SoftLink
(
'/mygroup/dataset'
)
>>>
f
[
'softlink'
]
==
grp
[
'dataset'
]
True
SoftLink
objects are very simple; they only have one property,
.path
, holding the path
provided when they are created:
>>>
softlink
=
h5py
.
SoftLink
(
'/some/path'
)
>>>
softlink
<SoftLink to "/some/path">
>>>
softlink
.
path
'/some/path'
Keep in mind that instances of
h5py.SoftLink
are purely a Python-side convenience,
not a wrapper around anything in HDF5. Nothing happens until you assign one of them
to a name in the file.
Returning to our example, since only the path is stored, if we move the dataset and
replace it with something else,
/softlink
would then point to the new object:
>>>
grp
.
move
(
'dataset'
,
'new_dataset_name'
)
>>>
dset2
=
grp
.
create_dataset
(
'dataset'
,
(
50
,))
>>>
f
[
'softlink'
]
==
dset
False
>>>
f
[
'softlink'
]
==
dset2
True
Soft links are therefore a great way to refer to “the object which resides at
/some/partic
ular/path
,” rather than any specific object in the file. This can be very handy if, for
example, a particular dataset represents some information that needs to be updated
without breaking all the links to it elsewhere in the file.
The value of a soft link is
not
checked when it's created. If you supply an invalid path
(or the object is later moved/deleted), accessing will fail with an exception. Because of
how HDF5 reports such an error, you will get the same exception as you would when
trying to access a nonexistent name in the group,
KeyError
: