Skip to content

Commit

Permalink
feat: PythonOperator __call__ can run callable without output
Browse files Browse the repository at this point in the history
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
  • Loading branch information
YeungOnion committed Mar 22, 2024
1 parent 27a6336 commit 4a9a74d
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/fasthep_flow/operators/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<string>', 'eval'),
globals(),
locals())
exec(compile('self.callable = ' + callable_str, '<string>', '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

0 comments on commit 4a9a74d

Please sign in to comment.