From a3add89f5a9cbe3dd3561059a72943ed1e0c55b5 Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Wed, 1 Apr 2015 12:56:19 -0400 Subject: [PATCH] * Added simple tools to read/write values to/from a HDF5 file. --- iofmt/hdf5.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 iofmt/hdf5.py diff --git a/iofmt/hdf5.py b/iofmt/hdf5.py new file mode 100644 index 0000000..1010fc0 --- /dev/null +++ b/iofmt/hdf5.py @@ -0,0 +1,64 @@ +# +# wpylib.iofmt.hdf5 module +# Created: 20150401 +# Wirawan Purwanto +# +""" +HDF5 binary format support, using h5py. + +""" + +import h5py +import numpy +import sys + + +# Quickie functions + +def hdf5_read_value(filename, key): + """Single-value read action from a file. + Raises KeyError if the item does not exist. + """ + F = h5py.File(filename, 'r') + try: + val = F[key].value + except KeyError: + F.close() + raise + F.close() + return val + + +def hdf5_write_value(filename, key, value): + """Single-value write action from a file. + Overwrites the existing value, if it exists. + Raises an exception upon error. + """ + F = h5py.File(filename, 'a') + try: + if key in F: + del F[key] + F[key] = value + except: + F.close() + raise + F.close() + + +def hdf5_write_values(filename, keyvals): + """Multiple-value write action to a file. + The key-value pairs are specified as a dict. + Overwrites the existing value, if it exists. + Raises an exception upon error. + """ + F = h5py.File(filename, 'a') + try: + for (key,value) in keyvals.iteritems(): + if key in F: + del F[key] + F[key] = value + except: + F.close() + raise + F.close() +