|
41 | 41 | }, |
42 | 42 | "outputs": [], |
43 | 43 | "source": [ |
44 | | - "from scwidgets import CodeInput, CodeExercise, Check, CheckRegistry\n", |
| 44 | + "from scwidgets import CodeInput, CodeExercise, Check, CheckRegistry, ExerciseRegistry\n", |
45 | 45 | "\n", |
46 | 46 | "import numpy as np " |
47 | 47 | ] |
|
143 | 143 | " import numpy as np\n", |
144 | 144 | " return np.cos(arr) # oops! wrong solution\n", |
145 | 145 | "\n", |
146 | | - "code_input_sinus = CodeInput(sinus)\n", |
147 | | - "\n", |
148 | 146 | "check_code_ex = CodeExercise(\n", |
149 | 147 | " key=\"sinus_with_references_2\",\n", |
150 | | - " code=code_input_sinus,\n", |
| 148 | + " title=\"sinus\",\n", |
| 149 | + " code=sinus,\n", |
151 | 150 | " check_registry=check_registry,\n", |
152 | 151 | ")\n", |
153 | 152 | "\n", |
|
162 | 161 | " outputs_references=[(np.asarray([0., 7.07106781e-01, 1.00000000e+00, 7.07106781e-01, 0.]),)]\n", |
163 | 162 | ")\n", |
164 | 163 | "\n", |
165 | | - "check_code_ex.run_check()\n", |
| 164 | + "#check_code_ex.run_check()\n", |
166 | 165 | "check_code_ex" |
167 | 166 | ] |
168 | 167 | }, |
169 | | - { |
170 | | - "cell_type": "code", |
171 | | - "execution_count": null, |
172 | | - "id": "10", |
173 | | - "metadata": {}, |
174 | | - "outputs": [], |
175 | | - "source": [ |
176 | | - "print(code_input_sinus.full_function_code)" |
177 | | - ] |
178 | | - }, |
179 | 168 | { |
180 | 169 | "cell_type": "markdown", |
181 | | - "id": "11", |
| 170 | + "id": "10", |
182 | 171 | "metadata": {}, |
183 | 172 | "source": [ |
184 | | - "One can adapt the default arguments by using partial functions" |
| 173 | + "One can adapt the default arguments of the asserts by using partial functions" |
185 | 174 | ] |
186 | 175 | }, |
187 | 176 | { |
188 | 177 | "cell_type": "code", |
189 | 178 | "execution_count": null, |
190 | | - "id": "12", |
| 179 | + "id": "11", |
191 | 180 | "metadata": { |
192 | 181 | "tags": [] |
193 | 182 | }, |
|
199 | 188 | { |
200 | 189 | "cell_type": "code", |
201 | 190 | "execution_count": null, |
202 | | - "id": "13", |
| 191 | + "id": "12", |
203 | 192 | "metadata": { |
204 | 193 | "tags": [] |
205 | 194 | }, |
|
212 | 201 | }, |
213 | 202 | { |
214 | 203 | "cell_type": "markdown", |
215 | | - "id": "14", |
| 204 | + "id": "13", |
216 | 205 | "metadata": {}, |
217 | 206 | "source": [ |
218 | 207 | "## Testing functional behavior" |
|
221 | 210 | { |
222 | 211 | "cell_type": "code", |
223 | 212 | "execution_count": null, |
224 | | - "id": "15", |
| 213 | + "id": "14", |
225 | 214 | "metadata": { |
226 | 215 | "tags": [] |
227 | 216 | }, |
|
231 | 220 | " import numpy as np\n", |
232 | 221 | " return np.cos(arr) # oops! wrong solution\n", |
233 | 222 | "\n", |
234 | | - "code_input_sinus = CodeInput(sinus)\n", |
235 | | - "\n", |
236 | | - "check_code_ex = CodeExercise(\n", |
| 223 | + "code_ex_functional_behavior = CodeExercise(\n", |
237 | 224 | " key=\"sinus_functional_behavior\",\n", |
238 | | - " code=code_input_sinus,\n", |
| 225 | + " code=sinus,\n", |
239 | 226 | " check_registry=check_registry,\n", |
240 | 227 | ")\n", |
241 | 228 | "\n", |
242 | 229 | "def assert_2pi_periodic() -> str:\n", |
243 | | - " out = code_input_sinus.get_function_object()([0, 2*np.pi])\n", |
| 230 | + " out = code_ex_functional_behavior.code([0, 2*np.pi])\n", |
244 | 231 | " if not np.allclose(out[0], out[1]):\n", |
245 | 232 | " return \"Function is not periodic.\"\n", |
246 | 233 | " return \"\" # empty strings means it passes\n", |
247 | 234 | "\n", |
248 | 235 | "check_registry.add_check(\n", |
249 | | - " check_code_ex,\n", |
| 236 | + " code_ex_functional_behavior,\n", |
250 | 237 | " asserts=[\n", |
251 | 238 | " assert_2pi_periodic,\n", |
252 | 239 | " ]\n", |
253 | 240 | ")\n", |
254 | 241 | "\n", |
255 | | - "check_code_ex.run_check()\n", |
256 | | - "check_code_ex" |
| 242 | + "code_ex_functional_behavior.run_check()\n", |
| 243 | + "code_ex_functional_behavior" |
257 | 244 | ] |
258 | 245 | }, |
259 | 246 | { |
260 | 247 | "cell_type": "markdown", |
261 | | - "id": "16", |
| 248 | + "id": "15", |
262 | 249 | "metadata": { |
263 | 250 | "tags": [] |
264 | 251 | }, |
|
269 | 256 | { |
270 | 257 | "cell_type": "code", |
271 | 258 | "execution_count": null, |
272 | | - "id": "17", |
| 259 | + "id": "16", |
273 | 260 | "metadata": { |
274 | 261 | "tags": [] |
275 | 262 | }, |
|
320 | 307 | }, |
321 | 308 | { |
322 | 309 | "cell_type": "markdown", |
323 | | - "id": "18", |
324 | | - "metadata": { |
325 | | - "raw_mimetype": "text/markdown", |
326 | | - "tags": [] |
327 | | - }, |
328 | | - "source": [ |
329 | | - "### Solution" |
330 | | - ] |
331 | | - }, |
332 | | - { |
333 | | - "cell_type": "code", |
334 | | - "execution_count": null, |
335 | | - "id": "19", |
336 | | - "metadata": { |
337 | | - "tags": [] |
338 | | - }, |
339 | | - "outputs": [], |
340 | | - "source": [ |
341 | | - "# Once you enter the solution you can get the reference output with\n", |
342 | | - "string_to_int(check_code_ex.compute_output_to_check())" |
343 | | - ] |
344 | | - }, |
345 | | - { |
346 | | - "cell_type": "markdown", |
347 | | - "id": "20", |
| 310 | + "id": "17", |
348 | 311 | "metadata": {}, |
349 | 312 | "source": [ |
350 | 313 | "## Checking all widgets" |
351 | 314 | ] |
352 | 315 | }, |
353 | 316 | { |
354 | 317 | "cell_type": "markdown", |
355 | | - "id": "21", |
| 318 | + "id": "18", |
356 | 319 | "metadata": {}, |
357 | 320 | "source": [ |
358 | 321 | "The check registry also provides the possibility to check all the widgets. " |
|
361 | 324 | { |
362 | 325 | "cell_type": "code", |
363 | 326 | "execution_count": null, |
364 | | - "id": "22", |
| 327 | + "id": "19", |
365 | 328 | "metadata": { |
366 | 329 | "tags": [] |
367 | 330 | }, |
|
372 | 335 | }, |
373 | 336 | { |
374 | 337 | "cell_type": "markdown", |
375 | | - "id": "23", |
| 338 | + "id": "20", |
376 | 339 | "metadata": {}, |
377 | 340 | "source": [ |
378 | | - "We create a widget that will raise an error to show how this is visualized." |
| 341 | + "For the demo to automatically we simulate a button press using the private function that should not be used" |
379 | 342 | ] |
380 | 343 | }, |
381 | 344 | { |
382 | 345 | "cell_type": "code", |
383 | 346 | "execution_count": null, |
384 | | - "id": "24", |
385 | | - "metadata": { |
386 | | - "tags": [] |
387 | | - }, |
388 | | - "outputs": [], |
389 | | - "source": [ |
390 | | - "def error(arr):\n", |
391 | | - " raise ValueError(\"Oops!\")\n", |
392 | | - " return arr\n", |
393 | | - "\n", |
394 | | - "check_code_ex = CodeExercise(\n", |
395 | | - " key=\"will_raise_error\",\n", |
396 | | - " code=error,\n", |
397 | | - " check_registry=check_registry,\n", |
398 | | - ")\n", |
399 | | - "\n", |
400 | | - "check_registry.add_check(\n", |
401 | | - " check_code_ex,\n", |
402 | | - " asserts=[\n", |
403 | | - " assert_type,\n", |
404 | | - " ],\n", |
405 | | - " inputs_parameters=[{\"arr\": np.asarray([1., 2., 3.5])}],\n", |
406 | | - " outputs_references=[(np.asarray([1., 2., 3.5]),)]\n", |
407 | | - ")\n", |
408 | | - "\n", |
409 | | - "check_code_ex.run_check()\n", |
410 | | - "check_code_ex" |
411 | | - ] |
412 | | - }, |
413 | | - { |
414 | | - "cell_type": "code", |
415 | | - "execution_count": null, |
416 | | - "id": "25", |
417 | | - "metadata": { |
418 | | - "tags": [] |
419 | | - }, |
| 347 | + "id": "21", |
| 348 | + "metadata": {}, |
420 | 349 | "outputs": [], |
421 | 350 | "source": [ |
422 | | - "# For the demo to automatically run we simulate a button press using the private function that should not be used\n", |
423 | 351 | "check_registry._check_all_widgets_button.click()" |
424 | 352 | ] |
425 | 353 | } |
|
0 commit comments