Skip to content

Commit 1669175

Browse files
committed
fix for reading fixed length string dataset values
1 parent c6a9779 commit 1669175

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

h5json/hdf5db.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def createHDF5File(filePath):
131131
@staticmethod
132132
def getVersionInfo():
133133
versionInfo = {}
134-
versionInfo['hdf5-json-version'] = "1.1.0" # todo - have this auto-synch with package version
134+
versionInfo['hdf5-json-version'] = "1.1.1" # todo - have this auto-synch with package version
135135
versionInfo['h5py_version'] = h5py.version.version
136136
versionInfo['hdf5_version'] = h5py.version.hdf5_version
137137
return versionInfo
@@ -2160,21 +2160,14 @@ def getDatasetValuesByUuid(self, obj_uuid, slices=Ellipsis, format="json"):
21602160
# opaque type - skip for now
21612161
self.log.warning("unable to get opaque type values")
21622162
values = "????"
2163-
elif dt.kind == 'S' and six.PY3:
2164-
# For Python3 fixed string values will be returned as bytes,
2165-
# so finese them into strings
2166-
if format != "json":
2167-
msg = "Only JSON is supported for for this data type"
2168-
self.log.info(msg)
2169-
raise IOError(errno.EINVAL, msg)
2163+
elif dt.kind == 'S' and format == "json" and six.PY3:
21702164
values = self.bytesArrayToList(dset[slices])
21712165
elif len(dt) > 1:
21722166
# compound type
21732167
if format == "json":
21742168
values = self.bytesArrayToList(dset[slices])
21752169
else:
21762170
values = dset[slices].tobytes()
2177-
21782171
else:
21792172
values = dset[slices]
21802173

test/unit/hdf5dbTest.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,35 @@ def testReadFixedStringDataset(self):
730730
self.assertEqual(item_type['strPad'], 'H5T_STR_NULLPAD')
731731
self.assertEqual(item_type['charSet'], 'H5T_CSET_ASCII')
732732
self.assertEqual(item_type['length'], 7)
733-
row = db.getDatasetValuesByUuid(dset_uuid, (slice(0, 1),))
734-
self.assertEqual(row, ['Parting'])
733+
row = db.getDatasetValuesByUuid(dset_uuid)
734+
self.assertEqual(row, ["Parting", "is such", "sweet", "sorrow."])
735+
row = db.getDatasetValuesByUuid(dset_uuid, (slice(0,1),))
736+
self.assertEqual(row, ["Parting",])
737+
row = db.getDatasetValuesByUuid(dset_uuid, (slice(2,3),))
738+
self.assertEqual(row, ["sweet",])
739+
740+
def testReadFixedStringDatasetBinary(self):
741+
item = None
742+
filepath = getFile('fixed_string_dset.h5', 'fixed_string_dset.h5')
743+
with Hdf5db(filepath, app_logger=self.log) as db:
744+
dset_uuid = db.getUUIDByPath('/DS1')
745+
item = db.getDatasetItemByUuid(dset_uuid)
746+
shape = item['shape']
747+
self.assertEqual(shape['class'], 'H5S_SIMPLE')
748+
dims = shape['dims']
749+
self.assertEqual(len(dims), 1)
750+
self.assertEqual(dims[0], 4)
751+
item_type = item['type']
752+
self.assertEqual(item_type['class'], 'H5T_STRING')
753+
self.assertEqual(item_type['strPad'], 'H5T_STR_NULLPAD')
754+
self.assertEqual(item_type['charSet'], 'H5T_CSET_ASCII')
755+
self.assertEqual(item_type['length'], 7)
756+
row = db.getDatasetValuesByUuid(dset_uuid, format="binary")
757+
self.assertEqual(row, b"Partingis suchsweet\x00\x00sorrow.")
758+
row = db.getDatasetValuesByUuid(dset_uuid, (slice(0,1),), format="binary")
759+
self.assertEqual(row, b"Parting")
760+
row = db.getDatasetValuesByUuid(dset_uuid, (slice(2,3),), format="binary")
761+
self.assertEqual(row, b"sweet\x00\x00")
735762

736763

737764
def testWriteVlenUnicodeAttribute(self):
@@ -1035,13 +1062,17 @@ def testBytesArrayToList(self):
10351062
val = db.bytesArrayToList(b'Hello')
10361063
self.assertTrue(type(val) is str)
10371064
val = db.bytesArrayToList([b'Hello',])
1065+
self.assertEqual(len(val), 1)
10381066
self.assertTrue(type(val[0]) is str)
1067+
self.assertEqual(val[0], 'Hello')
10391068

10401069
import numpy as np
10411070

10421071
data = np.array([b'Hello'])
10431072
val = db.bytesArrayToList(data)
1073+
self.assertEqual(len(val), 1)
10441074
self.assertTrue(type(val[0]) is str)
1075+
self.assertEqual(val[0], 'Hello')
10451076

10461077

10471078
def testGetDataValue(self):

0 commit comments

Comments
 (0)