Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use cached PropertyNodes in python API for property reads #993

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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