Skip to content

Commit d11ef8a

Browse files
authored
Update the check notebook (#113)
Replace old usage of `get_function_object()(...)` with `code(...)` Remove section that refers how to compute output for a check because it can be now just done with `code_ex.code()`. Remove widget that raises error since it is confusing what is meant. The connection with the check registry is not made.
1 parent 61f3bf1 commit d11ef8a

File tree

1 file changed

+25
-97
lines changed

1 file changed

+25
-97
lines changed

docs/src/check.ipynb

Lines changed: 25 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"outputs": [],
4343
"source": [
44-
"from scwidgets import CodeInput, CodeExercise, Check, CheckRegistry\n",
44+
"from scwidgets import CodeInput, CodeExercise, Check, CheckRegistry, ExerciseRegistry\n",
4545
"\n",
4646
"import numpy as np "
4747
]
@@ -143,11 +143,10 @@
143143
" import numpy as np\n",
144144
" return np.cos(arr) # oops! wrong solution\n",
145145
"\n",
146-
"code_input_sinus = CodeInput(sinus)\n",
147-
"\n",
148146
"check_code_ex = CodeExercise(\n",
149147
" key=\"sinus_with_references_2\",\n",
150-
" code=code_input_sinus,\n",
148+
" title=\"sinus\",\n",
149+
" code=sinus,\n",
151150
" check_registry=check_registry,\n",
152151
")\n",
153152
"\n",
@@ -162,32 +161,22 @@
162161
" outputs_references=[(np.asarray([0., 7.07106781e-01, 1.00000000e+00, 7.07106781e-01, 0.]),)]\n",
163162
")\n",
164163
"\n",
165-
"check_code_ex.run_check()\n",
164+
"#check_code_ex.run_check()\n",
166165
"check_code_ex"
167166
]
168167
},
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-
},
179168
{
180169
"cell_type": "markdown",
181-
"id": "11",
170+
"id": "10",
182171
"metadata": {},
183172
"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"
185174
]
186175
},
187176
{
188177
"cell_type": "code",
189178
"execution_count": null,
190-
"id": "12",
179+
"id": "11",
191180
"metadata": {
192181
"tags": []
193182
},
@@ -199,7 +188,7 @@
199188
{
200189
"cell_type": "code",
201190
"execution_count": null,
202-
"id": "13",
191+
"id": "12",
203192
"metadata": {
204193
"tags": []
205194
},
@@ -212,7 +201,7 @@
212201
},
213202
{
214203
"cell_type": "markdown",
215-
"id": "14",
204+
"id": "13",
216205
"metadata": {},
217206
"source": [
218207
"## Testing functional behavior"
@@ -221,7 +210,7 @@
221210
{
222211
"cell_type": "code",
223212
"execution_count": null,
224-
"id": "15",
213+
"id": "14",
225214
"metadata": {
226215
"tags": []
227216
},
@@ -231,34 +220,32 @@
231220
" import numpy as np\n",
232221
" return np.cos(arr) # oops! wrong solution\n",
233222
"\n",
234-
"code_input_sinus = CodeInput(sinus)\n",
235-
"\n",
236-
"check_code_ex = CodeExercise(\n",
223+
"code_ex_functional_behavior = CodeExercise(\n",
237224
" key=\"sinus_functional_behavior\",\n",
238-
" code=code_input_sinus,\n",
225+
" code=sinus,\n",
239226
" check_registry=check_registry,\n",
240227
")\n",
241228
"\n",
242229
"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",
244231
" if not np.allclose(out[0], out[1]):\n",
245232
" return \"Function is not periodic.\"\n",
246233
" return \"\" # empty strings means it passes\n",
247234
"\n",
248235
"check_registry.add_check(\n",
249-
" check_code_ex,\n",
236+
" code_ex_functional_behavior,\n",
250237
" asserts=[\n",
251238
" assert_2pi_periodic,\n",
252239
" ]\n",
253240
")\n",
254241
"\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"
257244
]
258245
},
259246
{
260247
"cell_type": "markdown",
261-
"id": "16",
248+
"id": "15",
262249
"metadata": {
263250
"tags": []
264251
},
@@ -269,7 +256,7 @@
269256
{
270257
"cell_type": "code",
271258
"execution_count": null,
272-
"id": "17",
259+
"id": "16",
273260
"metadata": {
274261
"tags": []
275262
},
@@ -320,39 +307,15 @@
320307
},
321308
{
322309
"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",
348311
"metadata": {},
349312
"source": [
350313
"## Checking all widgets"
351314
]
352315
},
353316
{
354317
"cell_type": "markdown",
355-
"id": "21",
318+
"id": "18",
356319
"metadata": {},
357320
"source": [
358321
"The check registry also provides the possibility to check all the widgets. "
@@ -361,7 +324,7 @@
361324
{
362325
"cell_type": "code",
363326
"execution_count": null,
364-
"id": "22",
327+
"id": "19",
365328
"metadata": {
366329
"tags": []
367330
},
@@ -372,54 +335,19 @@
372335
},
373336
{
374337
"cell_type": "markdown",
375-
"id": "23",
338+
"id": "20",
376339
"metadata": {},
377340
"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"
379342
]
380343
},
381344
{
382345
"cell_type": "code",
383346
"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": {},
420349
"outputs": [],
421350
"source": [
422-
"# For the demo to automatically run we simulate a button press using the private function that should not be used\n",
423351
"check_registry._check_all_widgets_button.click()"
424352
]
425353
}

0 commit comments

Comments
 (0)