mirror of
https://github.com/p2p-ld/numpydantic.git
synced 2024-11-09 16:24:28 +00:00
strings in compound dtypes
This commit is contained in:
parent
0f1e0d0caf
commit
f28c766b96
2 changed files with 10 additions and 4 deletions
|
@ -124,8 +124,13 @@ class H5Proxy:
|
|||
with h5py.File(self.file, "r") as h5f:
|
||||
obj = h5f.get(self.path)
|
||||
if self.field is not None:
|
||||
if h5py.h5t.check_string_dtype(obj.dtype[self.field]):
|
||||
obj = obj.fields(self.field).asstr()
|
||||
if encoding := h5py.h5t.check_string_dtype(obj.dtype[self.field]):
|
||||
if isinstance(item, tuple):
|
||||
item = (*item, self.field)
|
||||
else:
|
||||
item = (item, self.field)
|
||||
|
||||
return obj[item].decode(encoding.encoding)
|
||||
else:
|
||||
obj = obj.fields(self.field)
|
||||
else:
|
||||
|
|
|
@ -157,11 +157,12 @@ def test_compound_dtype(tmp_path):
|
|||
assert all(instance.array[1] == 2)
|
||||
|
||||
|
||||
def test_strings(hdf5_array):
|
||||
@pytest.mark.parametrize("compound", [True, False])
|
||||
def test_strings(hdf5_array, compound):
|
||||
"""
|
||||
HDF5 proxy can get and set strings just like any other dtype
|
||||
"""
|
||||
array = hdf5_array((10, 10), str)
|
||||
array = hdf5_array((10, 10), str, compound=compound)
|
||||
|
||||
class MyModel(BaseModel):
|
||||
array: NDArray[Shape["10, 10"], str]
|
||||
|
|
Loading…
Reference in a new issue