Skip to content

Commit

Permalink
Merge pull request #477 from RocketPy-Team/enh/get-attributes
Browse files Browse the repository at this point in the history
ENH: Get Instance Attributes
  • Loading branch information
MateusStano authored Dec 2, 2023
2 parents a9ac31c + b27edc3 commit feb3299
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
27 changes: 25 additions & 2 deletions rocketpy/environment/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -3126,7 +3126,19 @@ 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 as of version "
+ "1.2 and will be removed in version 1.4 "
+ "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],
Expand Down Expand Up @@ -3187,7 +3199,18 @@ 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 as of version "
+ "1.2 and will be removed in version 1.4 "
+ "Use 'utilities.get_instance_attributes' instead.",
DeprecationWarning,
)

# Dictionary creation, if not commented follows the SI
info = dict(
Expand Down
23 changes: 23 additions & 0 deletions rocketpy/utilities.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import traceback
import warnings

Expand Down Expand Up @@ -654,3 +655,25 @@ 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 = 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
13 changes: 13 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit feb3299

Please sign in to comment.