Skip to content

Commit e323123

Browse files
extends parachute input types
1 parent 0f0cbd5 commit e323123

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

lib/models/rocket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from enum import Enum
2-
from typing import Optional, Tuple, List
2+
from typing import Optional, Tuple, List, Union
33
from pydantic import BaseModel
44
from lib.models.motor import Motor
55
from lib.models.aerosurfaces import (
@@ -21,7 +21,7 @@ class Parachute(BaseModel):
2121
cd_s: float = 10
2222
sampling_rate: int = 105
2323
lag: float = 1.5
24-
trigger: str = "lambda p, h, y: y[5] < 0 and h < 800"
24+
trigger: Union[str, float] = "lambda p, h, y: y[5] < 0 and h < 800"
2525
noise: Tuple[float, float, float] = (0, 8.3, 0.5)
2626

2727

lib/services/rocket.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
from lib.views.rocket import RocketSummary
2222

2323

24+
class InvalidParachuteTrigger(Exception):
25+
"""Exception raised for invalid parachute trigger expressions."""
26+
27+
2428
class RocketService:
2529
_rocket: RocketPyRocket
2630

@@ -85,7 +89,9 @@ def from_rocket_model(cls, rocket: Rocket) -> Self:
8589
trigger_expression := parachute.trigger
8690
):
8791
parachute.trigger = eval( # pylint: disable=eval-used
88-
trigger_expression, {"__builtins__": None}, {}
92+
trigger_expression,
93+
{"__builtins__": None},
94+
{"apogee": "apogee"},
8995
)
9096
rocketpy_parachute = cls.get_rocketpy_parachute(parachute)
9197
rocketpy_rocket.parachutes.append(rocketpy_parachute)
@@ -217,7 +223,7 @@ def get_rocketpy_parachute(parachute: Parachute) -> RocketPyParachute:
217223
return rocketpy_parachute
218224

219225
@staticmethod
220-
def check_parachute_trigger(expression: str) -> bool:
226+
def check_parachute_trigger(expression) -> bool:
221227
"""
222228
Check if the trigger expression is valid.
223229
@@ -228,9 +234,6 @@ def check_parachute_trigger(expression: str) -> bool:
228234
bool: True if the expression is valid, False otherwise.
229235
"""
230236

231-
class InvalidParachuteTrigger(Exception):
232-
pass
233-
234237
# Parsing the expression into an AST
235238
try:
236239
parsed_expression = ast.parse(expression, mode="eval")

0 commit comments

Comments
 (0)