From 48e2558a7620918212e5de117f19b97f7b587820 Mon Sep 17 00:00:00 2001 From: Sean McLeod Date: Sun, 3 Dec 2023 16:39:32 +0200 Subject: [PATCH] Use cached PropertyNodes in python API for property reads (#993) --- python/jsbsim.pyx.in | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/python/jsbsim.pyx.in b/python/jsbsim.pyx.in index 4a8c2144f7..3344bcc3b2 100644 --- a/python/jsbsim.pyx.in +++ b/python/jsbsim.pyx.in @@ -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): @@ -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 @@ -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)