|
15 | 15 | import re |
16 | 16 | import time |
17 | 17 | from bisect import bisect_left |
18 | | - |
| 18 | +import warnings |
19 | 19 | import dill |
20 | 20 | import matplotlib.pyplot as plt |
21 | 21 | import numpy as np |
22 | 22 | import pytz |
23 | 23 | from cftime import num2pydate |
24 | 24 | from matplotlib.patches import Ellipse |
25 | 25 | from packaging import version as packaging_version |
26 | | -from rocketpy.mathutils.function import Function |
27 | | -from rocketpy.mathutils.vector_matrix import Vector |
28 | | - |
29 | | -# Mapping of module name and the name of the package that should be installed |
30 | 26 | INSTALL_MAPPING = {"IPython": "ipython"} |
31 | 27 |
|
32 | 28 |
|
@@ -55,6 +51,66 @@ def tuple_handler(value): |
55 | 51 | return tuple(value) |
56 | 52 | else: |
57 | 53 | raise ValueError("value must be a list or tuple of length 1 or 2.") |
| 54 | + |
| 55 | +def deprecated(reason=None, version=None, alternative=None): |
| 56 | + """ |
| 57 | + Decorator to mark functions or methods as deprecated. |
| 58 | +
|
| 59 | + This decorator issues a DeprecationWarning when the decorated function |
| 60 | + is called, indicating that it will be removed in future versions. |
| 61 | +
|
| 62 | + Parameters |
| 63 | + ---------- |
| 64 | + reason : str, optional |
| 65 | + Custom deprecation message. If not provided, a default message will be used. |
| 66 | + version : str, optional |
| 67 | + Version when the function will be removed. If provided, it will be |
| 68 | + included in the warning message. |
| 69 | + alternative : str, optional |
| 70 | + Name of the alternative function/method that should be used instead. |
| 71 | + If provided, it will be included in the warning message. |
| 72 | +
|
| 73 | + Returns |
| 74 | + ------- |
| 75 | + callable |
| 76 | + The decorated function with deprecation warning functionality. |
| 77 | +
|
| 78 | + Examples |
| 79 | + -------- |
| 80 | + >>> @deprecated(reason="This function is obsolete", version="v2.0.0", |
| 81 | + ... alternative="new_function") |
| 82 | + ... def old_function(): |
| 83 | + ... return "old result" |
| 84 | +
|
| 85 | + >>> @deprecated() |
| 86 | + ... def another_old_function(): |
| 87 | + ... return "result" |
| 88 | + """ |
| 89 | + |
| 90 | + def decorator(func): |
| 91 | + @functools.wraps(func) |
| 92 | + def wrapper(*args, **kwargs): |
| 93 | + # Build the deprecation message |
| 94 | + if reason: |
| 95 | + message = reason |
| 96 | + else: |
| 97 | + message = f"The function `{func.__name__}` is deprecated" |
| 98 | + |
| 99 | + if version: |
| 100 | + message += f" and will be removed in {version}" |
| 101 | + |
| 102 | + if alternative: |
| 103 | + message += f". Use `{alternative}` instead" |
| 104 | + |
| 105 | + message += "." |
| 106 | + |
| 107 | + warnings.warn(message, DeprecationWarning, stacklevel=2) |
| 108 | + return func(*args, **kwargs) |
| 109 | + |
| 110 | + return wrapper |
| 111 | + |
| 112 | + return decorator |
| 113 | + |
58 | 114 |
|
59 | 115 |
|
60 | 116 | def calculate_cubic_hermite_coefficients(x0, x1, y0, yp0, y1, yp1): |
@@ -1023,10 +1079,10 @@ def wrapper(*args, **kwargs): |
1023 | 1079 |
|
1024 | 1080 |
|
1025 | 1081 |
|
1026 | | -def _pat_dynamic_helper(com_inertia_moment, mass, distance_vec_3d, axes_term_lambda): |
1027 | | - |
1028 | | - |
1029 | | - |
| 1082 | +def _pat_dynamic_helper(com_inertia_moment, mass, distance_vec_3d, axes_term_lambda): |
| 1083 | + "Local import to break circular dependency with mathutils.function" |
| 1084 | + from rocketpy.mathutils.function import Function |
| 1085 | + from rocketpy.mathutils.vector_matrix import Vector |
1030 | 1086 | is_dynamic = ( |
1031 | 1087 | isinstance(com_inertia_moment, Function) |
1032 | 1088 | or isinstance(mass, Function) |
@@ -1058,8 +1114,9 @@ def new_source(t): |
1058 | 1114 | def _pat_dynamic_product_helper( |
1059 | 1115 | com_inertia_product, mass, distance_vec_3d, product_term_lambda |
1060 | 1116 | ): |
1061 | | - |
1062 | | - |
| 1117 | + "Local import to break circular dependency with mathutils.function" |
| 1118 | + from rocketpy.mathutils.function import Function |
| 1119 | + from rocketpy.mathutils.vector_matrix import Vector |
1063 | 1120 | is_dynamic = ( |
1064 | 1121 | isinstance(com_inertia_product, Function) |
1065 | 1122 | or isinstance(mass, Function) |
|
0 commit comments