diff --git a/rocketpy/Flight.py b/rocketpy/Flight.py index 8be8a2342..cb908dcab 100644 --- a/rocketpy/Flight.py +++ b/rocketpy/Flight.py @@ -9,7 +9,6 @@ import math import time import warnings -from functools import cached_property import matplotlib.pyplot as plt import numpy as np @@ -18,6 +17,11 @@ from .Function import Function +try: + from functools import cached_property +except ImportError: + from .tools import cached_property + class Flight: """Keeps all flight information and has a method to simulate flight. diff --git a/rocketpy/tools.py b/rocketpy/tools.py new file mode 100644 index 000000000..9b5d1b59c --- /dev/null +++ b/rocketpy/tools.py @@ -0,0 +1,25 @@ +_NOT_FOUND = object() + + +class cached_property: + def __init__(self, func): + self.func = func + self.attrname = None + self.__doc__ = func.__doc__ + + def __set_name__(self, owner, name): + self.attrname = name + + def __get__(self, instance, owner=None): + if instance is None: + return self + if self.attrname is None: + raise TypeError( + "Cannot use cached_property instance without calling __set_name__ on it." + ) + cache = instance.__dict__ + val = cache.get(self.attrname, _NOT_FOUND) + if val is _NOT_FOUND: + val = self.func(instance) + cache[self.attrname] = val + return val