"""HDF5 backend implementation
"""
+import types as _types
+
import h5py as _h5py
from .. import LOG as _LOG
class HDF5_Storage (_FileStorage):
"""Back a `Config` class with an HDF5 file.
- TODO: Special handling for Choice (enums), FloatList (arrays), etc.?
-
The `.save` and `.load` methods have an optional `group` argument
that allows you to save and load settings from an externally
opened HDF5 file. This can make it easier to stash several
>>> pprint_HDF5(filename) # doctest: +REPORT_UDIFF
/
/base
- <HDF5 dataset "age": shape (), type "|S3">
+ <HDF5 dataset "age": shape (), type "<f8">
1.3
- <HDF5 dataset "alive": shape (), type "|S3">
- yes
- <HDF5 dataset "bids": shape (), type "|S11">
- 5.4, 3.2, 1
+ <HDF5 dataset "alive": shape (), type "|b1">
+ True
+ <HDF5 dataset "bids": shape (3,), type "<f8">
+ [ 5.4 3.2 1. ]
<HDF5 dataset "children": shape (), type "|S1">
<BLANKLINE>
- <HDF5 dataset "daisies": shape (), type "|S2">
+ <HDF5 dataset "daisies": shape (), type "<i4">
13
<HDF5 dataset "name": shape (), type "|S1">
<BLANKLINE>
for s in config.settings:
if s.name not in group.keys():
continue
+ elif isinstance(s, (_config.BooleanSetting,
+ _config.NumericSetting,
+ _config.FloatListSetting)):
+ v = group[s.name][...]
+ if isinstance(v, _types.StringTypes):
+ # convert back from None, etc.
+ v = s.convert_from_text(v)
+ elif isinstance(s, _config.FloatListSetting):
+ v = list(v) # convert from numpy array
+ config[s.name] = v
elif isinstance(s, _config.ConfigListSetting):
try:
cwg = h5_create_group(group, s.name)
f = _h5py.File(self._filename, 'a')
group = f[self.group]
for s in config.settings:
- if isinstance(s, _config.ConfigListSetting):
+ value = None
+ if isinstance(s, (_config.BooleanSetting,
+ _config.NumericSetting,
+ _config.FloatListSetting)):
+ value = config[s.name]
+ if value in [None, []]:
+ value = s.convert_to_text(value)
+ elif isinstance(s, _config.ConfigListSetting):
configs = config[s.name]
if configs:
cwg = h5_create_group(group, s.name, force=True)
cwg = h5_create_group(group, s.name, force=True)
self._save(config=cfg, group=cwg)
continue
- value = s.convert_to_text(config[s.name])
+ if value is None: # not set yet, or invalid
+ value = s.convert_to_text(config[s.name])
try:
del group[s.name]
except KeyError:
from __future__ import absolute_import
import os.path as _os_path
+import types as _types
import yaml as _yaml # global PyYAML module
class YAML_Storage (_FileStorage):
"""Back a `Config` class with a YAML file.
- TODO: Special handling for Choice (enums), etc.?
-
>>> import os
>>> from ..test import TestConfig
>>> import os.path
if isinstance(setting, (_config.BooleanSetting,
_config.NumericSetting,
_config.FloatListSetting)):
+ if isinstance(v, _types.StringTypes):
+ # older versions of h5config
+ value = s.convert_from_text(value)
v = value
elif isinstance(setting, _config.ConfigListSetting) and value:
values = []
>>> pprint_HDF5(filename) # doctest: +REPORT_UDIFF
/
/base
- <HDF5 dataset "age": shape (), type "|S3">
+ <HDF5 dataset "age": shape (), type "<f8">
1.3
- <HDF5 dataset "alive": shape (), type "|S2">
- no
- <HDF5 dataset "bids": shape (), type "|S11">
- 5.4, 3.2, 1
+ <HDF5 dataset "alive": shape (), type "|b1">
+ False
+ <HDF5 dataset "bids": shape (3,), type "<f8">
+ [ 5.4 3.2 1. ]
<HDF5 dataset "children": shape (), type "|S1">
<BLANKLINE>
- <HDF5 dataset "daisies": shape (), type "|S2">
+ <HDF5 dataset "daisies": shape (), type "<i4">
13
<HDF5 dataset "name": shape (), type "|S1">
<BLANKLINE>