Databases Reference
In-Depth Information
You'll notice that instances of
SoftLink
and
ExternalLink
were returned, complete
with path information. This is the official way to retrieve such information after the link
is created.
For the hard links at
subgroup
and
dataset
, there's also an instance of something called
h5py.HardLink
. This exists solely to support the use of
get
; it has no other function and
no properties or methods.
Finally, if all you care about is the
kind
of link involved, and not the exact values of the
paths and files involved, you can combine the
getclass
and
getlink
keywords to return
the link class:
>>>
for
name
in
f
:
...
print
name
,
f
.
get
(
name
,
getclass
=
True
,
getlink
=
True
)
dataset <class 'h5py._hl.group.HardLink'>
extlink <class 'h5py._hl.group.ExternalLink'>
softlink <class 'h5py._hl.group.SoftLink'>
subgroup <class 'h5py._hl.group.HardLink'>
For many of the classes involved here, you may notice that they were
originally defined in the subpackage
h5py._hl
, for example
h5py._hl.group.SoftLink
shown earlier. This is an implementation
detail that may change; when doing
isinstance
checks, etc., use the
names directly attached to the
h5py
package (e.g.,
h5py.SoftLink
).
Using require to Simplify Your Application
Unlike Python dictionaries, you can't directly overwrite the members of a group:
>>>
f
=
h5py
.
File
(
'require_demo.hdf5'
,
'w'
)
>>>
f
.
create_group
(
'x'
)
>>>
f
.
create_group
(
'y'
)
>>>
f
.
create_group
(
'y'
)
ValueError: unable to create group (Symbol table: Unable to initialize object)
This also holds true for manually hard-linking objects:
>>>
f
[
'y'
]
=
f
[
'x'
]
ValueError: unable to create link (Links: Unable to initialize object)
This is an intentional feature designed to prevent data loss. Since objects are immediately
deleted when you unlink them from a group, you have to explicitly delete the link rather
than having HDF5 do it for you:
>>>
del
f
[
'y'
]
>>>
f
[
'y'
]
=
f
[
'x'
]
This leads to some headaches in real-world code. For example, a fragment of analysis
code might create a file and write the results to a dataset: