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

Method to_child

pythonwhat/State.py:116–182  ·  view source on GitHub ↗

Dive into nested tree. Set the current state as a state with a subtree of this syntax tree as student tree and solution tree. This is necessary when testing if statements or for loops for example.

(self, append_message=None, node_name="", **kwargs)

Source from the content-addressed store, hash-verified

114 return self.manual_sigs
115
116 def to_child(self, append_message=None, node_name="", **kwargs):
117 """Dive into nested tree.
118
119 Set the current state as a state with a subtree of this syntax tree as
120 student tree and solution tree. This is necessary when testing if statements or
121 for loops for example.
122 """
123 bad_parameters = set(kwargs) - set(self.parameters)
124 if bad_parameters:
125 raise ValueError(
126 "Invalid init parameters for State: %s" % ", ".join(bad_parameters)
127 )
128
129 base_kwargs = {
130 attr: getattr(self, attr)
131 for attr in self.parameters
132 if hasattr(self, attr) and attr not in ["ast_dispatcher", "highlight"]
133 }
134
135 if append_message and not isinstance(append_message, FeedbackComponent):
136 append_message = FeedbackComponent(append_message)
137 kwargs["feedback_context"] = append_message
138 kwargs["creator"] = {"type": "to_child", "args": {"state": self}}
139
140 def update_kwarg(name, func):
141 kwargs[name] = func(kwargs[name])
142
143 def update_context(name):
144 update_kwarg(name, getattr(self, name).update_ctx)
145
146 for ast_arg in ["student_ast", "solution_ast"]:
147 if isinstance(kwargs.get(ast_arg), list):
148 update_kwarg(ast_arg, wrap_in_module)
149
150 if kwargs.get("student_ast") and kwargs.get("student_code") is None:
151 kwargs["student_code"] = self.student_ast_tokens.get_text(
152 kwargs["student_ast"]
153 )
154 if kwargs.get("solution_ast") and kwargs.get("solution_code") is None:
155 kwargs["solution_code"] = self.solution_ast_tokens.get_text(
156 kwargs["solution_ast"]
157 )
158
159 for context in [
160 "student_context",
161 "solution_context",
162 "student_env",
163 "solution_env",
164 ]:
165 if context in kwargs:
166 if kwargs[context] is not None:
167 update_context(context)
168 else:
169 kwargs.pop(context)
170
171 klass = self.SUBCLASSES[node_name] if node_name else State
172 init_kwargs = {**base_kwargs, **kwargs}
173 child = klass(**init_kwargs)

Callers 8

runFunction · 0.80
part_to_childFunction · 0.80
check_functionFunction · 0.80
overrideFunction · 0.80
set_contextFunction · 0.80
set_envFunction · 0.80
_testFunction · 0.80
callFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected