Consolidate de-numpy-ing in hdf5 module.
authorW. Trevor King <wking@drexel.edu>
Mon, 26 Sep 2011 00:19:01 +0000 (20:19 -0400)
committerW. Trevor King <wking@drexel.edu>
Mon, 26 Sep 2011 00:19:01 +0000 (20:19 -0400)
h5config/config.py
h5config/storage/hdf5.py

index 5fab0050734e10439ae707d3a3609efdef4d2169..4aa9e91f637211863accd362e97f634aa83297d0 100644 (file)
 
 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):
index 6cb3970112dee554d54e5b21c589c6a0f827c29e..c2add451eed5c0a57e421bacf1ebba7a78b901c4 100644 (file)
@@ -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()