Skip to content

Commit f05ea45

Browse files
authored
Rename exercise widgets related member variables (#108)
`exercise_key` to `key` `exercise_description` to `description` `exercise_title` to `title`
1 parent 8999150 commit f05ea45

File tree

4 files changed

+86
-67
lines changed

4 files changed

+86
-67
lines changed

src/scwidgets/exercise/_widget_code_exercise.py

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ class CodeExercise(VBox, CheckableWidget, ExerciseWidget):
4040
A function or CodeInput that is the input of code
4141
4242
:param check_registry:
43-
a check registry that is used to register checks
43+
A check registry that is used to register checks
44+
45+
:param exercise_registry:
46+
A exercise registry that is used to register the answers to save them
47+
later. If specified the save and load panel will appear.
48+
49+
:param key:
50+
The key that is used to store the exercise in the json file.
4451
4552
:param parameters:
4653
Input parameters for the :py:class:`ParametersPanel` class or an initialized
@@ -58,14 +65,20 @@ class CodeExercise(VBox, CheckableWidget, ExerciseWidget):
5865
A function that is run during the update process. The function takes as argument
5966
the CodeExercise, so it can update all cue_ouputs
6067
68+
:param description:
69+
A string describing the exercises that will be put into an HTML widget
70+
above the exercise.
71+
72+
:param title:
73+
A title for the exercise. If not given the key is used.
6174
"""
6275

6376
def __init__(
6477
self,
6578
code: Union[None, WidgetCodeInput, types.FunctionType] = None,
6679
check_registry: Optional[CheckRegistry] = None,
6780
exercise_registry: Optional[ExerciseRegistry] = None,
68-
exercise_key: Optional[str] = None,
81+
key: Optional[str] = None,
6982
parameters: Optional[
7083
Union[Dict[str, Union[Check.FunInParamT, Widget]], ParametersPanel]
7184
] = None,
@@ -77,8 +90,8 @@ def __init__(
7790
Callable[[], Union[Any, Check.FunOutParamsT]],
7891
]
7992
] = None,
80-
exercise_description: Optional[str] = None,
81-
exercise_title: Optional[str] = None,
93+
description: Optional[str] = None,
94+
title: Optional[str] = None,
8295
*args,
8396
**kwargs,
8497
):
@@ -117,26 +130,26 @@ def __init__(
117130
else:
118131
self._update_func_nb_nondefault_args = None
119132

120-
self._exercise_description = exercise_description
121-
if exercise_description is None:
122-
self._exercise_description_html = None
133+
self._description = description
134+
if description is None:
135+
self._description_html = None
123136
else:
124-
self._exercise_description_html = HTMLMath(self._exercise_description)
125-
if exercise_title is None:
126-
if exercise_key is None:
127-
self._exercise_title = None
128-
self._exercise_title_html = None
137+
self._description_html = HTMLMath(self._description)
138+
if title is None:
139+
if key is None:
140+
self._title = None
141+
self._title_html = None
129142
else:
130-
self._exercise_title = exercise_key
131-
self._exercise_title_html = HTML(f"<b>{exercise_key}</b>")
143+
self._title = key
144+
self._title_html = HTML(f"<b>{key}</b>")
132145
else:
133-
self._exercise_title = exercise_title
134-
self._exercise_title_html = HTML(f"<b>{exercise_title}</b>")
146+
self._title = title
147+
self._title_html = HTML(f"<b>{title}</b>")
135148

136-
if self._exercise_description_html is not None:
137-
self._exercise_description_html.add_class("exercise-description")
138-
if self._exercise_title_html is not None:
139-
self._exercise_title_html.add_class("exercise-title")
149+
if self._description_html is not None:
150+
self._description_html.add_class("exercise-description")
151+
if self._title_html is not None:
152+
self._title_html.add_class("exercise-title")
140153

141154
# verify if input argument `parameter` is valid
142155
if parameters is not None:
@@ -176,10 +189,10 @@ def __init__(
176189
"code and parameters do no match: " + compatibility_result
177190
)
178191

179-
name = kwargs.get("name", exercise_key)
192+
name = kwargs.get("name", key)
180193
CheckableWidget.__init__(self, check_registry, name)
181194
if exercise_registry is not None:
182-
ExerciseWidget.__init__(self, exercise_registry, exercise_key)
195+
ExerciseWidget.__init__(self, exercise_registry, key)
183196
else:
184197
# otherwise ExerciseWidget constructor will raise an error
185198
ExerciseWidget.__init__(self, None, None)
@@ -464,10 +477,10 @@ def __init__(
464477
)
465478

466479
demo_children = [CssStyle()]
467-
if self._exercise_title_html is not None:
468-
demo_children.append(self._exercise_title_html)
469-
if self._exercise_description_html is not None:
470-
demo_children.append(self._exercise_description_html)
480+
if self._title_html is not None:
481+
demo_children.append(self._title_html)
482+
if self._description_html is not None:
483+
demo_children.append(self._description_html)
471484

472485
if self._cue_code is not None:
473486
demo_children.append(self._cue_code)
@@ -584,12 +597,12 @@ def parameters(self) -> Dict[str, Check.FunInParamT]:
584597
)
585598

586599
@property
587-
def exercise_title(self) -> Union[str, None]:
588-
return self._exercise_title
600+
def title(self) -> Union[str, None]:
601+
return self._title
589602

590603
@property
591-
def exercise_description(self) -> Union[str, None]:
592-
return self._exercise_description
604+
def description(self) -> Union[str, None]:
605+
return self._description
593606

594607
def _on_trait_parameters_changed(self, change: dict):
595608
self.run_update()

src/scwidgets/exercise/_widget_text_exercise.py

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,47 @@ class TextExercise(VBox, ExerciseWidget):
1414
a custom textarea with custom styling, if not specified the standard parameters
1515
are given.
1616
17-
the (keyword) arguments are passed to VBox
17+
:param key:
18+
The key that is used to store the exercise in the json file.
19+
20+
:param description:
21+
A string describing the exercises that will be put into an HTML widget
22+
above the exercise.
23+
24+
:param title:
25+
A title for the exercise. If not given the key is used.
1826
"""
1927

2028
def __init__(
2129
self,
2230
value: Optional[str] = None,
23-
exercise_key: Optional[str] = None,
31+
key: Optional[str] = None,
2432
exercise_registry: Optional[ExerciseRegistry] = None,
25-
exercise_description: Optional[str] = None,
26-
exercise_title: Optional[str] = None,
33+
description: Optional[str] = None,
34+
title: Optional[str] = None,
2735
*args,
2836
**kwargs,
2937
):
30-
self._exercise_description = exercise_description
31-
if exercise_description is None:
32-
self._exercise_description_html = None
38+
self._description = description
39+
if description is None:
40+
self._description_html = None
3341
else:
34-
self._exercise_description_html = HTMLMath(self._exercise_description)
35-
if exercise_title is None:
36-
if exercise_key is None:
37-
self._exercise_title = None
38-
self._exercise_title_html = None
42+
self._description_html = HTMLMath(self._description)
43+
if title is None:
44+
if key is None:
45+
self._title = None
46+
self._title_html = None
3947
else:
40-
self._exercise_title = exercise_key
41-
self._exercise_title_html = HTML(f"<b>{exercise_key}</b>")
48+
self._title = key
49+
self._title_html = HTML(f"<b>{key}</b>")
4250
else:
43-
self._exercise_title = exercise_title
44-
self._exercise_title_html = HTML(f"<b>{exercise_title}</b>")
51+
self._title = title
52+
self._title_html = HTML(f"<b>{title}</b>")
4553

46-
if self._exercise_description_html is not None:
47-
self._exercise_description_html.add_class("exercise-description")
48-
if self._exercise_title_html is not None:
49-
self._exercise_title_html.add_class("exercise-title")
54+
if self._description_html is not None:
55+
self._description_html.add_class("exercise-description")
56+
if self._title_html is not None:
57+
self._title_html.add_class("exercise-title")
5058

5159
layout = kwargs.pop("layout", Layout(width="auto", height="150px"))
5260
self._textarea = Textarea(value, *args, layout=layout, **kwargs)
@@ -98,13 +106,13 @@ def __init__(
98106
layout=Layout(justify_content="flex-end"),
99107
)
100108

101-
ExerciseWidget.__init__(self, exercise_registry, exercise_key)
109+
ExerciseWidget.__init__(self, exercise_registry, key)
102110

103111
widget_children = [CssStyle()]
104-
if self._exercise_title_html is not None:
105-
widget_children.append(self._exercise_title_html)
106-
if self._exercise_description_html is not None:
107-
widget_children.append(self._exercise_description_html)
112+
if self._title_html is not None:
113+
widget_children.append(self._title_html)
114+
if self._description_html is not None:
115+
widget_children.append(self._description_html)
108116
widget_children.append(self._cue_textarea)
109117
if self._button_panel:
110118
widget_children.append(self._button_panel)
@@ -117,12 +125,12 @@ def __init__(
117125
)
118126

119127
@property
120-
def exercise_title(self) -> Union[str, None]:
121-
return self._exercise_title
128+
def title(self) -> Union[str, None]:
129+
return self._title
122130

123131
@property
124-
def exercise_description(self) -> Union[str, None]:
125-
return self._exercise_description
132+
def description(self) -> Union[str, None]:
133+
return self._description
126134

127135
@property
128136
def answer(self) -> dict:

tests/notebooks/widget_answers.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
# -------
3737
# Test if TextExercise shows correct output
3838

39-
text_exercise = TextExercise(
40-
exercise_registry=exercise_registry, exercise_key="exercise_1"
41-
)
39+
text_exercise = TextExercise(exercise_registry=exercise_registry, key="exercise_1")
4240
text_exercise
4341

4442
# Test 3:
@@ -56,7 +54,7 @@ def foo(x):
5654
parameters={"x": (0, 2, 1)},
5755
update_mode="manual",
5856
exercise_registry=exercise_registry,
59-
exercise_key="exercise_2",
57+
key="exercise_2",
6058
)
6159
code_ex
6260
# -

tests/test_code.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ def print_success(code_ex: CodeExercise | None):
359359
code=function,
360360
parameters={"parameter": fixed(5)},
361361
exercise_registry=exercise_registry,
362-
exercise_key="test_save_registry_ex",
362+
key="test_save_registry_ex",
363363
outputs=[cue_output],
364364
update=print_success,
365365
)
@@ -431,15 +431,15 @@ def test_consrtuction_with_registries(self):
431431
`CheckRegistry` we need to ensure the `CodeExercise` can be run with
432432
each individual one and both"""
433433
CodeExercise(
434-
exercise_key="some_key",
434+
key="some_key",
435435
check_registry=CheckRegistry(),
436436
)
437437
CodeExercise(
438-
exercise_key="some_key",
438+
key="some_key",
439439
exercise_registry=ExerciseRegistry(),
440440
)
441441
CodeExercise(
442-
exercise_key="some_key",
442+
key="some_key",
443443
check_registry=CheckRegistry(),
444444
exercise_registry=ExerciseRegistry(),
445445
)

0 commit comments

Comments
 (0)