Skip to content

Commit e89cd49

Browse files
removes lambda support from parachutes
1 parent e3c389c commit e89cd49

File tree

2 files changed

+7
-63
lines changed

2 files changed

+7
-63
lines changed

lib/models/rocket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Parachute(BaseModel):
2121
cd_s: float = 10
2222
sampling_rate: int = 105
2323
lag: float = 1.5
24-
trigger: Union[str, float] = "lambda p, h, y: y[5] < 0 and h < 800"
24+
trigger: Union[str, float] = "apogee"
2525
noise: Tuple[float, float, float] = (0, 8.3, 0.5)
2626

2727

lib/services/rocket.py

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ast
21
from typing import Self
32

43
import dill
@@ -81,14 +80,7 @@ def from_rocket_model(cls, rocket: Rocket) -> Self:
8180

8281
# Parachutes
8382
for parachute in rocket.parachutes:
84-
if cls.check_parachute_trigger(
85-
trigger_expression := parachute.trigger
86-
):
87-
parachute.trigger = eval( # pylint: disable=eval-used
88-
trigger_expression,
89-
{"__builtins__": None},
90-
{"apogee": "apogee"},
91-
)
83+
if cls.check_parachute_trigger(parachute.trigger):
9284
rocketpy_parachute = cls.get_rocketpy_parachute(parachute)
9385
rocketpy_rocket.parachutes.append(rocketpy_parachute)
9486
else:
@@ -219,67 +211,19 @@ def get_rocketpy_parachute(parachute: Parachute) -> RocketPyParachute:
219211
return rocketpy_parachute
220212

221213
@staticmethod
222-
def check_parachute_trigger(expression) -> bool:
214+
def check_parachute_trigger(trigger) -> bool:
223215
"""
224216
Check if the trigger expression is valid.
225217
226218
Args:
227-
expression: str
219+
trigger: str | float
228220
229221
Returns:
230222
bool: True if the expression is valid, False otherwise.
231223
"""
232224

233-
# Parsing the expression into an AST
234-
try:
235-
parsed_expression = ast.parse(expression, mode="eval")
236-
except SyntaxError as e:
237-
raise InvalidParachuteTrigger(
238-
f"Invalid expression syntax: {str(e)}"
239-
) from None
240-
241-
# Constant case (supported after beta v1)
242-
if isinstance(parsed_expression.body, ast.Constant):
225+
if trigger == "apogee":
243226
return True
244-
# Name case (supported after beta v1)
245-
if (
246-
isinstance(parsed_expression.body, ast.Name)
247-
and parsed_expression.body.id == "apogee"
248-
):
227+
if isinstance(trigger, (int, float)):
249228
return True
250-
251-
# Validating the expression structure
252-
if not isinstance(parsed_expression.body, ast.Lambda):
253-
raise InvalidParachuteTrigger(
254-
"Invalid expression structure: not a lambda."
255-
) from None
256-
257-
lambda_node = parsed_expression.body
258-
if len(lambda_node.args.args) != 3:
259-
raise InvalidParachuteTrigger(
260-
"Invalid expression structure: lambda should have 3 arguments."
261-
) from None
262-
263-
if not isinstance(lambda_node.body, ast.Compare):
264-
try:
265-
for operand in lambda_node.body.values:
266-
if not isinstance(operand, ast.Compare):
267-
raise InvalidParachuteTrigger(
268-
"Invalid expression structure: not a Compare."
269-
) from None
270-
except AttributeError:
271-
raise InvalidParachuteTrigger(
272-
"Invalid expression structure: not a Compare."
273-
) from None
274-
275-
# Restricting access to functions or attributes
276-
for node in ast.walk(lambda_node):
277-
if isinstance(node, ast.Call):
278-
raise InvalidParachuteTrigger(
279-
"Calling functions is not allowed in the expression."
280-
) from None
281-
if isinstance(node, ast.Attribute):
282-
raise InvalidParachuteTrigger(
283-
"Accessing attributes is not allowed in the expression."
284-
) from None
285-
return True
229+
return False

0 commit comments

Comments
 (0)