From: W. Trevor King Date: Sat, 19 Jan 2013 03:15:29 +0000 (-0500) Subject: storage:hdf5: Add support for dtype(object) and 0d string arrays X-Git-Tag: v0.3~1 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=ec3a4a001ed6e1b4df9ff6e01ce8fcf0c99f7552;p=h5config.git storage:hdf5: Add support for dtype(object) and 0d string arrays I'm not sure if it was a due to upgrading libhdf5, h5py, or Numpy, but I'm getting strings back as `array('abc', dtype=object)` instead of `array('abc', dtype='|S3')`. Add some 0d-array-extraction logic to handle this case. --- diff --git a/h5config/storage/hdf5.py b/h5config/storage/hdf5.py index ce81289..47a522c 100644 --- a/h5config/storage/hdf5.py +++ b/h5config/storage/hdf5.py @@ -211,7 +211,7 @@ class HDF5_Storage (_FileStorage): if isinstance(v, _numpy.ndarray): if isinstance(s, _config.BooleanSetting): v = bool(v) # array(True, dtype=bool) -> True - elif v.dtype.type == _numpy.string_: + elif v.dtype.type in [_numpy.string_, _numpy.object_]: if isinstance(s, _config.ListSetting): try: v = list(v) @@ -222,6 +222,10 @@ class HDF5_Storage (_FileStorage): if isinstance(v_, bytes): v[i] = str(v_, 'utf-8') else: # array('abc', dtype='|S3') -> 'abc' + if not v.shape: + # array('abc', dtype=object) -> 'abc' + # convert from numpy 0d array + v = v.item() if _sys.version_info >= (3,): v = str(v, 'utf-8') else: