Skip to content

Commit 031db97

Browse files
Merge pull request #385 from RocketPy-Team/enh/resetable-funcified-methods
ENH: Resetable Funcified Methods
2 parents c810266 + 56391b8 commit 031db97

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

rocketpy/Function.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,8 +2916,8 @@ def calcOutput(func, inputs):
29162916

29172917

29182918
def funcify_method(*args, **kwargs):
2919-
"""Decorator factory to wrap methods as Function objects and save them as cached
2920-
properties.
2919+
"""Decorator factory to wrap methods as Function objects and save them as
2920+
cached properties.
29212921
29222922
Parameters
29232923
----------
@@ -2952,8 +2952,8 @@ def funcify_method(*args, **kwargs):
29522952
>>> g(2)
29532953
11
29542954
2955-
2. Method which returns a rocketpy.Function instance. An interesting use is to reset
2956-
input and output names after algebraic operations.
2955+
2. Method which returns a rocketpy.Function instance. An interesting use is
2956+
to reset input and output names after algebraic operations.
29572957
29582958
>>> class Example():
29592959
... @funcify_method(inputs=['x'], outputs=['x**3'])
@@ -3025,6 +3025,7 @@ def __get__(self, instance, owner=None):
30253025
)
30263026

30273027
val.__doc__ = self.__doc__
3028+
val.__cached__ = True
30283029
cache[self.attrname] = val
30293030
return val
30303031

@@ -3034,6 +3035,27 @@ def __get__(self, instance, owner=None):
30343035
return funcify_method_decorator
30353036

30363037

3038+
def reset_funcified_methods(instance):
3039+
"""Resets all the funcified methods of the instance. It does so by
3040+
deleting the current Functions, which will make the interperter redefine
3041+
them when they are called. This is useful when the instance has changed
3042+
and the methods need to be recalculated.
3043+
3044+
Parameters
3045+
----------
3046+
instance : object
3047+
The instance of the class whose funcified methods will be recalculated.
3048+
The class must have a mutable __dict__ attribute.
3049+
3050+
Return
3051+
------
3052+
None
3053+
"""
3054+
for key in list(instance.__dict__):
3055+
if hasattr(instance.__dict__[key], "__cached__"):
3056+
instance.__dict__.pop(key)
3057+
3058+
30373059
if __name__ == "__main__":
30383060
import doctest
30393061

rocketpy/motors/HybridMotor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
except ImportError:
1010
from rocketpy.tools import cached_property
1111

12-
from rocketpy.Function import funcify_method
12+
13+
from rocketpy.Function import funcify_method, reset_funcified_methods
1314

1415
from .LiquidMotor import LiquidMotor
1516
from .Motor import Motor
@@ -493,6 +494,7 @@ def addTank(self, tank, position):
493494
"""
494495
self.liquid.addTank(tank, position)
495496
self.solid.massFlowRate = self.totalMassFlowRate - self.liquid.massFlowRate
497+
reset_funcified_methods(self)
496498

497499
def allInfo(self):
498500
"""Prints out all data and graphs available about the Motor.

rocketpy/motors/LiquidMotor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
except ImportError:
1010
from rocketpy.tools import cached_property
1111

12-
from rocketpy.Function import Function, funcify_method
12+
from rocketpy.Function import Function, funcify_method, reset_funcified_methods
1313

1414
from .Motor import Motor
1515

@@ -430,6 +430,7 @@ def addTank(self, tank, position):
430430
geometry reference zero point.
431431
"""
432432
self.positioned_tanks.append({"tank": tank, "position": position})
433+
reset_funcified_methods(self)
433434

434435
def allInfo(self):
435436
"""Prints out all data and graphs available about the Motor.

rocketpy/motors/SolidMotor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
except ImportError:
1313
from rocketpy.tools import cached_property
1414

15-
from rocketpy.Function import Function, funcify_method
15+
from rocketpy.Function import Function, funcify_method, reset_funcified_methods
1616

1717
from .Motor import Motor
1818

@@ -511,6 +511,8 @@ def terminateBurn(t, y):
511511
"constant",
512512
)
513513

514+
reset_funcified_methods(self)
515+
514516
return [self.grainInnerRadius, self.grainHeight]
515517

516518
@funcify_method("Time (s)", "burn area (m²)")

0 commit comments

Comments
 (0)