From 2a93694cfc9963b6f2eed0ce35f9e2471bf8bd6e Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Mon, 2 Sep 2024 17:05:32 -0700 Subject: [PATCH] fix setting values --- src/numpydantic/interface/hdf5.py | 10 +++++++--- tests/test_interface/test_hdf5.py | 7 +++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/numpydantic/interface/hdf5.py b/src/numpydantic/interface/hdf5.py index 0dac4c6..9250353 100644 --- a/src/numpydantic/interface/hdf5.py +++ b/src/numpydantic/interface/hdf5.py @@ -112,9 +112,13 @@ class H5Proxy: ): with h5py.File(self.file, "r+", locking=True) as h5f: obj = h5f.get(self.path) - if self.field is not None: - obj = obj.fields(self.field) - obj[key] = value + if self.field is None: + obj[key] = value + else: + if isinstance(key, tuple): + obj[*key, self.field] = value + else: + obj[key, self.field] = value def open(self, mode: str = "r") -> "h5py.Dataset": """ diff --git a/tests/test_interface/test_hdf5.py b/tests/test_interface/test_hdf5.py index cdb255a..23d26a3 100644 --- a/tests/test_interface/test_hdf5.py +++ b/tests/test_interface/test_hdf5.py @@ -150,3 +150,10 @@ def test_compound_dtype(tmp_path): assert instance.array.dtype == np.dtype("int64") assert instance.array.shape == (10, 20) assert instance.array[0, 0] == 0 + + # set values too + instance.array[0, :] = 1 + assert all(instance.array[0, :] == 1) + assert all(instance.array[1, :] == 0) + instance.array[1] = 2 + assert all(instance.array[1] == 2)