From 2368caba4539e676265b5cf7c3e7d383a6fa926f Mon Sep 17 00:00:00 2001 From: MateusStano Date: Fri, 17 Nov 2023 16:15:56 +0100 Subject: [PATCH 1/6] ENH: add get attributes function --- rocketpy/utilities.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/rocketpy/utilities.py b/rocketpy/utilities.py index ac649eeac..275141e54 100644 --- a/rocketpy/utilities.py +++ b/rocketpy/utilities.py @@ -1,3 +1,4 @@ +import inspect import traceback import warnings @@ -654,3 +655,20 @@ def liftoff_speed(mass): if plot: retfunc.plot(min_mass, max_mass, points) return retfunc + + +def get_instance_attributes(instance): + """Returns a dictionary with all attributes of a given instance. + + Parameters + ---------- + instance : object + Instance of a class. + + Returns + ------- + dictionary + Dictionary with all attributes of the given instance. + """ + attributes_dict = inspect.getmembers(instance) + return attributes_dict From 931f3302ceee7cb46c39f24a0fda9747b0e36ffa Mon Sep 17 00:00:00 2001 From: MateusStano Date: Fri, 17 Nov 2023 16:16:09 +0100 Subject: [PATCH 2/6] MNT: add deprecation warnings --- rocketpy/environment/environment.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/rocketpy/environment/environment.py b/rocketpy/environment/environment.py index f5305aef2..741964d84 100644 --- a/rocketpy/environment/environment.py +++ b/rocketpy/environment/environment.py @@ -3126,7 +3126,18 @@ def all_plot_info_returned(self): ------ plot_info : Dict Dict of data relevant to plot externally + + Warning + ------- + Deprecated in favor of `utilities.get_instance_attributes`. + """ + warnings.warn( + "The method 'all_plot_info_returned' is deprecated. " + + "Use 'utilities.get_instance_attributes' instead.", + DeprecationWarning, + ) + grid = np.linspace(self.elevation, self.max_expected_height) plot_info = dict( grid=[i for i in grid], @@ -3187,7 +3198,17 @@ def all_info_returned(self): ------ info : Dict Information relevant about the Environment class. + + Warning + ------- + Deprecated in favor of `utilities.get_instance_attributes`. + """ + warnings.warn( + "The method 'all_info_returned' is deprecated. " + + "Use 'utilities.get_instance_attributes' instead.", + DeprecationWarning, + ) # Dictionary creation, if not commented follows the SI info = dict( From 1dd5e1e27b60a914a1908940c122e3161a1077ad Mon Sep 17 00:00:00 2001 From: MateusStano Date: Fri, 17 Nov 2023 19:08:09 +0100 Subject: [PATCH 3/6] ENH refactor get_instance_attributes function to filter out methods and protected attributes --- rocketpy/utilities.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rocketpy/utilities.py b/rocketpy/utilities.py index 275141e54..a1dfadaf5 100644 --- a/rocketpy/utilities.py +++ b/rocketpy/utilities.py @@ -670,5 +670,10 @@ def get_instance_attributes(instance): dictionary Dictionary with all attributes of the given instance. """ - attributes_dict = inspect.getmembers(instance) + attributes_dict = dict() + members = inspect.getmembers(instance) + for member in members: + # Filter out methods and protected attributes + if not inspect.ismethod(member[1]) and not member[0].startswith("__"): + attributes_dict[member[0]] = member[1] return attributes_dict From 04b751d94ded9bfd39a231f03777b9782bddd495 Mon Sep 17 00:00:00 2001 From: MateusStano Date: Fri, 17 Nov 2023 19:08:21 +0100 Subject: [PATCH 4/6] BUG: fix time_array assignment in Environment class --- rocketpy/environment/environment.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rocketpy/environment/environment.py b/rocketpy/environment/environment.py index 741964d84..0f8230772 100644 --- a/rocketpy/environment/environment.py +++ b/rocketpy/environment/environment.py @@ -2365,7 +2365,7 @@ def process_forecast_reanalysis(self, file, dictionary): self.wind_vs = wind_vs self.levels = levels self.temperatures = temperatures - self.time_array = time_array + self.time_array = time_array[:].tolist() self.height = height # Close weather data @@ -2735,7 +2735,7 @@ def process_ensemble(self, file, dictionary): self.wind_vs = wind_vs self.levels = levels self.temperatures = temperatures - self.time_array = time_array + self.time_array = time_array[:].tolist() self.height = height # Close weather data From 438f4c0f5cc564797ddc4c4db026b7cac017e800 Mon Sep 17 00:00:00 2001 From: MateusStano Date: Wed, 29 Nov 2023 17:44:19 +0100 Subject: [PATCH 5/6] MNT: improve warnings --- rocketpy/environment/environment.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rocketpy/environment/environment.py b/rocketpy/environment/environment.py index 0f8230772..63d24fa9e 100644 --- a/rocketpy/environment/environment.py +++ b/rocketpy/environment/environment.py @@ -3133,7 +3133,8 @@ def all_plot_info_returned(self): """ warnings.warn( - "The method 'all_plot_info_returned' is deprecated. " + "The method 'all_plot_info_returned' is deprecated as of version " + + "1.2 and will be removed in version 1.4 " + "Use 'utilities.get_instance_attributes' instead.", DeprecationWarning, ) @@ -3205,7 +3206,8 @@ def all_info_returned(self): """ warnings.warn( - "The method 'all_info_returned' is deprecated. " + "The method 'all_info_returned' is deprecated as of version " + + "1.2 and will be removed in version 1.4 " + "Use 'utilities.get_instance_attributes' instead.", DeprecationWarning, ) From b27edc3c902dfdd8edc89578ea06feb198c0448b Mon Sep 17 00:00:00 2001 From: MateusStano Date: Wed, 29 Nov 2023 19:01:32 +0100 Subject: [PATCH 6/6] TST: add test --- tests/test_utilities.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 577a8aaa7..aeb374db6 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -161,3 +161,16 @@ def test_fin_flutter_analysis(mock_show, flight): ) == None ) + + +def test_get_instance_attributes(flight_calisto_robust): + """Tests if get_instance_attributes returns the expected results for a + robust flight object.""" + + attributes = utilities.get_instance_attributes(flight_calisto_robust) + for key, value in attributes.items(): + attr = getattr(flight_calisto_robust, key) + if isinstance(attr, np.ndarray): + assert np.allclose(attr, value) + else: + assert attr == value