Skip to content

Commit 476a870

Browse files
ENH: improve some Components class methods
1 parent 1606bbd commit 476a870

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

rocketpy/rocket/components.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ def __init__(self):
2525

2626
def __repr__(self):
2727
"""Return a string representation of the Components instance."""
28-
return repr(self._components)
28+
components_str = "\n".join(
29+
[
30+
f"\tComponent: {str(c.component):80} Position: {c.position:>6.3f}"
31+
for c in self._components
32+
]
33+
)
34+
return f"Components:\n{components_str}"
2935

3036
def __len__(self):
3137
"""Return the number of components in the list of components."""
@@ -155,3 +161,19 @@ def clear(self):
155161
None
156162
"""
157163
self._components.clear()
164+
165+
def sort_by_position(self, reverse=False):
166+
"""Sort the list of components by position.
167+
168+
Parameters
169+
----------
170+
reverse : bool
171+
If True, sort in descending order. If False, sort in ascending
172+
order.
173+
174+
Returns
175+
-------
176+
None
177+
"""
178+
self._components.sort(key=lambda x: x.position, reverse=reverse)
179+
return None

0 commit comments

Comments
 (0)