From: W. Trevor King Date: Fri, 29 Jul 2011 10:54:07 +0000 (-0400) Subject: Numeric types are now stored in native format (vs. old strings). X-Git-Tag: v0.2~33 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=0f39adfab67eca66ac79fa1dcb88c362f3611200;p=h5config.git Numeric types are now stored in native format (vs. old strings). ChoiceSettings have string-type storage values anyway, so we'll leave them as strings in the data files. --- diff --git a/h5config/storage/hdf5.py b/h5config/storage/hdf5.py index b488c37..93913d6 100644 --- a/h5config/storage/hdf5.py +++ b/h5config/storage/hdf5.py @@ -18,6 +18,8 @@ """HDF5 backend implementation """ +import types as _types + import h5py as _h5py from .. import LOG as _LOG @@ -75,8 +77,6 @@ def h5_create_group(cwg, path, force=False): 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 @@ -98,15 +98,15 @@ class HDF5_Storage (_FileStorage): >>> pprint_HDF5(filename) # doctest: +REPORT_UDIFF / /base - + 1.3 - - yes - - 5.4, 3.2, 1 + + True + + [ 5.4 3.2 1. ] - + 13 @@ -155,6 +155,16 @@ class HDF5_Storage (_FileStorage): 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) @@ -195,7 +205,14 @@ class HDF5_Storage (_FileStorage): 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) @@ -209,7 +226,8 @@ class HDF5_Storage (_FileStorage): 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: diff --git a/h5config/storage/yaml.py b/h5config/storage/yaml.py index ec06aba..3550230 100644 --- a/h5config/storage/yaml.py +++ b/h5config/storage/yaml.py @@ -21,6 +21,7 @@ from __future__ import absolute_import import os.path as _os_path +import types as _types import yaml as _yaml # global PyYAML module @@ -45,8 +46,6 @@ _YAMLDumper.add_representer(bool, _YAMLDumper.represent_bool) 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 @@ -109,6 +108,9 @@ class YAML_Storage (_FileStorage): 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 = [] diff --git a/h5config/test.py b/h5config/test.py index 5734a27..a0e2182 100644 --- a/h5config/test.py +++ b/h5config/test.py @@ -43,15 +43,15 @@ Saving fills in all the config values. >>> pprint_HDF5(filename) # doctest: +REPORT_UDIFF / /base - + 1.3 - - no - - 5.4, 3.2, 1 + + False + + [ 5.4 3.2 1. ] - + 13