From ca4233be237e98f37e6f311cff4df0597f940d0e Mon Sep 17 00:00:00 2001 From: Austin Cory Bart Date: Sat, 16 Mar 2024 13:39:08 -0400 Subject: [PATCH] Provide get_function command --- change_log.txt | 2 +- pedal/sandbox/commands.py | 9 +++++++++ pedal/sandbox/sandbox.py | 8 ++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/change_log.txt b/change_log.txt index db658e7..35df6fe 100644 --- a/change_log.txt +++ b/change_log.txt @@ -3,7 +3,7 @@ Version 2.6.0 (3/16/2024) * Allow unit_test to be used as a context manager * Various documentation improvements * Improved table output on terminal -* +* Provide Sandbox command get_function to wrap a function with call Version 2.5.9 (8/21/2023) ------------------------- diff --git a/pedal/sandbox/commands.py b/pedal/sandbox/commands.py index d8e021d..ee285ce 100644 --- a/pedal/sandbox/commands.py +++ b/pedal/sandbox/commands.py @@ -471,6 +471,15 @@ def get_module(module_name, report=MAIN_REPORT): raise ValueError(f"Unknown Sandbox Module: `{module_name}`") +def get_function(function_name, report=MAIN_REPORT): + """ + Creates an executable function from the given name, based on the + students' namespace. This will be executed using the call method. + """ + sandbox: Sandbox = report[TOOL_NAME]['sandbox'] + return sandbox.get_function(function_name) + + def get_python_errors(report=MAIN_REPORT): """ Retrieves any runtime or syntax errors from the report. diff --git a/pedal/sandbox/sandbox.py b/pedal/sandbox/sandbox.py index 57c62d5..8460b2d 100644 --- a/pedal/sandbox/sandbox.py +++ b/pedal/sandbox/sandbox.py @@ -853,6 +853,14 @@ def var(self): """ return {k: SandboxVariable(k, v) for k, v in self.data.items()} + def get_function(self, by_name): + """ + Creates an executable function from the given name, based + on the student's namespace. This will be executed using the call + method. + """ + return lambda *args, **kwargs: self.call(by_name, *args, **kwargs) + def __getitem__(self, item): value, exception = None, None try: