Skip to content

Commit

Permalink
Add SwigPtrView 's fields as properties
Browse files Browse the repository at this point in the history
This way, `dir(SwigPtrView(...))` will show the available fields
and sufficiently smart IDEs will show them for code completion.
  • Loading branch information
dweindl committed Dec 17, 2023
1 parent 594b07e commit a0fa108
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion python/sdist/amici/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit a0fa108

Please sign in to comment.