Skip to content

Commit 86cd132

Browse files
committed
update hdfdict to np v2, safely unpack str/bytes in datetime validation
1 parent 7338149 commit 86cd132

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/pynxtools/dataconverter/hdfdict.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import h5py
99
import yaml
10-
from numpy import string_
10+
from numpy import str_
1111

1212
TYPE = "_type_"
1313

@@ -45,7 +45,7 @@ def unpack_dataset(item):
4545
4646
"""
4747
value = item[()]
48-
type_id = item.attrs.get(TYPE, string_()).astype(str)
48+
type_id = item.attrs.get(TYPE, str_()).astype(str)
4949
if type_id == "datetime":
5050
if hasattr(value, "__iter__"):
5151
value = [datetime.fromtimestamp(ts) for ts in value]
@@ -62,7 +62,7 @@ def unpack_dataset(item):
6262
value = tuple(value)
6363

6464
elif type_id == "str":
65-
value = string_(value).astype(str)
65+
value = str_(value).astype(str)
6666

6767
return value
6868

@@ -181,15 +181,15 @@ def pack_dataset(hdfobject, key, value):
181181
attr_data = None
182182

183183
if attr_data:
184-
ds.attrs.create(name=TYPE, data=string_(attr_data))
184+
ds.attrs.create(name=TYPE, data=str_(attr_data))
185185

186186
except (TypeError, ValueError):
187187
# Obviously the data was not serializable. To give it
188188
# a last try; serialize it to yaml
189189
# and save it to the hdf file:
190-
ds = hdfobject.create_dataset(name=key, data=string_(yaml.safe_dump(value)))
190+
ds = hdfobject.create_dataset(name=key, data=str_(yaml.safe_dump(value)))
191191

192-
ds.attrs.create(name=TYPE, data=string_("yaml"))
192+
ds.attrs.create(name=TYPE, data=str_("yaml"))
193193
# if this fails again, restructure your data!
194194

195195

src/pynxtools/dataconverter/helpers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,10 @@ def validate_data_value(
794794
collector.collect_and_log(path, ValidationProblem.IsNotPosInt, value)
795795

796796
if nxdl_type in ("ISO8601", "NX_DATE_TIME"):
797-
results = ISO8601.search(value)
798-
if results is None:
797+
798+
if ISO8601.search(
799+
value.decode("utf-8") if isinstance(value, bytes) else value
800+
) is None:
799801
collector.collect_and_log(
800802
path, ValidationProblem.InvalidDatetime, value
801803
)

0 commit comments

Comments
 (0)