storage:hdf5: convert int/float lists to builtin Python types on load.
authorW. Trevor King <wking@tremily.us>
Wed, 12 Sep 2012 20:52:22 +0000 (16:52 -0400)
committerW. Trevor King <wking@tremily.us>
Wed, 12 Sep 2012 20:52:22 +0000 (16:52 -0400)
This avoids loading a FloatListSetting as a list of `numpy.float64`s,
which lead to problems if you attempted to serialize the resulting
config using `YAML_Storage`:

  Traceback (most recent call last):
    ...
    File ".../h5config/storage/yaml.py", line 140, in _save
      default_flow_style=False)
    File "/usr/lib64/python2.7/site-packages/yaml/__init__.py", line 202, in dump
      return dump_all([data], stream, Dumper=Dumper, **kwds)
    ...
    File "/usr/lib64/python2.7/site-packages/yaml/representer.py", line 247, in represent_undefined
      raise RepresenterError("cannot represent an object: %s" % data)
  yaml.representer.RepresenterError: cannot represent an object: 0.0

h5config/storage/hdf5.py

index 6b8a0bcf1fa16e378de5ce05d1bfaa6ca3d1c72d..ce81289a4dccd430332a32284dce9378bcc4c26e 100644 (file)
@@ -233,7 +233,12 @@ class HDF5_Storage (_FileStorage):
                         elif isinstance(s, _config.NumericSetting):
                             raise NotImplementedError(type(s))
                         elif isinstance(s, _config.ListSetting):
-                            v = list(v)  # convert from numpy array
+                            # convert from numpy array
+                            if isinstance(s, _config.IntegerListSetting):
+                                conv = int
+                            elif isinstance(s, _config.FloatListSetting):
+                                conv = float
+                            v = list(conv(x) for x in v)
                     if _is_string(v):
                         # convert back from None, etc.
                         v = s.convert_from_text(v)