Skip to content

Commit

Permalink
Make CodeInput naming related to the function object more consistent (
Browse files Browse the repository at this point in the history
#84)

Rename `get_function_object` to `wrapped_function` and
make it property to be more consistent with the `function`.

In `__call__` the wrapped function is now called, this way `__call__`
replaces `run` function that is removed as a consequence.
  • Loading branch information
agoscinski authored Nov 30, 2024
1 parent 202d993 commit 843cbbb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
30 changes: 19 additions & 11 deletions src/scwidgets/code/_widget_code_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import types
import warnings
from functools import wraps
from typing import Any, List, Optional
from typing import List, Optional

from widget_code_input import WidgetCodeInput
from widget_code_input.utils import (
Expand Down Expand Up @@ -68,12 +68,21 @@ def __init__(
@property
def function(self) -> types.FunctionType:
"""
Returns the unwrapped function object
Return the compiled function object.
This can be assigned to a variable and then called, for instance::
func = widget.wrapped_function # This can raise a SyntaxError
retval = func(parameters)
:raise SyntaxError: if the function code has syntax errors (or if
the function name is not a valid identifier)
"""
return inspect.unwrap(self.get_function_object())
return inspect.unwrap(self.wrapped_function)

def __call__(self, *args, **kwargs) -> Any:
return self.function(*args, **kwargs)
def __call__(self, *args, **kwargs) -> Check.FunOutParamsT:
"""Calls the wrapped function"""
return self.wrapped_function(*args, **kwargs)

def compatible_with_signature(self, parameters: List[str]) -> str:
"""
Expand All @@ -95,9 +104,6 @@ def compatible_with_signature(self, parameters: List[str]) -> str:
def function_parameters_name(self) -> List[str]:
return self.function_parameters.replace(",", "").split(" ")

def run(self, *args, **kwargs) -> Check.FunOutParamsT:
return self.get_function_object()(*args, **kwargs)

@staticmethod
def get_code(func: types.FunctionType) -> str:
source_lines, _ = inspect.getsourcelines(func)
Expand Down Expand Up @@ -140,13 +146,15 @@ def get_code(func: types.FunctionType) -> str:

return source

def get_function_object(self):
@property
def wrapped_function(self) -> types.FunctionType:
"""
Return the compiled function object.
Return the compiled function object wrapped by an try-catch block
raising a `CodeValidationError`.
This can be assigned to a variable and then called, for instance::
func = widget.get_function_object() # This can raise a SyntaxError
func = widget.wrapped_function # This can raise a SyntaxError
retval = func(parameters)
:raise SyntaxError: if the function code has syntax errors (or if
Expand Down
2 changes: 1 addition & 1 deletion src/scwidgets/exercise/_widget_code_exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ def run_code(self, *args, **kwargs) -> Check.FunOutParamsT:
raise ValueError(
"run_code was invoked, but no code was given on initializaion"
)
return self._code.run(*args, **kwargs)
return self._code(*args, **kwargs)
except CodeValidationError as e:
raise e
except Exception as e:
Expand Down

0 comments on commit 843cbbb

Please sign in to comment.