-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
664dd84
commit 43ebf35
Showing
125 changed files
with
1,786 additions
and
22,718 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
- repo: https://github.com/astral-sh/ruff-pre-commit | ||
# Ruff version. | ||
rev: v0.5.1 | ||
hooks: | ||
# Run the linter. | ||
- id: ruff | ||
args: [ --fix ] | ||
# Run the formatter. | ||
- id: ruff-format |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Callable, Iterable | ||
from dataclasses import dataclass | ||
from typing import Generic, 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
Oops, something went wrong.