Skip to content

Commit

Permalink
Use cached PropertyNodes in python API for property reads (#993)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcleod authored Dec 3, 2023
1 parent a5855b6 commit 48e2558
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions python/jsbsim.pyx.in
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ cdef class FGFDMExec(FGJSBBase):
"""@Dox(JSBSim::FGFDMExec)"""

cdef c_FGFDMExec *thisptr # hold a C++ instance which we're wrapping
cdef dict properties_cache # Dictionary cache of property nodes

def __cinit__(self, root_dir, FGPropertyManager pm_root=None, *args,
**kwargs):
Expand Down Expand Up @@ -701,6 +702,8 @@ cdef class FGFDMExec(FGJSBBase):
self.set_aircraft_path("aircraft")
self.set_systems_path("systems")

self.properties_cache = { }

def __dealloc__(self) -> None:
del self.thisptr

Expand All @@ -720,10 +723,17 @@ cdef class FGFDMExec(FGJSBBase):

def __getitem__(self, key: str) -> float:
_key = key.strip()
pm = self.get_property_manager()
if not pm.hasNode(_key):
raise KeyError("No property named {}".format(_key))
return self.get_property_value(_key)
try:
property_node = self.properties_cache[_key]
return property_node.get_double_value()
except KeyError:
pm = self.get_property_manager()
property_node = pm.get_node(_key)
if property_node is not None:
self.properties_cache[_key] = property_node
return property_node.get_double_value()
else:
raise KeyError(f'No property named {_key}')

def __setitem__(self, key: str, value: float) -> None:
self.set_property_value(key.strip(), value)
Expand Down

0 comments on commit 48e2558

Please sign in to comment.