-
-
Notifications
You must be signed in to change notification settings - Fork 3
Add decorator and output syntax for executing code on runtimes #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| # Copyright (c) 2023-2025 Datalayer, Inc. | ||
| # Distributed under the terms of the Modified BSD License. | ||
|
|
||
| import functools | ||
| import inspect | ||
| from typing import Any, Callable, Optional, Union | ||
|
|
||
| from datalayer_core.sdk.datalayer import DatalayerClient | ||
|
|
||
| # TODO: | ||
| # - inputs are different from args and kwargs (rename) | ||
| # - inputs cannot be kewyword args of the function | ||
| # - incorrect number of args | ||
|
|
||
|
|
||
| def datalayer( | ||
| runtime_name: Union[Callable[..., Any], Optional[str]] = None, | ||
| inputs: Optional[list[str]] = None, | ||
| output: Optional[str] = None, | ||
| snapshot_name: Optional[str] = None, | ||
| ) -> Any: | ||
| """ | ||
| Decorator to execute a function in a Datalayer runtime. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| runtime_name : str, optional | ||
| The name of the runtime to use. If not provided, a default runtime will be used. | ||
| inputs : list[str], optional | ||
| A list of input variable names for the function. | ||
| output : str, optional | ||
| The name of the output variable for the function | ||
| snapshot_name : str, optional | ||
| The name of the runtime snapshot to use | ||
|
|
||
| Returns | ||
| ------- | ||
| Callable[..., Any] | ||
| A decorator that wraps the function to be executed in a Datalayer runtime. | ||
|
|
||
| Examples | ||
| -------- | ||
|
|
||
| >>> from datalayer_core.sdk.decorators import datalayer | ||
| >>> @datalayer | ||
| ... def example(x: float, y: float) -> float: | ||
| ... return x + y | ||
|
|
||
| >>> from datalayer_core.sdk.decorators import datalayer | ||
| >>> @datalayer(runtime_name="example-runtime", inputs=["x", "y"], output="z") | ||
| ... def example(x: float, y: float) -> float: | ||
| ... return x + y | ||
| """ | ||
| variables = {} | ||
| inputs_decorated = inputs | ||
| output_decorated = output | ||
| snapshot_name_decorated = snapshot_name | ||
|
|
||
| if callable(runtime_name): | ||
| runtime_name_decorated = None | ||
| else: | ||
| runtime_name_decorated = runtime_name | ||
|
|
||
| def decorator(func: Callable[..., Any]) -> Callable[..., Any]: | ||
| if output_decorated is None: | ||
| output = f"DATALAYER_RUNTIME_OUTPUT_{func.__name__}".upper() | ||
|
|
||
| sig = inspect.signature(func) | ||
| if inputs_decorated is None: | ||
| inputs = [] | ||
| for name, _param in sig.parameters.items(): | ||
| inputs.append(name) | ||
| variables[name] = ( | ||
| _param.default | ||
| if _param.default is not inspect.Parameter.empty | ||
| else None | ||
| ) | ||
| else: | ||
| if len(sig.parameters) != len(inputs_decorated): | ||
| raise ValueError( | ||
| f"Function {func.__name__} has {len(sig.parameters)} parameters, " | ||
| f"but {len(inputs_decorated)} inputs were provided." | ||
| ) | ||
|
|
||
| @functools.wraps(func) | ||
| def wrapper(*args: Any, **kwargs: Any) -> Any: | ||
| sig = inspect.signature(func) | ||
| mapping = {} | ||
| for idx, (name, _param) in enumerate(sig.parameters.items()): | ||
| mapping[name] = (inputs_decorated or inputs)[idx] | ||
|
|
||
| for kwarg, kwarg_value in kwargs.items(): | ||
| variables[mapping[kwarg]] = kwarg_value | ||
|
|
||
| for idx, (arg_value) in enumerate(args): | ||
| kwarg = (inputs_decorated or inputs)[idx] | ||
| variables[kwarg] = arg_value | ||
|
|
||
| function_call = ( | ||
| f"{output_decorated or output} = {func.__name__}(" | ||
| + ", ".join(inputs_decorated or inputs) | ||
| + ")" | ||
| ) | ||
|
|
||
| start = 0 | ||
| func_source_lines = inspect.getsource(func).split("\n") | ||
| for start, line in enumerate(func_source_lines): | ||
| if line.startswith("def "): | ||
| break | ||
| function_source = "\n".join(func_source_lines[start:]) | ||
|
|
||
| # print("inputs", inputs_decorated or inputs) | ||
| # print("variables", variables) | ||
| # print([function_source]) | ||
| # print([function_call]) | ||
|
|
||
| client = DatalayerClient() | ||
| with client.create_runtime( | ||
| name=runtime_name_decorated, snapshot_name=snapshot_name_decorated | ||
| ) as runtime: | ||
| runtime.execute(function_source) | ||
| return runtime.execute( | ||
| function_call, | ||
| variables=variables, | ||
| output=output_decorated or output, | ||
| ) | ||
|
|
||
| return wrapper | ||
|
|
||
| # print(f"Using runtime: {runtime_name}, inputs: {inputs}, output: {output}") | ||
| if callable(runtime_name): | ||
| return decorator(runtime_name) | ||
| else: | ||
| return decorator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Copyright (c) 2023-2025 Datalayer, Inc. | ||
| # Distributed under the terms of the Modified BSD License. | ||
|
|
||
| import os | ||
| import time | ||
|
|
||
| import pytest | ||
| from dotenv import load_dotenv | ||
|
|
||
| from datalayer_core.sdk.decorators import datalayer | ||
|
|
||
| load_dotenv() | ||
|
|
||
|
|
||
| DATALAYER_TEST_TOKEN = os.environ.get("DATALAYER_TEST_TOKEN") | ||
|
|
||
|
|
||
| def sum_test(x: float, y: float, z: float = 1) -> float: | ||
| return x + y + z | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "args,expected_output,decorator", | ||
| [ | ||
| ([1, 4.5, 2], 7.5, datalayer), | ||
| ([1, 4.5, 2], 7.5, datalayer(runtime_name="runtime-test")), | ||
| ([1, 4.5, 2], 7.5, datalayer(output="result")), | ||
| ([1, 4.5, 2], 7.5, datalayer(inputs=["a", "b", "c"])), | ||
| ], | ||
| ) | ||
| @pytest.mark.skipif( | ||
| not bool(DATALAYER_TEST_TOKEN), | ||
| reason="DATALAYER_TEST_TOKEN is not set, skipping secret tests.", | ||
| ) | ||
| def test_decorator(args, expected_output, decorator): # type: ignore | ||
| """ | ||
| Test the Datalayer decorator. | ||
| """ | ||
| time.sleep(10) | ||
| func = decorator(sum_test) | ||
| assert func(*args) == expected_output | ||
| time.sleep(10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.