Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Get Instance Attributes #477

Merged
merged 7 commits into from
Dec 2, 2023
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
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 @@
self.wind_vs = wind_vs
self.levels = levels
self.temperatures = temperatures
self.time_array = time_array
self.time_array = time_array[:].tolist()
Gui-FernandesBR marked this conversation as resolved.
Show resolved Hide resolved
self.height = height

# Close weather data
Expand Down Expand Up @@ -2735,7 +2735,7 @@
self.wind_vs = wind_vs
self.levels = levels
self.temperatures = temperatures
self.time_array = time_array
self.time_array = time_array[:].tolist()

Check warning on line 2738 in rocketpy/environment/environment.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/environment/environment.py#L2738

Added line #L2738 was not covered by tests
self.height = height

# Close weather data
Expand Down Expand Up @@ -3126,7 +3126,19 @@
------
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 @@
------
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):
Gui-FernandesBR marked this conversation as resolved.
Show resolved Hide resolved
"""Returns a dictionary with all attributes of a given instance.
Parameters
----------
instance : object
Instance of a class.
Returns
-------
Gui-FernandesBR marked this conversation as resolved.
Show resolved Hide resolved
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("__"):
phmbressan marked this conversation as resolved.
Show resolved Hide resolved
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
Loading