Skip to content

Commit

Permalink
Mirror update button description in public API of CodeExercise
Browse files Browse the repository at this point in the history
To improve the API from a user perspective we use the same naming for
public functions that update a `CodeExercise` instantce as the buttons
as their description. This means renaming `run_update` to `run_code` and
duplicating the functionality of the `run_update` with the function
`update`, since the update button is called "Update" when no `code` is
given on initalization. Since `run_code` was already taken we rename it
tc `call_code`
  • Loading branch information
agoscinski committed Nov 29, 2024
1 parent eb1e3a9 commit 5e0bcd0
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/scwidgets/exercise/_widget_code_exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def run_check(self) -> None:
self._on_click_check_action()

def compute_output_to_check(self, *args, **kwargs) -> Check.FunOutParamsT:
return self.run_code(*args, **kwargs)
return self.call_code(*args, **kwargs)

def handle_checks_result(self, results: List[Union[CheckResult, Exception]]):
self._output.clear_output(wait=True)
Expand Down Expand Up @@ -712,7 +712,7 @@ def _on_click_update_action(self) -> bool:
else:
self._update_func(self) # type: ignore[call-arg]
elif self._code is not None:
self.run_code(**self.params)
self.call_code(**self.params)

for cue_output in self.outputs:
if hasattr(cue_output, "draw_display"):
Expand All @@ -732,7 +732,7 @@ def _on_click_update_action(self) -> bool:

return not (raised_error)

def run_update(self):
def run_code(self):
"""
Invokes an update run, the same that is invoked by a click on the update button
or for :param update_mode: "release" and "continuous" when a parameter panel
Expand All @@ -746,15 +746,22 @@ def run_update(self):
# displayed
self._on_click_update_action()

def run_code(self, *args, **kwargs) -> Check.FunOutParamsT:
def update(self):
"""
Does the same as `run_code`. In case no `code` was provided on
initialization the button name is "Update".
"""
self.run_code()

def call_code(self, *args, **kwargs) -> Check.FunOutParamsT:
"""
Runs the `code` with the given (keyword) arguments and returns the output of the
`code`. If no `code` was given on intialization, then a `ValueError` is raised.
"""
try:
if self._code is None:
raise ValueError(
"run_code was invoked, but no code was given on initializaion"
"call_code was invoked, but no code was given on initializaion"
)
return self._code.run(*args, **kwargs)
except CodeValidationError as e:
Expand Down

0 comments on commit 5e0bcd0

Please sign in to comment.