From b347fe0b38dc09ec4aa35b93de6ec2f5afcb60bb Mon Sep 17 00:00:00 2001 From: Jan-Lukas Wynen Date: Thu, 31 Aug 2023 13:33:54 +0200 Subject: [PATCH 1/4] Explicitly import scheduler --- src/sciline/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sciline/__init__.py b/src/sciline/__init__.py index eb74611c..13f6084f 100644 --- a/src/sciline/__init__.py +++ b/src/sciline/__init__.py @@ -18,6 +18,7 @@ UnsatisfiedRequirement, ) from .series import Series +from . import scheduler __all__ = [ "AmbiguousProvider", @@ -27,4 +28,5 @@ "Scope", "UnboundTypeVar", "UnsatisfiedRequirement", + "scheduler", ] From 346d137e04b4a08d61f57f6aaa6d4a6cb39b5f26 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Wynen Date: Thu, 31 Aug 2023 17:17:05 +0200 Subject: [PATCH 2/4] Add Pipeline.bin_and_call --- src/sciline/pipeline.py | 55 ++++++++++++++++++ tests/pipeline_test.py | 123 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) diff --git a/src/sciline/pipeline.py b/src/sciline/pipeline.py index e4a20834..cbdf64c5 100644 --- a/src/sciline/pipeline.py +++ b/src/sciline/pipeline.py @@ -3,6 +3,7 @@ from __future__ import annotations from collections import defaultdict +from itertools import chain from typing import ( Any, Callable, @@ -632,3 +633,57 @@ def get( else: graph = self.build(keys) return TaskGraph(graph=graph, keys=keys, scheduler=scheduler) + + @overload + def bind_and_call(self, fns: Callable[..., T], /) -> T: + ... + + @overload + def bind_and_call(self, fns: Iterable[Callable[..., Any]], /) -> Tuple[Any, ...]: + ... + + def bind_and_call( + self, fns: Union[Callable[..., Any], Iterable[Callable[..., Any]]], / + ) -> Any: + """ + Call the given functions with arguments provided by the pipeline. + + Parameters + ---------- + fns: + Functions to call. + The pipeline will provide all arguments based on the function's type hints. + + If this is a single callable, it is called directly. + Otherwise, ``bind_and_call`` will iterate over it and call all functions. + If will in either case call :meth:`Pipeline.compute` only once. + + Returns + ------- + : + The return values of the functions in the same order as the functions. + If only one function is passed, its return value + is *not* wrapped in a tuple. + """ + return_tuple = True + if callable(fns): + fns = (fns,) + return_tuple = False + + arg_types_per_function = { + fn: { + name: ty for name, ty in get_type_hints(fn).items() if name != 'return' + } + for fn in fns + } + all_arg_types = tuple( + set(chain(*(a.values() for a in arg_types_per_function.values()))) + ) + values_per_type = self.compute(all_arg_types) + results = tuple( + fn(**{name: values_per_type[ty] for name, ty in arg_types.items()}) + for fn, arg_types in arg_types_per_function.items() + ) + if not return_tuple: + return results[0] + return results diff --git a/tests/pipeline_test.py b/tests/pipeline_test.py index 7145a39e..b37de872 100644 --- a/tests/pipeline_test.py +++ b/tests/pipeline_test.py @@ -619,3 +619,126 @@ def test_get_with_NaiveScheduler() -> None: pipeline = sl.Pipeline([int_to_float, make_int]) task = pipeline.get(float, scheduler=sl.scheduler.NaiveScheduler()) assert task.compute() == 1.5 + + +def test_bind_and_call_no_function() -> None: + pipeline = sl.Pipeline([make_int]) + assert pipeline.bind_and_call(()) == () + + +def test_bind_and_call_function_without_args() -> None: + def func() -> str: + return "func" + + pipeline = sl.Pipeline([make_int]) + assert pipeline.bind_and_call(func) == "func" + + +def test_bind_and_call_function_with_1_arg() -> None: + def func(i: int) -> int: + return i * 2 + + pipeline = sl.Pipeline([make_int]) + assert pipeline.bind_and_call(func) == 6 + + +def test_bind_and_call_function_with_2_arg2() -> None: + def func(i: int, f: float) -> float: + return i + f + + pipeline = sl.Pipeline([make_int, int_to_float]) + assert pipeline.bind_and_call(func) == 4.5 + + +def test_bind_and_call_function_in_iterator() -> None: + def func(i: int) -> int: + return i * 2 + + pipeline = sl.Pipeline([make_int]) + assert pipeline.bind_and_call(iter((func,))) == (6,) + + +def test_bind_and_call_dataclass_without_args() -> None: + @dataclass + class C: + ... + + pipeline = sl.Pipeline([make_int]) + assert pipeline.bind_and_call(C) == C() + + +def test_bind_and_call_dataclass_with_1_arg() -> None: + @dataclass + class C: + i: int + + pipeline = sl.Pipeline([make_int]) + assert pipeline.bind_and_call(C) == C(i=3) + + +def test_bind_and_call_dataclass_with_2_arg2() -> None: + @dataclass + class C: + i: int + f: float + + pipeline = sl.Pipeline([make_int, int_to_float]) + assert pipeline.bind_and_call(C) == C(i=3, f=1.5) + + +def test_bind_and_call_two_functions() -> None: + def func1(i: int) -> int: + return 2 * i + + def func2(f: float) -> float: + return f + 1 + + pipeline = sl.Pipeline([make_int, int_to_float]) + assert pipeline.bind_and_call((func1, func2)) == (6, 2.5) + + +def test_bind_and_call_two_functions_in_iterator() -> None: + def func1(i: int) -> int: + return 2 * i + + def func2(f: float) -> float: + return f + 1 + + pipeline = sl.Pipeline([make_int, int_to_float]) + assert pipeline.bind_and_call(iter((func1, func2))) == (6, 2.5) + + +def test_bind_and_call_function_and_dataclass() -> None: + def func(i: int) -> int: + return 2 * i + + @dataclass + class C: + i: int + f: float + + pipeline = sl.Pipeline([make_int, int_to_float]) + assert pipeline.bind_and_call((func, C)) == (6, C(i=3, f=1.5)) + + +def test_bind_and_call_function_without_return_annotation() -> None: + def func(i: int): # type: ignore[no-untyped-def] + return 2 * i + + pipeline = sl.Pipeline([make_int]) + assert pipeline.bind_and_call(func) == 6 + + +def test_bind_and_call_generic_function() -> None: + T = TypeVar('T') + A = NewType('A', int) + B = NewType('B', int) + + class G(sl.Scope[T, int], int): + ... + + def func(a: G[A]) -> int: + return -4 * a + + pipeline = sl.Pipeline([], params={G[A]: 3, G[B]: 4}) + assert pipeline.bind_and_call(func) == -12 From e67d0e40b392c240625bab4d0e01b8c93b5de251 Mon Sep 17 00:00:00 2001 From: jl-wynen Date: Thu, 31 Aug 2023 15:18:54 +0000 Subject: [PATCH 3/4] Apply automatic formatting --- src/sciline/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sciline/__init__.py b/src/sciline/__init__.py index 13f6084f..a53e70af 100644 --- a/src/sciline/__init__.py +++ b/src/sciline/__init__.py @@ -9,6 +9,7 @@ except importlib.metadata.PackageNotFoundError: __version__ = "0.0.0" +from . import scheduler from .domain import Scope from .param_table import ParamTable from .pipeline import ( @@ -18,7 +19,6 @@ UnsatisfiedRequirement, ) from .series import Series -from . import scheduler __all__ = [ "AmbiguousProvider", From effd1afe9b8b1b1b89d2ae05fdd903b9dc94adae Mon Sep 17 00:00:00 2001 From: Jan-Lukas Wynen Date: Fri, 1 Sep 2023 08:56:18 +0200 Subject: [PATCH 4/4] Add test for default args --- tests/pipeline_test.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/pipeline_test.py b/tests/pipeline_test.py index b37de872..278be7cb 100644 --- a/tests/pipeline_test.py +++ b/tests/pipeline_test.py @@ -650,6 +650,14 @@ def func(i: int, f: float) -> float: assert pipeline.bind_and_call(func) == 4.5 +def test_bind_and_call_overrides_default_args() -> None: + def func(i: int, f: float = -0.5) -> float: + return i + f + + pipeline = sl.Pipeline([make_int, int_to_float]) + assert pipeline.bind_and_call(func) == 4.5 + + def test_bind_and_call_function_in_iterator() -> None: def func(i: int) -> int: return i * 2