pyochain 0.5.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,360 @@
1
+ from __future__ import annotations
2
+
3
+ import itertools
4
+ from collections.abc import Callable, Generator, Iterable, Iterator, Mapping, Sequence
5
+ from functools import partial
6
+ from typing import TYPE_CHECKING, Any, Concatenate, overload
7
+
8
+ import cytoolz as cz
9
+ import more_itertools as mit
10
+
11
+ from .._core import IterWrapper
12
+
13
+ if TYPE_CHECKING:
14
+ from ._main import Iter
15
+
16
+
17
+ class BaseMap[T](IterWrapper[T]):
18
+ def for_each[**P](
19
+ self,
20
+ func: Callable[Concatenate[T, P], Any],
21
+ *args: P.args,
22
+ **kwargs: P.kwargs,
23
+ ) -> None:
24
+ """
25
+ Consume the Iterator by applying a function to each element in the iterable.
26
+
27
+ Args:
28
+ func: Function to apply to each element.
29
+ args: Positional arguments for the function.
30
+ kwargs: Keyword arguments for the function.
31
+
32
+ Is a terminal operation, and is useful for functions that have side effects, or when you want to force evaluation of a lazy iterable.
33
+ ```python
34
+ >>> import pyochain as pc
35
+ >>> pc.Iter.from_([1, 2, 3]).for_each(lambda x: print(x))
36
+ 1
37
+ 2
38
+ 3
39
+
40
+ ```
41
+ """
42
+
43
+ def _for_each(data: Iterable[T]) -> None:
44
+ for v in data:
45
+ func(v, *args, **kwargs)
46
+
47
+ return self.into(_for_each)
48
+
49
+ def map[R](self, func: Callable[[T], R]) -> Iter[R]:
50
+ """
51
+ Map each element through func and return a Iter of results.
52
+
53
+ Args:
54
+ func: Function to apply to each element.
55
+
56
+ ```python
57
+ >>> import pyochain as pc
58
+ >>> pc.Iter.from_([1, 2]).map(lambda x: x + 1).into(list)
59
+ [2, 3]
60
+
61
+ ```
62
+ """
63
+ return self._lazy(partial(map, func))
64
+
65
+ @overload
66
+ def flat_map[U, R](
67
+ self: IterWrapper[Iterable[Iterable[Iterable[U]]]],
68
+ func: Callable[[T], Iterable[Iterable[R]]],
69
+ ) -> Iter[Iterable[Iterable[R]]]: ...
70
+ @overload
71
+ def flat_map[U, R](
72
+ self: IterWrapper[Iterable[Iterable[U]]], func: Callable[[T], Iterable[R]]
73
+ ) -> Iter[Iterable[R]]: ...
74
+ @overload
75
+ def flat_map[U, R](
76
+ self: IterWrapper[Iterable[U]], func: Callable[[T], R]
77
+ ) -> Iter[R]: ...
78
+ def flat_map[U: Iterable[Any], R](
79
+ self: IterWrapper[U], func: Callable[[T], R]
80
+ ) -> Iter[Any]:
81
+ """
82
+ Map each element through func and flatten the result by one level.
83
+
84
+ Args:
85
+ func: Function to apply to each element.
86
+
87
+ ```python
88
+ >>> import pyochain as pc
89
+ >>> data = [[1, 2], [3, 4]]
90
+ >>> pc.Iter.from_(data).flat_map(lambda x: x + 10).into(list)
91
+ [11, 12, 13, 14]
92
+
93
+ ```
94
+ """
95
+
96
+ def _flat_map(data: Iterable[U]) -> map[R]:
97
+ return map(func, itertools.chain.from_iterable(data))
98
+
99
+ return self._lazy(_flat_map)
100
+
101
+ def map_star[U: Iterable[Any], R](
102
+ self: IterWrapper[U], func: Callable[..., R]
103
+ ) -> Iter[R]:
104
+ """
105
+ Applies a function to each element, where each element is an iterable.
106
+
107
+ Args:
108
+ func: Function to apply to unpacked elements.
109
+
110
+ Unlike `.map()`, which passes each element as a single argument, `.starmap()` unpacks each element into positional arguments for the function.
111
+
112
+ In short, for each `element` in the sequence, it computes `func(*element)`.
113
+ ```python
114
+ >>> import pyochain as pc
115
+ >>> def make_sku(color, size):
116
+ ... return f"{color}-{size}"
117
+ >>> data = pc.Seq(["blue", "red"])
118
+ >>> data.iter().product(["S", "M"]).map_star(make_sku).into(list)
119
+ ['blue-S', 'blue-M', 'red-S', 'red-M']
120
+
121
+ ```
122
+ This is equivalent to:
123
+ ```python
124
+ >>> data.iter().product(["S", "M"]).map(lambda x: make_sku(*x)).into(list)
125
+ ['blue-S', 'blue-M', 'red-S', 'red-M']
126
+
127
+ ```
128
+
129
+ - Use map_star when the performance matters (it is faster).
130
+ - Use map with unpacking when readability matters (the types can be inferred).
131
+ """
132
+
133
+ return self._lazy(partial(itertools.starmap, func))
134
+
135
+ def map_if[R](
136
+ self,
137
+ predicate: Callable[[T], bool],
138
+ func: Callable[[T], R],
139
+ func_else: Callable[[T], R] | None = None,
140
+ ) -> Iter[R]:
141
+ """
142
+ Evaluate each item from iterable using pred.
143
+
144
+ Args:
145
+ predicate: Function to evaluate each item.
146
+ func: Function to apply if predicate is True.
147
+ func_else: Function to apply if predicate is False.
148
+
149
+ - If the result is equivalent to True, transform the item with func and yield it.
150
+ - Otherwise, transform the item with func_else and yield it.
151
+ - Predicate, func, and func_else should each be functions that accept one argument.
152
+
153
+ By default, func_else is the identity function.
154
+ ```python
155
+ >>> import pyochain as pc
156
+ >>> from math import sqrt
157
+ >>> iterable = pc.Iter.from_(range(-5, 5)).collect()
158
+ >>> iterable.into(list)
159
+ [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
160
+ >>> iterable.iter().map_if(lambda x: x > 3, lambda x: "toobig").into(list)
161
+ [-5, -4, -3, -2, -1, 0, 1, 2, 3, 'toobig']
162
+ >>> iterable.iter().map_if(
163
+ ... lambda x: x >= 0,
164
+ ... lambda x: f"{sqrt(x):.2f}",
165
+ ... lambda x: None,
166
+ ... ).into(list)
167
+ [None, None, None, None, None, '0.00', '1.00', '1.41', '1.73', '2.00']
168
+
169
+ ```
170
+ """
171
+ return self._lazy(mit.map_if, predicate, func, func_else=func_else)
172
+
173
+ def map_except[R](
174
+ self, func: Callable[[T], R], *exceptions: type[BaseException]
175
+ ) -> Iter[R]:
176
+ """
177
+ Transform each item from iterable with function and yield the result, unless function raises one of the specified exceptions.
178
+
179
+ Args:
180
+ func: Function to apply to each item.
181
+ exceptions: Exceptions to catch and ignore.
182
+
183
+ The function is called to transform each item in iterable
184
+
185
+ If an exception other than one given by exceptions is raised by function, it is raised like normal.
186
+ ```python
187
+ >>> import pyochain as pc
188
+ >>> iterable = ["1", "2", "three", "4", None]
189
+ >>> pc.Iter.from_(iterable).map_except(int, ValueError, TypeError).into(list)
190
+ [1, 2, 4]
191
+
192
+ ```
193
+ """
194
+
195
+ def _map_except(data: Iterable[T]) -> Iterator[R]:
196
+ return mit.map_except(func, data, *exceptions)
197
+
198
+ return self._lazy(_map_except)
199
+
200
+ def repeat(
201
+ self, n: int, factory: Callable[[Iterable[T]], Sequence[T]] = tuple
202
+ ) -> Iter[Iterable[T]]:
203
+ """
204
+ Repeat the entire iterable n times (as elements).
205
+
206
+ Args:
207
+ n: Number of repetitions.
208
+ factory: Factory to create the repeated Sequence (default: tuple).
209
+
210
+ ```python
211
+ >>> import pyochain as pc
212
+ >>> pc.Iter.from_([1, 2]).repeat(2).collect().unwrap()
213
+ [(1, 2), (1, 2)]
214
+ >>> pc.Iter.from_([1, 2]).repeat(3, list).collect().unwrap()
215
+ [[1, 2], [1, 2], [1, 2]]
216
+
217
+ ```
218
+ """
219
+
220
+ def _repeat(data: Iterable[T]) -> Iterator[Iterable[T]]:
221
+ return itertools.repeat(factory(data), n)
222
+
223
+ return self._lazy(_repeat)
224
+
225
+ @overload
226
+ def repeat_last(self, default: T) -> Iter[T]: ...
227
+ @overload
228
+ def repeat_last[U](self, default: U) -> Iter[T | U]: ...
229
+ def repeat_last[U](self, default: U = None) -> Iter[T | U]:
230
+ """
231
+ After the iterable is exhausted, keep yielding its last element.
232
+
233
+ **Warning** ⚠️
234
+ This creates an infinite iterator.
235
+ Be sure to use `Iter.take()` or `Iter.slice()` to limit the number of items taken.
236
+
237
+ Args:
238
+ default: Value to yield if the iterable is empty.
239
+
240
+ Example:
241
+ ```python
242
+ >>> import pyochain as pc
243
+ >>> pc.Iter.from_(range(3)).repeat_last().take(5).into(list)
244
+ [0, 1, 2, 2, 2]
245
+
246
+ If the iterable is empty, yield default forever:
247
+ ```python
248
+ >>> pc.Iter.from_(range(0)).repeat_last(42).take(5).into(list)
249
+ [42, 42, 42, 42, 42]
250
+
251
+ ```
252
+ """
253
+ return self._lazy(mit.repeat_last, default)
254
+
255
+ def ichunked(self, n: int) -> Iter[Iterator[T]]:
256
+ """
257
+
258
+ Break *iterable* into sub-iterables with *n* elements each.
259
+
260
+ Args:
261
+ n: Number of elements in each chunk.
262
+
263
+ If the sub-iterables are read in order, the elements of *iterable*
264
+ won't be stored in memory.
265
+
266
+ If they are read out of order, :func:`itertools.tee` is used to cache
267
+ elements as necessary.
268
+ ```python
269
+ >>> import pyochain as pc
270
+ >>> all_chunks = pc.Iter.from_count().ichunked(4).unwrap()
271
+ >>> c_1, c_2, c_3 = next(all_chunks), next(all_chunks), next(all_chunks)
272
+ >>> list(c_2) # c_1's elements have been cached; c_3's haven't been
273
+ [4, 5, 6, 7]
274
+ >>> list(c_1)
275
+ [0, 1, 2, 3]
276
+ >>> list(c_3)
277
+ [8, 9, 10, 11]
278
+
279
+ ```
280
+ """
281
+ return self._lazy(mit.ichunked, n)
282
+
283
+ @overload
284
+ def flatten[U](
285
+ self: IterWrapper[Iterable[Iterable[Iterable[U]]]],
286
+ ) -> Iter[Iterable[Iterable[U]]]: ...
287
+ @overload
288
+ def flatten[U](self: IterWrapper[Iterable[Iterable[U]]]) -> Iter[Iterable[U]]: ...
289
+ @overload
290
+ def flatten[U](self: IterWrapper[Iterable[U]]) -> Iter[U]: ...
291
+ def flatten(self: IterWrapper[Iterable[Any]]) -> Iter[Any]:
292
+ """
293
+ Flatten one level of nesting and return a new Iterable wrapper.
294
+
295
+ This is a shortcut for `.apply(itertools.chain.from_iterable)`.
296
+ ```python
297
+ >>> import pyochain as pc
298
+ >>> pc.Iter.from_([[1, 2], [3]]).flatten().into(list)
299
+ [1, 2, 3]
300
+
301
+ ```
302
+ """
303
+ return self._lazy(itertools.chain.from_iterable)
304
+
305
+ def pluck[U: Mapping[Any, Any]](
306
+ self: IterWrapper[U], *keys: str | int
307
+ ) -> Iter[Any]:
308
+ """
309
+ Get an element from each item in a sequence using a nested key path.
310
+
311
+ Args:
312
+ keys: Nested keys to extract values.
313
+
314
+ ```python
315
+ >>> import pyochain as pc
316
+ >>> data = pc.Seq(
317
+ ... [
318
+ ... {"id": 1, "info": {"name": "Alice", "age": 30}},
319
+ ... {"id": 2, "info": {"name": "Bob", "age": 25}},
320
+ ... ]
321
+ ... )
322
+ >>> data.iter().pluck("info").into(list)
323
+ [{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]
324
+ >>> data.iter().pluck("info", "name").into(list)
325
+ ['Alice', 'Bob']
326
+
327
+ ```
328
+ Example: get the maximum age along with the corresponding id)
329
+ ```python
330
+ >>> data.iter().pluck("info", "age").zip(
331
+ ... data.iter().pluck("id").into(list)
332
+ ... ).max()
333
+ (30, 1)
334
+
335
+ ```
336
+ """
337
+
338
+ getter = partial(cz.dicttoolz.get_in, keys)
339
+ return self._lazy(partial(map, getter))
340
+
341
+ def round[U: float | int](
342
+ self: IterWrapper[U], ndigits: int | None = None
343
+ ) -> Iter[float]:
344
+ """
345
+ Round each element in the iterable to the given number of decimal places.
346
+
347
+ Args:
348
+ ndigits: Number of decimal places to round to.
349
+ ```python
350
+ >>> import pyochain as pc
351
+ >>> pc.Iter.from_([1.2345, 2.3456, 3.4567]).round(2).into(list)
352
+ [1.23, 2.35, 3.46]
353
+
354
+ ```
355
+ """
356
+
357
+ def _round(data: Iterable[U]) -> Generator[float | int, None, None]:
358
+ return (round(x, ndigits) for x in data)
359
+
360
+ return self._lazy(_round)
@@ -0,0 +1,145 @@
1
+ from __future__ import annotations
2
+
3
+ import itertools
4
+ from collections.abc import Callable
5
+ from functools import partial
6
+ from typing import TYPE_CHECKING, Literal, overload
7
+
8
+ import cytoolz as cz
9
+
10
+ from .._core import IterWrapper
11
+
12
+ if TYPE_CHECKING:
13
+ from ._main import Iter
14
+
15
+
16
+ class BasePartitions[T](IterWrapper[T]):
17
+ @overload
18
+ def windows(self, length: Literal[1]) -> Iter[tuple[T]]: ...
19
+ @overload
20
+ def windows(self, length: Literal[2]) -> Iter[tuple[T, T]]: ...
21
+ @overload
22
+ def windows(self, length: Literal[3]) -> Iter[tuple[T, T, T]]: ...
23
+ @overload
24
+ def windows(self, length: Literal[4]) -> Iter[tuple[T, T, T, T]]: ...
25
+ @overload
26
+ def windows(self, length: Literal[5]) -> Iter[tuple[T, T, T, T, T]]: ...
27
+
28
+ def windows(self, length: int) -> Iter[tuple[T, ...]]:
29
+ """
30
+ A sequence of overlapping subsequences of the given length.
31
+
32
+ Args:
33
+ length: The length of each window.
34
+ ```python
35
+ >>> import pyochain as pc
36
+ >>> pc.Iter.from_([1, 2, 3, 4]).windows(2).into(list)
37
+ [(1, 2), (2, 3), (3, 4)]
38
+
39
+ ```
40
+ This function allows you to apply custom function not available in the rolling namespace.
41
+ ```python
42
+ >>> def moving_average(seq: tuple[int, ...]) -> float:
43
+ ... return float(sum(seq)) / len(seq)
44
+ >>> pc.Iter.from_([1, 2, 3, 4]).windows(2).map(moving_average).into(list)
45
+ [1.5, 2.5, 3.5]
46
+
47
+ ```
48
+ """
49
+ return self._lazy(partial(cz.itertoolz.sliding_window, length))
50
+
51
+ @overload
52
+ def partition(self, n: Literal[1], pad: None = None) -> Iter[tuple[T]]: ...
53
+ @overload
54
+ def partition(self, n: Literal[2], pad: None = None) -> Iter[tuple[T, T]]: ...
55
+ @overload
56
+ def partition(self, n: Literal[3], pad: None = None) -> Iter[tuple[T, T, T]]: ...
57
+ @overload
58
+ def partition(self, n: Literal[4], pad: None = None) -> Iter[tuple[T, T, T, T]]: ...
59
+ @overload
60
+ def partition(
61
+ self, n: Literal[5], pad: None = None
62
+ ) -> Iter[tuple[T, T, T, T, T]]: ...
63
+ @overload
64
+ def partition(self, n: int, pad: int) -> Iter[tuple[T, ...]]: ...
65
+ def partition(self, n: int, pad: int | None = None) -> Iter[tuple[T, ...]]:
66
+ """
67
+ Partition sequence into tuples of length n.
68
+
69
+ Args:
70
+ n: Length of each partition.
71
+ pad: Value to pad the last partition if needed.
72
+ ```python
73
+ >>> import pyochain as pc
74
+ >>> pc.Iter.from_([1, 2, 3, 4]).partition(2).into(list)
75
+ [(1, 2), (3, 4)]
76
+
77
+ ```
78
+ If the length of seq is not evenly divisible by n, the final tuple is dropped if pad is not specified, or filled to length n by pad:
79
+ ```python
80
+ >>> pc.Iter.from_([1, 2, 3, 4, 5]).partition(2).into(list)
81
+ [(1, 2), (3, 4), (5, None)]
82
+
83
+ ```
84
+ """
85
+
86
+ return self._lazy(partial(cz.itertoolz.partition, n, pad=pad))
87
+
88
+ def partition_all(self, n: int) -> Iter[tuple[T, ...]]:
89
+ """
90
+ Partition all elements of sequence into tuples of length at most n.
91
+
92
+ Args:
93
+ n: Maximum length of each partition.
94
+ The final tuple may be shorter to accommodate extra elements.
95
+ ```python
96
+ >>> import pyochain as pc
97
+ >>> pc.Iter.from_([1, 2, 3, 4]).partition_all(2).into(list)
98
+ [(1, 2), (3, 4)]
99
+ >>> pc.Iter.from_([1, 2, 3, 4, 5]).partition_all(2).into(list)
100
+ [(1, 2), (3, 4), (5,)]
101
+
102
+ ```
103
+ """
104
+ return self._lazy(partial(cz.itertoolz.partition_all, n))
105
+
106
+ def partition_by(self, predicate: Callable[[T], bool]) -> Iter[tuple[T, ...]]:
107
+ """
108
+ Partition the `iterable` into a sequence of `tuples` according to a predicate function.
109
+
110
+ Every time the output of `predicate` changes, a new `tuple` is started,
111
+ and subsequent items are collected into that `tuple`.
112
+
113
+ Args:
114
+ predicate: Function to determine partition boundaries.
115
+ ```python
116
+ >>> import pyochain as pc
117
+ >>> pc.Iter.from_("I have space").partition_by(lambda c: c == " ").into(list)
118
+ [('I',), (' ',), ('h', 'a', 'v', 'e'), (' ',), ('s', 'p', 'a', 'c', 'e')]
119
+ >>>
120
+ >>> data = [1, 2, 1, 99, 88, 33, 99, -1, 5]
121
+ >>> pc.Iter.from_(data).partition_by(lambda x: x > 10).into(list)
122
+ [(1, 2, 1), (99, 88, 33, 99), (-1, 5)]
123
+
124
+ ```
125
+ """
126
+ return self._lazy(partial(cz.recipes.partitionby, predicate))
127
+
128
+ def batch(self, n: int) -> Iter[tuple[T, ...]]:
129
+ """
130
+ Batch elements into tuples of length n and return a new Iter.
131
+
132
+ - The last batch may be shorter than n.
133
+ - The data is consumed lazily, just enough to fill a batch.
134
+ - The result is yielded as soon as a batch is full or when the input iterable is exhausted.
135
+
136
+ Args:
137
+ n: Number of elements in each batch.
138
+ ```python
139
+ >>> import pyochain as pc
140
+ >>> pc.Iter.from_("ABCDEFG").batch(3).into(list)
141
+ [('A', 'B', 'C'), ('D', 'E', 'F'), ('G',)]
142
+
143
+ ```
144
+ """
145
+ return self._lazy(itertools.batched, n)