Return child state with indexed name part as its ast tree. ``index`` can be: - an integer, in which case the student/solution_parts are indexed by position. - a string, in which case the student/solution_parts are expected to be a dictionary. - a list of indices (which can be integ
(state, name, index, part_msg, missing_msg=None, expand_msg=None)
| 65 | |
| 66 | |
| 67 | def check_part_index(state, name, index, part_msg, missing_msg=None, expand_msg=None): |
| 68 | """Return child state with indexed name part as its ast tree. |
| 69 | |
| 70 | ``index`` can be: |
| 71 | |
| 72 | - an integer, in which case the student/solution_parts are indexed by position. |
| 73 | - a string, in which case the student/solution_parts are expected to be a dictionary. |
| 74 | - a list of indices (which can be integer or string), in which case the student parts are indexed step by step. |
| 75 | """ |
| 76 | |
| 77 | if missing_msg is None: |
| 78 | missing_msg = "Are you sure you defined the {{part}}? " |
| 79 | if expand_msg is None: |
| 80 | expand_msg = "Did you correctly specify the {{part}}? " |
| 81 | |
| 82 | # create message |
| 83 | ordinal = get_ord(index + 1) if isinstance(index, int) else "" |
| 84 | fmt_kwargs = {"index": index, "ordinal": ordinal} |
| 85 | fmt_kwargs.update(part=render(part_msg, fmt_kwargs)) |
| 86 | |
| 87 | append_message = FeedbackComponent(expand_msg, fmt_kwargs) |
| 88 | |
| 89 | # check there are enough parts for index |
| 90 | has_part(state, name, missing_msg, fmt_kwargs, index) |
| 91 | |
| 92 | # get part at index |
| 93 | stu_part = state.student_parts[name] |
| 94 | sol_part = state.solution_parts[name] |
| 95 | |
| 96 | if isinstance(index, list): |
| 97 | for ind in index: |
| 98 | stu_part = stu_part[ind] |
| 99 | sol_part = sol_part[ind] |
| 100 | else: |
| 101 | stu_part = stu_part[index] |
| 102 | sol_part = sol_part[index] |
| 103 | |
| 104 | assert_ast(state, sol_part, fmt_kwargs) |
| 105 | |
| 106 | # return child state from part |
| 107 | return part_to_child(stu_part, sol_part, append_message, state) |
| 108 | |
| 109 | |
| 110 | def check_node( |