From 4a9a74dd9fa6f9494c636021e22919405f2a8a95 Mon Sep 17 00:00:00 2001 From: Orion Yeung <11580988+orionyeung001@users.noreply.github.com> Date: Fri, 22 Mar 2024 12:26:56 -0500 Subject: [PATCH] feat: PythonOperator `__call__` can run callable without output partial implementation of `__call__` to discuss: - what are outputs of calling `self.callable: Callable[Any, Any]`? - typing provides _very_ little constraint - callable could have various types of useful logging side effects - level of laziness, likely beyond core --- src/fasthep_flow/operators/python.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/fasthep_flow/operators/python.py b/src/fasthep_flow/operators/python.py index 6e949ae..92d4a1c 100644 --- a/src/fasthep_flow/operators/python.py +++ b/src/fasthep_flow/operators/python.py @@ -31,26 +31,33 @@ def configure(self, **kwargs: Any) -> None: if isinstance(self.callable, str): obj = None + callable_str = self.callable # breakpoint() try: - obj = eval(compile(self.callable, '', 'eval'), - globals(), - locals()) + exec(compile('self.callable = ' + callable_str, '', 'exec'), + globals(), + locals()) + obj = self.callable except (SyntaxError, AttributeError) as e: raise e if not callable(obj): - msg = f"provided string `{self.callable}` did not compile to a callable" + self.callable = None + msg = f"provided string `{callable_str}` did not compile to a callable" raise AttributeError(msg) - self.callable = obj return def __call__(self, **kwargs: Any) -> dict[str, Any]: + args = ast.literal_eval("(" + ",".join(self.arguments) + ",)") + breakpoint() + out = self.callable(*args) stdout, stderr, exit_code = "", "", 0 + return {"stdout": stdout, "stderr": stderr, "exit_code": exit_code} def __repr__(self) -> str: - return f"LocalPythonOperator(callable={self.python_callable}, arguments={self.arguments})" + return f"LocalPythonOperator(callable={self.callable}, arguments={self.arguments})" + PythonOperator = LocalPythonOperator