Skip to content

Commit c14db02

Browse files
committed
Allow serialization of h5 files with Nones by dropping key: value pairs if value is None.
1 parent b7ff2b8 commit c14db02

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

cadet/h5.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,16 @@ def recursively_save(h5file: h5py.File, path: str, dic: Dict, func: callable) ->
424424

425425
for key, item in dic.items():
426426
key = str(key)
427-
value = None
427+
428+
if item is None:
429+
continue
428430

429431
if not isinstance(key, str):
430432
raise ValueError("dict keys must be strings to save to hdf5")
431433

432434
if isinstance(item, dict):
433435
recursively_save(h5file, path + key + '/', item, func)
436+
continue
434437
elif isinstance(item, str):
435438
value = numpy.array(item.encode('utf-8'))
436439
elif isinstance(item, list) and all(isinstance(i, str) for i in item):
@@ -441,11 +444,10 @@ def recursively_save(h5file: h5py.File, path: str, dic: Dict, func: callable) ->
441444
except TypeError:
442445
raise ValueError(f'Cannot save {path}/{func(key)} key with {type(item)} type.')
443446

444-
if value is not None:
445-
try:
446-
h5file[path + func(key)] = value
447-
except OSError as e:
448-
if str(e) == 'Unable to create link (name already exists)':
449-
raise KeyError(f'Name conflict with upper and lower case entries for key "{path}{key}".')
450-
else:
451-
raise
447+
try:
448+
h5file[path + func(key)] = value
449+
except OSError as e:
450+
if str(e) == 'Unable to create link (name already exists)':
451+
raise KeyError(f'Name conflict with upper and lower case entries for key "{path}{key}".')
452+
else:
453+
raise

0 commit comments

Comments
 (0)