From a0fa1088adc4e42bd050c5fce466e023ceec4a7d Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Sun, 17 Dec 2023 17:52:36 +0100 Subject: [PATCH] Add SwigPtrView 's fields as properties This way, `dir(SwigPtrView(...))` will show the available fields and sufficiently smart IDEs will show them for code completion. --- python/sdist/amici/numpy.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/python/sdist/amici/numpy.py b/python/sdist/amici/numpy.py index b84e52cc2b..97cb624f4a 100644 --- a/python/sdist/amici/numpy.py +++ b/python/sdist/amici/numpy.py @@ -91,6 +91,15 @@ def __init__(self, swigptr): self._cache = {} super(SwigPtrView, self).__init__() + # create properties for all fields + for field in self._field_names: + if not hasattr(self, field): + setattr( + self, + field, + property(lambda self_: self_.__getitem__(field)), + ) + def __len__(self) -> int: """ Returns the number of available keys/fields @@ -237,7 +246,7 @@ def __init__(self, rdata: Union[ReturnDataPtr, ReturnData]): if not isinstance(rdata, (ReturnDataPtr, ReturnData)): raise TypeError( f"Unsupported pointer {type(rdata)}, must be" - f"amici.ExpDataPtr!" + f"amici.ReturnDataPtr or amici.ReturnData!" ) self._field_dimensions = { "ts": [rdata.nt],