Databases Reference
In-Depth Information
You might wonder what happens if your program crashes with open
files. If the program exits with a Python exception, don't worry! The
HDF library will automatically close every open file for you when the
application exits.
Use as a Context Manager
One of the coolest features introduced in Python 2.6 is support for
context managers
.
These are objects with a few special methods called on entry and exit from a block of
code, using the
with
statement. The classic example is the built-in Python
file
object:
>>>
with
open
(
"somefile.txt"
,
"w"
)
as
f
:
...
f
.
write
(
"Hello!"
)
The preceding code opens a brand-new
file
object, which is available in the code block
with the name
f
. When the block exits, the file is automatically closed (even if an ex‐
ception occurs!).
The
h5py.File
object supports exactly this kind of use. It's a great way to make sure the
file is always properly closed, without wrapping everything in
try
/
except
blocks:
>>>
with
h5py
.
File
(
"name.hdf5"
,
"w"
)
as
f
:
...
print
f
[
"missing_dataset"
]
KeyError: "unable to open object (Symbol table: Can't open object)"
>>>
print
f
<Closed HDF5 file>
File Drivers
File
drivers
sit between the filesystem and the higher-level world of HDF5 groups, da‐
tasets, and attributes. They deal with the mechanics of mapping the HDF5 “address
space” to an arrangement of bytes on disk. Typically you won't have to worry about
which driver is in use, as the default driver works well for most applications.
The great thing about drivers is that once the file is opened, they're totally transparent.
You just use the HDF5 library as normal, and the driver takes care of the storage me‐
chanics.
Here are a couple of the more interesting ones, which can be helpful for unusual prob‐
lems.
core driver
The
core
driver stores your file
entirely in memory
. Obviously there's a limit to how
much data you can store, but the trade-off is blazingly fast reads and writes. It's a great
choice when you want the speed of memory access, but also want to use the HDF5
structures. To enable, set the
driver
keyword to
"core"
: