-
-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add broadcasts for sequences (#2971)
* start of allowing generators to broadcast length data * allow output lengths to be set * Fixed typing and broadcasting issue for sequence types (#2972) * Fixed typing and broadcasting issue for sequence types * Fixed errors * Sequence item types for Load Video (#2974) * Sequence item types for Load Video * Ignore type error * Don't cache individually run generators and delete any caches on new run --------- Co-authored-by: Michael Schmidt <mitchi5000.ms@googlemail.com>
- Loading branch information
1 parent
fb7b8da
commit a2135a0
Showing
21 changed files
with
436 additions
and
200 deletions.
There are no files selected for viewing
This file contains 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 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 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,72 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import Callable, Generic, Iterable, TypeVar | ||
|
||
I = TypeVar("I") | ||
L = TypeVar("L") | ||
|
||
|
||
@dataclass | ||
class Generator(Generic[I]): | ||
supplier: Callable[[], Iterable[I | Exception]] | ||
expected_length: int | ||
fail_fast: bool = True | ||
metadata: object | None = None | ||
|
||
def with_fail_fast(self, fail_fast: bool): | ||
self.fail_fast = fail_fast | ||
return self | ||
|
||
def with_metadata(self, metadata: object): | ||
self.metadata = metadata | ||
return self | ||
|
||
@staticmethod | ||
def from_iter( | ||
supplier: Callable[[], Iterable[I | Exception]], expected_length: int | ||
) -> Generator[I]: | ||
return Generator(supplier, expected_length) | ||
|
||
@staticmethod | ||
def from_list(l: list[L], map_fn: Callable[[L, int], I]) -> Generator[I]: | ||
""" | ||
Creates a new generator from a list that is mapped using the given | ||
function. The iterable will be equivalent to `map(map_fn, l)`. | ||
""" | ||
|
||
def supplier(): | ||
for i, x in enumerate(l): | ||
try: | ||
yield map_fn(x, i) | ||
except Exception as e: | ||
yield e | ||
|
||
return Generator(supplier, len(l)) | ||
|
||
@staticmethod | ||
def from_range(count: int, map_fn: Callable[[int], I]) -> Generator[I]: | ||
""" | ||
Creates a new generator the given number of items where each item is | ||
lazily evaluated. The iterable will be equivalent to `map(map_fn, range(count))`. | ||
""" | ||
assert count >= 0 | ||
|
||
def supplier(): | ||
for i in range(count): | ||
try: | ||
yield map_fn(i) | ||
except Exception as e: | ||
yield e | ||
|
||
return Generator(supplier, count) | ||
|
||
|
||
N = TypeVar("N") | ||
R = TypeVar("R") | ||
|
||
|
||
@dataclass | ||
class Collector(Generic[N, R]): | ||
on_iterate: Callable[[N], None] | ||
on_complete: Callable[[], R] |
This file contains 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 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 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 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.