From cb9f377b8b77a447986cd9a0bc249ab25e3c919f Mon Sep 17 00:00:00 2001 From: giovaniceotto Date: Tue, 27 Jun 2023 00:03:03 -0300 Subject: [PATCH 1/6] ENH: implement reset_funcified_methods --- rocketpy/Function.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/rocketpy/Function.py b/rocketpy/Function.py index 32b620e8c..c800ea55a 100644 --- a/rocketpy/Function.py +++ b/rocketpy/Function.py @@ -2916,8 +2916,8 @@ def calcOutput(func, inputs): def funcify_method(*args, **kwargs): - """Decorator factory to wrap methods as Function objects and save them as cached - properties. + """Decorator factory to wrap methods as Function objects and save them as + cached properties. Parameters ---------- @@ -2952,8 +2952,8 @@ def funcify_method(*args, **kwargs): >>> g(2) 11 - 2. Method which returns a rocketpy.Function instance. An interesting use is to reset - input and output names after algebraic operations. + 2. Method which returns a rocketpy.Function instance. An interesting use is + to reset input and output names after algebraic operations. >>> class Example(): ... @funcify_method(inputs=['x'], outputs=['x**3']) @@ -3025,6 +3025,7 @@ def __get__(self, instance, owner=None): ) val.__doc__ = self.__doc__ + val.__cached__ = True cache[self.attrname] = val return val @@ -3033,6 +3034,25 @@ def __get__(self, instance, owner=None): else: return funcify_method_decorator +def reset_funcified_methods(instance): + """Recalculates all the funcified methods of the instance. It does so by + deleting the current Functions, which will make the interperter redefine + them when they are called. This is useful when the instance has changed + and the methods need to be recalculated. + + Parameters + ---------- + instance : object + The instance of the class whose funcified methods will be recalculated. + The class must have a multable __dict__ attribute. + + Return + ------ + None + """ + for key in list(instance.__dict__): + if hasattr(instance.__dict__[key], "__cached__"): + instance.__dict__.pop(key) if __name__ == "__main__": import doctest From f444a1c379c2a6afdc485df3522d8e4e12597ebd Mon Sep 17 00:00:00 2001 From: giovaniceotto Date: Tue, 27 Jun 2023 00:04:38 -0300 Subject: [PATCH 2/6] ENH: Use reset_funcified_methods in Liquid and Hybrild Motors addTank method --- rocketpy/motors/HybridMotor.py | 3 ++- rocketpy/motors/LiquidMotor.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/rocketpy/motors/HybridMotor.py b/rocketpy/motors/HybridMotor.py index 5cd31dfd1..f02b793c6 100644 --- a/rocketpy/motors/HybridMotor.py +++ b/rocketpy/motors/HybridMotor.py @@ -9,7 +9,7 @@ except ImportError: from rocketpy.tools import cached_property -from rocketpy.Function import funcify_method +from rocketpy.Function import funcify_method, reset_funcified_methods from .Motor import Motor from .LiquidMotor import LiquidMotor from .SolidMotor import SolidMotor @@ -493,6 +493,7 @@ def addTank(self, tank, position): """ self.liquid.addTank(tank, position) self.solid.massFlowRate = self.totalMassFlowRate - self.liquid.massFlowRate + reset_funcified_methods(self) def allInfo(self): """Prints out all data and graphs available about the Motor. diff --git a/rocketpy/motors/LiquidMotor.py b/rocketpy/motors/LiquidMotor.py index 883d0d966..86609ed4f 100644 --- a/rocketpy/motors/LiquidMotor.py +++ b/rocketpy/motors/LiquidMotor.py @@ -9,7 +9,7 @@ except ImportError: from rocketpy.tools import cached_property -from rocketpy.Function import Function, funcify_method +from rocketpy.Function import Function, funcify_method, reset_funcified_methods from .Motor import Motor @@ -336,6 +336,7 @@ def addTank(self, tank, position): geometry reference zero point. """ self.positioned_tanks.append({"tank": tank, "position": position}) + reset_funcified_methods(self) def allInfo(self): """Prints out all data and graphs available about the Motor. From 0ff14ebca965dab12b1c4b4a4659322e298e9ae0 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Tue, 27 Jun 2023 03:10:49 +0000 Subject: [PATCH 3/6] Fix code style issues with Black --- rocketpy/Function.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rocketpy/Function.py b/rocketpy/Function.py index c800ea55a..ee4f14b7d 100644 --- a/rocketpy/Function.py +++ b/rocketpy/Function.py @@ -3034,6 +3034,7 @@ def __get__(self, instance, owner=None): else: return funcify_method_decorator + def reset_funcified_methods(instance): """Recalculates all the funcified methods of the instance. It does so by deleting the current Functions, which will make the interperter redefine @@ -3054,6 +3055,7 @@ def reset_funcified_methods(instance): if hasattr(instance.__dict__[key], "__cached__"): instance.__dict__.pop(key) + if __name__ == "__main__": import doctest From b34baf7c13a94a1a240b48c83ad47342fce126cc Mon Sep 17 00:00:00 2001 From: Guilherme Date: Fri, 30 Jun 2023 17:39:38 +0200 Subject: [PATCH 4/6] MAINT: change 'recalculates' to 'resets' Update rocketpy/Function.py Co-authored-by: MateusStano <69485049+MateusStano@users.noreply.github.com> --- rocketpy/Function.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocketpy/Function.py b/rocketpy/Function.py index ee4f14b7d..5b6993223 100644 --- a/rocketpy/Function.py +++ b/rocketpy/Function.py @@ -3036,7 +3036,7 @@ def __get__(self, instance, owner=None): def reset_funcified_methods(instance): - """Recalculates all the funcified methods of the instance. It does so by + """Resets all the funcified methods of the instance. It does so by deleting the current Functions, which will make the interperter redefine them when they are called. This is useful when the instance has changed and the methods need to be recalculated. From 1e9f841d4435ebc81ae2a408dcae28ad5cbbc368 Mon Sep 17 00:00:00 2001 From: Guilherme Date: Fri, 30 Jun 2023 17:40:17 +0200 Subject: [PATCH 5/6] MAINT: typo fix Update rocketpy/Function.py Co-authored-by: phmbressan <87212571+phmbressan@users.noreply.github.com> --- rocketpy/Function.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocketpy/Function.py b/rocketpy/Function.py index 5b6993223..48b2e929a 100644 --- a/rocketpy/Function.py +++ b/rocketpy/Function.py @@ -3045,7 +3045,7 @@ def reset_funcified_methods(instance): ---------- instance : object The instance of the class whose funcified methods will be recalculated. - The class must have a multable __dict__ attribute. + The class must have a mutable __dict__ attribute. Return ------ From 56391b837199cbbfd57fda3c68e1900f34be9782 Mon Sep 17 00:00:00 2001 From: giovaniceotto Date: Fri, 30 Jun 2023 14:11:49 -0300 Subject: [PATCH 6/6] ENH: Use reset_funcified_methods in SolidMotor evaluateGeometry --- rocketpy/motors/SolidMotor.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rocketpy/motors/SolidMotor.py b/rocketpy/motors/SolidMotor.py index db7f04144..0aa040efc 100644 --- a/rocketpy/motors/SolidMotor.py +++ b/rocketpy/motors/SolidMotor.py @@ -12,7 +12,7 @@ except ImportError: from rocketpy.tools import cached_property -from rocketpy.Function import Function, funcify_method +from rocketpy.Function import Function, funcify_method, reset_funcified_methods from .Motor import Motor @@ -511,6 +511,8 @@ def terminateBurn(t, y): "constant", ) + reset_funcified_methods(self) + return [self.grainInnerRadius, self.grainHeight] @funcify_method("Time (s)", "burn area (m²)")