Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion rocketpy/Flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import math
import time
import warnings
from functools import cached_property

import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -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.
Expand Down
25 changes: 25 additions & 0 deletions rocketpy/tools.py
Original file line number Diff line number Diff line change
@@ -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