Skip to content

Commit 5a50431

Browse files
Merge pull request #12 from lambda-feedback/tr151-cases-early-out
Changed behaviour so that the command handler finished as soon as a m…
2 parents c747a34 + f4eb095 commit 5a50431

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

tools/commands.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def evaluate(event: JsonType) -> Response:
129129

130130
result = evaluation_function(body["response"], body["answer"], params)
131131

132-
if "cases" in params and len(params["cases"]) > 0:
132+
if result["is_correct"] is False and "cases" in params and len(params["cases"]) > 0:
133133
match, warnings = get_case_feedback(
134134
body["response"], params, params["cases"]
135135
)
@@ -170,7 +170,10 @@ def get_case_feedback(
170170
issues encountered when evaluating each case against the student's
171171
response.
172172
"""
173-
matches, feedback, warnings = evaluate_all_cases(response, params, cases)
173+
# NOTE: Previous behaviour, where all cases are evaluated but only the feedback
174+
# for the first case is returned can be restored by changing the line below to:
175+
# matches, feedback, warnings = evaluate_all_cases(response, params, cases)
176+
matches, feedback, warnings = find_first_matching_case(response, params, cases)
174177

175178
if not matches:
176179
return None, warnings
@@ -198,6 +201,38 @@ def get_case_feedback(
198201

199202
return match, warnings
200203

204+
def find_first_matching_case(
205+
response: Any,
206+
params: Dict,
207+
cases: List[Dict],
208+
) -> Tuple[List[int], List[str], List[CaseWarning]]:
209+
"""Evaluates cases until it finds matching one.
210+
211+
Args:
212+
response (Any): The student's response.
213+
params (Dict): The params of the evaluation function.
214+
cases (List[Dict]): A list of cases to check against.
215+
216+
Returns:
217+
Tuple[List[int], List[str], List[CaseWarning]]: Returns a list of
218+
indices of cases that match a student's response, a list of feedback
219+
strings from each case, and a list of issues encountered when
220+
evaluating cases against the student's response.
221+
"""
222+
matches, feedback, warnings = [], [], []
223+
224+
for index, case in enumerate(cases):
225+
result = evaluate_case(response, params, case, index)
226+
227+
if result.warning is not None:
228+
warnings.append(result.warning)
229+
230+
if result.is_correct:
231+
matches.append(index)
232+
feedback.append(result.feedback)
233+
break
234+
235+
return matches, feedback, warnings
201236

202237
def evaluate_all_cases(
203238
response: Any,

0 commit comments

Comments
 (0)