From d24fce726ff77b0d00e653a651c7045f728ce4ff Mon Sep 17 00:00:00 2001 From: Giovani Hidalgo Ceotto Date: Sat, 12 Nov 2022 00:43:09 -0300 Subject: [PATCH 1/4] ENH: created a cached_property decorator --- rocketpy/tools.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 rocketpy/tools.py diff --git a/rocketpy/tools.py b/rocketpy/tools.py new file mode 100644 index 000000000..f79a8e733 --- /dev/null +++ b/rocketpy/tools.py @@ -0,0 +1,21 @@ +_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 + 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 From d7a8e604530285d3f6d1c7c87ed949785829be22 Mon Sep 17 00:00:00 2001 From: Giovani Hidalgo Ceotto Date: Sat, 12 Nov 2022 00:43:48 -0300 Subject: [PATCH 2/4] FIX: import local cached_property if standard does not exist --- rocketpy/Flight.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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. From b81e30f8d9c22b7f66ea5c544b6f27364525ffbe Mon Sep 17 00:00:00 2001 From: Giovani Hidalgo Ceotto Date: Sat, 12 Nov 2022 23:09:03 -0300 Subject: [PATCH 3/4] ENH: Prevents cached_property decorator from being used before `__set_name__` --- rocketpy/tools.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rocketpy/tools.py b/rocketpy/tools.py index f79a8e733..a283a498b 100644 --- a/rocketpy/tools.py +++ b/rocketpy/tools.py @@ -13,6 +13,9 @@ def __set_name__(self, owner, 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: From a46aa2efca8294357ec16e7274e466062c08eab2 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Sun, 13 Nov 2022 02:09:36 +0000 Subject: [PATCH 4/4] Fix code style issues with Black --- rocketpy/tools.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rocketpy/tools.py b/rocketpy/tools.py index a283a498b..9b5d1b59c 100644 --- a/rocketpy/tools.py +++ b/rocketpy/tools.py @@ -15,7 +15,8 @@ def __get__(self, instance, owner=None): return self if self.attrname is None: raise TypeError( - "Cannot use cached_property instance without calling __set_name__ on it.") + "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: