Skip to content

Commit a52122d

Browse files
committed
Rename map_tuple to batched_map, more consistent with python stdlib
1 parent 462ee30 commit a52122d

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ result = (
2323
StreamStart(range(10))
2424
.filter(lambda x: x % 2 == 0)
2525
.map(lambda x: x ^ 2)
26-
.map_tuple(lambda x: x, 2)
26+
.batched_map(lambda x: x, 2)
2727
.to_list()
2828
)
2929
print(result)
@@ -40,7 +40,7 @@ chain = (
4040
ChainStart()
4141
.filter(lambda x: x % 2 == 0)
4242
.map(lambda x: x ^ 2)
43-
.map_tuple(lambda x: sum(x), 2)
43+
.batched_map(lambda x: sum(x), 2)
4444
)
4545
print(Stream(range(10), chain).to_list())
4646
#> [2, 10, 10]

src/pipedata/core/chain.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

3-
from itertools import islice
3+
import functools
4+
import itertools
45
from typing import (
56
Any,
67
Callable,
@@ -21,6 +22,12 @@
2122
TOther = TypeVar("TOther")
2223

2324

25+
def _batched(iterable: Iterator[TEnd], n: Optional[int]) -> Iterator[Tuple[TEnd, ...]]:
26+
"""Can be replaced by itertools.batched once using Python 3.12+."""
27+
while (elements := tuple(itertools.islice(iterable, n))) != ():
28+
yield elements
29+
30+
2431
def _identity(input_iterator: Iterator[TEnd]) -> Iterator[TEnd]:
2532
yield from input_iterator
2633

@@ -122,10 +129,10 @@ def filter(self, func: Callable[[TEnd], bool]) -> Chain[TStart, TEnd]: # noqa:
122129
Remove elements from the stream that do not pass the filter function.
123130
"""
124131

132+
@functools.wraps(func)
125133
def new_action(previous_step: Iterator[TEnd]) -> Iterator[TEnd]:
126134
return filter(func, previous_step)
127135

128-
new_action.__name__ = func.__name__
129136
return self.flat_map(new_action)
130137

131138
def map( # noqa: A003
@@ -135,13 +142,13 @@ def map( # noqa: A003
135142
Return a single transformed element from each input element.
136143
"""
137144

145+
@functools.wraps(func)
138146
def new_action(previous_step: Iterator[TEnd]) -> Iterator[TOther]:
139147
return map(func, previous_step)
140148

141-
new_action.__name__ = func.__name__
142149
return self.flat_map(new_action)
143150

144-
def map_tuple(
151+
def batched_map(
145152
self, func: Callable[[Tuple[TEnd, ...]], TOther], n: Optional[int] = None
146153
) -> Chain[TStart, TOther]:
147154
"""
@@ -151,11 +158,11 @@ def map_tuple(
151158
an iterator of 1 element.
152159
"""
153160

161+
@functools.wraps(func)
154162
def new_action(previous_step: Iterator[TEnd]) -> Iterator[TOther]:
155-
while elements := tuple(islice(previous_step, n)):
163+
for elements in _batched(previous_step, n):
156164
yield func(elements)
157165

158-
new_action.__name__ = func.__name__
159166
return self.flat_map(new_action)
160167

161168
def get_counts(self) -> List[Dict[str, Any]]:

src/pipedata/core/stream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ def filter(self, func: Callable[[TEnd], bool]) -> Stream[TEnd]: # noqa: A003
4545
def map(self, func: Callable[[TEnd], TNewEnd]) -> Stream[TNewEnd]: # noqa: A003
4646
return Stream(self._items, self._chain.map(func))
4747

48-
def map_tuple(
48+
def batched_map(
4949
self, func: Callable[[Tuple[TEnd, ...]], TNewEnd], n: Optional[int] = None
5050
) -> Stream[TNewEnd]:
51-
return Stream(self._items, self._chain.map_tuple(func, n))
51+
return Stream(self._items, self._chain.batched_map(func, n))
5252

5353
@overload
5454
def reduce(self, func: Callable[[TEnd, TEnd], TEnd]) -> TEnd:

tests/core/test_chain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ def test_chain_map_changing_types() -> None:
151151
assert result == ["0", "1", "2", "3"]
152152

153153

154-
def test_chain_map_tuple() -> None:
154+
def test_chain_batched_map() -> None:
155155
def add_values(values: Tuple[int, ...]) -> int:
156156
return sum(values)
157157

158-
chain = ChainStart[int]().map_tuple(add_values, 2)
158+
chain = ChainStart[int]().batched_map(add_values, 2)
159159
result = list(chain(iter([0, 1, 2, 3, 4])))
160160
assert result == [1, 5, 4]
161161
assert chain.get_counts() == [

tests/core/test_stream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ def append_values(a: List[int], b: int) -> List[int]:
148148
assert result == [0, 1, 2, 3]
149149

150150

151-
def test_stream_map_tuple() -> None:
151+
def test_stream_batched_map() -> None:
152152
def add_values(values: Iterable[int]) -> int:
153153
return sum(values)
154154

155-
result = StreamStart([0, 1, 2, 3, 4]).map_tuple(add_values, 2).to_list()
155+
result = StreamStart([0, 1, 2, 3, 4]).batched_map(add_values, 2).to_list()
156156
assert result == [1, 5, 4]

0 commit comments

Comments
 (0)