|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from typing import Callable, Generic, Iterator, Optional, Tuple, TypeVar |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import ( |
| 5 | + Any, |
| 6 | + Callable, |
| 7 | + Dict, |
| 8 | + Generic, |
| 9 | + Iterator, |
| 10 | + List, |
| 11 | + Optional, |
| 12 | + Tuple, |
| 13 | + TypeVar, |
| 14 | + Union, |
| 15 | + cast, |
| 16 | + overload, |
| 17 | +) |
4 | 18 |
|
5 | 19 | from pipedata.core.itertools import take_next, take_up_to_n |
6 | 20 |
|
7 | 21 | TStart = TypeVar("TStart") |
8 | 22 | TEnd = TypeVar("TEnd") |
9 | | -TNewEnd = TypeVar("TNewEnd") |
| 23 | +TOther = TypeVar("TOther") |
10 | 24 |
|
11 | 25 |
|
12 | 26 | def _identity(input_iterator: Iterator[TEnd]) -> Iterator[TEnd]: |
13 | | - while element := take_next(input_iterator): |
| 27 | + while (element := take_next(input_iterator)) is not None: |
14 | 28 | yield element |
15 | 29 |
|
16 | 30 |
|
17 | | -class Chain(Generic[TStart, TEnd]): |
18 | | - def __init__(self, action: Callable[[Iterator[TStart]], Iterator[TEnd]]) -> None: |
19 | | - self._action = action |
| 31 | +class CountingIterator(Iterator[TStart]): |
| 32 | + def __init__(self, iterator: Iterator[TStart]) -> None: |
| 33 | + self._iterator = iterator |
| 34 | + self._count = 0 |
| 35 | + |
| 36 | + def __iter__(self) -> Iterator[TStart]: |
| 37 | + return self |
| 38 | + |
| 39 | + def __next__(self) -> TStart: |
| 40 | + self._count += 1 |
| 41 | + try: |
| 42 | + return next(self._iterator) |
| 43 | + except StopIteration as err: |
| 44 | + self._count -= 1 |
| 45 | + raise StopIteration from err |
| 46 | + |
| 47 | + def get_count(self) -> int: |
| 48 | + return self._count |
| 49 | + |
| 50 | + |
| 51 | +class CountedFunc(Generic[TStart, TEnd]): |
| 52 | + def __init__( |
| 53 | + self, |
| 54 | + func: Callable[[Iterator[TStart]], Iterator[TEnd]], |
| 55 | + ) -> None: |
| 56 | + self._func = func |
| 57 | + self._counting_input: Optional[CountingIterator[TStart]] = None |
| 58 | + self._counting_output: Optional[CountingIterator[TEnd]] = None |
| 59 | + |
| 60 | + @property |
| 61 | + def __name__(self) -> str: # noqa: A003 |
| 62 | + return self._func.__name__ |
| 63 | + |
| 64 | + def __call__(self, input_iterator: Iterator[TStart]) -> Iterator[TEnd]: |
| 65 | + self._counting_input = CountingIterator(input_iterator) |
| 66 | + self._counting_output = CountingIterator(self._func(self._counting_input)) |
| 67 | + return self._counting_output |
20 | 68 |
|
21 | | - @classmethod |
22 | | - def start(cls) -> Chain[TEnd, TEnd]: |
23 | | - return Chain(_identity) |
| 69 | + def get_counts(self) -> Tuple[int, int]: |
| 70 | + return ( |
| 71 | + 0 if self._counting_input is None else self._counting_input.get_count(), |
| 72 | + 0 if self._counting_output is None else self._counting_output.get_count(), |
| 73 | + ) |
24 | 74 |
|
25 | | - def __call__(self, previous_step: Iterator[TStart]) -> Iterator[TEnd]: |
26 | | - return self._action(previous_step) |
| 75 | + |
| 76 | +@dataclass |
| 77 | +class StepCount: |
| 78 | + name: str |
| 79 | + inputs: int |
| 80 | + outputs: int |
| 81 | + |
| 82 | + |
| 83 | +class Chain(Generic[TStart, TEnd]): |
| 84 | + @overload |
| 85 | + def __init__( |
| 86 | + self, |
| 87 | + previous_steps: Chain[TStart, TOther], |
| 88 | + func: Callable[[Iterator[TOther]], Iterator[TEnd]], |
| 89 | + ): |
| 90 | + ... |
| 91 | + |
| 92 | + @overload |
| 93 | + def __init__( |
| 94 | + self, |
| 95 | + previous_steps: None, |
| 96 | + func: Callable[[Iterator[TStart]], Iterator[TEnd]], |
| 97 | + ): |
| 98 | + ... |
| 99 | + |
| 100 | + def __init__( |
| 101 | + self, |
| 102 | + previous_steps: Optional[Chain[TStart, TOther]], |
| 103 | + func: Union[ |
| 104 | + Callable[[Iterator[TOther]], Iterator[TEnd]], |
| 105 | + Callable[[Iterator[TStart]], Iterator[TEnd]], |
| 106 | + ], |
| 107 | + ) -> None: |
| 108 | + self._previous_steps = previous_steps |
| 109 | + self._func = CountedFunc(func) |
| 110 | + |
| 111 | + def __call__(self, input_iterator: Iterator[TStart]) -> Iterator[TEnd]: |
| 112 | + if self._previous_steps is None: |
| 113 | + func = cast(CountedFunc[TStart, TEnd], self._func) |
| 114 | + return func(input_iterator) |
| 115 | + |
| 116 | + return self._func(self._previous_steps(input_iterator)) # type: ignore |
27 | 117 |
|
28 | 118 | def flat_map( |
29 | | - self, func: Callable[[Iterator[TEnd]], Iterator[TNewEnd]] |
30 | | - ) -> Chain[TStart, TNewEnd]: |
| 119 | + self, func: Callable[[Iterator[TEnd]], Iterator[TOther]] |
| 120 | + ) -> Chain[TStart, TOther]: |
31 | 121 | """ |
32 | 122 | Output zero or more elements from one or more input elements. |
33 | 123 |
|
34 | 124 | This is a fully general operation, that can arbitrarily transform the |
35 | 125 | stream of elements. It is the most powerful operation, and all the |
36 | 126 | other operations are implemented in terms of it. |
37 | 127 | """ |
38 | | - |
39 | | - def new_action(previous_step: Iterator[TStart]) -> Iterator[TNewEnd]: |
40 | | - return func(self._action(previous_step)) |
41 | | - |
42 | | - return Chain(new_action) |
| 128 | + return Chain(self, func) |
43 | 129 |
|
44 | 130 | def filter(self, func: Callable[[TEnd], bool]) -> Chain[TStart, TEnd]: # noqa: A003 |
45 | 131 | """ |
46 | 132 | Remove elements from the stream that do not pass the filter function. |
47 | 133 | """ |
48 | 134 |
|
49 | 135 | def new_action(previous_step: Iterator[TEnd]) -> Iterator[TEnd]: |
50 | | - while element := take_next(previous_step): |
| 136 | + while (element := take_next(previous_step)) is not None: |
51 | 137 | if func(element) is True: |
52 | 138 | yield element |
53 | 139 |
|
| 140 | + new_action.__name__ = func.__name__ |
54 | 141 | return self.flat_map(new_action) |
55 | 142 |
|
56 | 143 | def map( # noqa: A003 |
57 | | - self, func: Callable[[TEnd], TNewEnd] |
58 | | - ) -> Chain[TStart, TNewEnd]: |
| 144 | + self, func: Callable[[TEnd], TOther] |
| 145 | + ) -> Chain[TStart, TOther]: |
59 | 146 | """ |
60 | 147 | Return a single transformed element from each input element. |
61 | 148 | """ |
62 | 149 |
|
63 | | - def new_action(previous_step: Iterator[TEnd]) -> Iterator[TNewEnd]: |
64 | | - while element := take_next(previous_step): |
| 150 | + def new_action(previous_step: Iterator[TEnd]) -> Iterator[TOther]: |
| 151 | + while (element := take_next(previous_step)) is not None: |
65 | 152 | yield func(element) |
66 | 153 |
|
| 154 | + new_action.__name__ = func.__name__ |
67 | 155 | return self.flat_map(new_action) |
68 | 156 |
|
69 | 157 | def map_tuple( |
70 | | - self, func: Callable[[Tuple[TEnd, ...]], TNewEnd], n: Optional[int] = None |
71 | | - ) -> Chain[TStart, TNewEnd]: |
| 158 | + self, func: Callable[[Tuple[TEnd, ...]], TOther], n: Optional[int] = None |
| 159 | + ) -> Chain[TStart, TOther]: |
72 | 160 | """ |
73 | 161 | Return a single transformed element from (up to) n input elements. |
74 | 162 |
|
75 | 163 | If n is None, then apply the function to all the elements, and return |
76 | 164 | an iterator of 1 element. |
77 | 165 | """ |
78 | 166 |
|
79 | | - def new_action(previous_step: Iterator[TEnd]) -> Iterator[TNewEnd]: |
| 167 | + def new_action(previous_step: Iterator[TEnd]) -> Iterator[TOther]: |
80 | 168 | while elements := take_up_to_n(previous_step, n): |
81 | 169 | yield func(elements) |
82 | 170 |
|
| 171 | + new_action.__name__ = func.__name__ |
83 | 172 | return self.flat_map(new_action) |
| 173 | + |
| 174 | + def get_counts(self) -> List[Dict[str, Any]]: |
| 175 | + step_counts = [] |
| 176 | + if self._previous_steps is not None: |
| 177 | + step_counts = self._previous_steps.get_counts() |
| 178 | + |
| 179 | + inputs, outputs = self._func.get_counts() |
| 180 | + step_counts.append( |
| 181 | + { |
| 182 | + "name": self._func.__name__, |
| 183 | + "inputs": inputs, |
| 184 | + "outputs": outputs, |
| 185 | + } |
| 186 | + ) |
| 187 | + return step_counts |
| 188 | + |
| 189 | + |
| 190 | +class ChainStart(Chain[TOther, TOther]): |
| 191 | + def __init__(self) -> None: |
| 192 | + super().__init__(None, _identity) |
0 commit comments