MCPcopy Index your code
hub / github.com/datacamp/pythonwhat / override

Function override

pythonwhat/checks/check_logic.py:124–161  ·  view source on GitHub ↗

Override the solution code with something arbitrary. There might be cases in which you want to temporarily override the solution code so you can allow for alternative ways of solving an exercise. When you use ``override()`` in an SCT chain, the remainder of that SCT chain will run a

(state, solution)

Source from the content-addressed store, hash-verified

122
123
124def override(state, solution):
125 """Override the solution code with something arbitrary.
126
127 There might be cases in which you want to temporarily override the solution code
128 so you can allow for alternative ways of solving an exercise.
129 When you use ``override()`` in an SCT chain, the remainder of that SCT chain will
130 run as if the solution code you specified is the only code that was in the solution.
131
132 Check the glossary for an example (pandas plotting)
133
134 Args:
135 solution: solution code as a string that overrides the original solution code.
136 state: State instance describing student and solution code. Can be omitted if used with Ex().
137 """
138
139 # the old ast may be a number of node types, but generally either a
140 # (1) ast.Module, or for single expressions...
141 # (2) whatever was grabbed using module.body[0]
142 # (3) module.body[0].value, when module.body[0] is an Expr node
143 old_ast = state.solution_ast
144 new_ast = ast.parse(solution)
145 if not isinstance(old_ast, ast.Module) and len(new_ast.body) == 1:
146 expr = new_ast.body[0]
147 candidates = [expr, expr.value] if isinstance(expr, ast.Expr) else [expr]
148 for node in candidates:
149 if isinstance(node, old_ast.__class__):
150 new_ast = node
151 break
152
153 kwargs = state.feedback_context.kwargs if state.feedback_context else {}
154 child = state.to_child(
155 solution_ast=new_ast,
156 student_ast=state.student_ast,
157 highlight=state.highlight,
158 append_message=FeedbackComponent("", kwargs),
159 )
160
161 return child
162
163
164def set_context(state, *args, **kwargs):

Callers

nothing calls this directly

Calls 2

to_childMethod · 0.80
parseMethod · 0.45

Tested by

no test coverage detected