From 6779f3896ed0a85ebd4122c9f019508e9495cbb4 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 25 Sep 2011 20:19:01 -0400 Subject: [PATCH] Consolidate de-numpy-ing in hdf5 module. --- h5config/config.py | 7 +------ h5config/storage/hdf5.py | 27 +++++++++++++++------------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/h5config/config.py b/h5config/config.py index 5fab005..4aa9e91 100644 --- a/h5config/config.py +++ b/h5config/config.py @@ -20,14 +20,12 @@ import copy as _copy -import numpy as _numpy - from . import LOG as _LOG class Setting (object): "A named setting with arbitrary text values." - def __init__(self, name, help='', default=None): + def __init__(self, name, help='', default=None): self.name = name self._help = help self.default = default @@ -85,9 +83,6 @@ class ChoiceSetting (Setting): return ret.strip() def convert_from_text(self, value): - if (isinstance(value, _numpy.ndarray) and - value.dtype.type == _numpy.string_): - value = str(value) return dict(self.choices)[value] def convert_to_text(self, value): diff --git a/h5config/storage/hdf5.py b/h5config/storage/hdf5.py index 6cb3970..c2add45 100644 --- a/h5config/storage/hdf5.py +++ b/h5config/storage/hdf5.py @@ -21,6 +21,7 @@ import types as _types import h5py as _h5py +import numpy as _numpy from .. import LOG as _LOG from .. import config as _config @@ -155,17 +156,7 @@ 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): + if isinstance(s, _config.ConfigListSetting): try: cwg = h5_create_group(group, s.name) except ValueError: @@ -192,7 +183,19 @@ class HDF5_Storage (_FileStorage): config[s.name] = s.config_class() self._load(config=config[s.name], group=cwg) else: - config[s.name] = s.convert_from_text(group[s.name][...]) + v = group[s.name][...] + if isinstance(s, _config.BooleanSetting): + if isinstance(v, _numpy.ndarray): + v = bool(v) # array(True, dtype=bool) -> True + elif (isinstance(v, _numpy.ndarray) and + v.dtype.type == _numpy.string_): + v = str(v) # array('abc', dtype='|S3') -> 'abc' + elif isinstance(s, _config.FloatListSetting): + v = list(v) # convert from numpy array + if isinstance(v, _types.StringTypes): + # convert back from None, etc. + v = s.convert_from_text(v) + config[s.name] = v finally: if f: f.close() -- 2.26.2