pyochain 0.5.1__py3-none-any.whl → 0.5.31__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.

Potentially problematic release.


This version of pyochain might be problematic. Click here for more details.

pyochain/_iter/_groups.py DELETED
@@ -1,264 +0,0 @@
1
- from __future__ import annotations
2
-
3
- from collections.abc import Callable, Iterable, Iterator
4
- from functools import partial
5
- from typing import TYPE_CHECKING, Any, overload
6
-
7
- import cytoolz as cz
8
- import more_itertools as mit
9
-
10
- from .._core import IterWrapper
11
-
12
- if TYPE_CHECKING:
13
- from .._dict import Dict
14
- from ._main import Iter
15
-
16
-
17
- class BaseGroups[T](IterWrapper[T]):
18
- def reduce_by[K](
19
- self, key: Callable[[T], K], binop: Callable[[T, T], T]
20
- ) -> Dict[K, T]:
21
- """
22
- Perform a simultaneous groupby and reduction.
23
-
24
- Args:
25
- key: Function to compute the key for grouping.
26
- binop: Binary operation to reduce the grouped elements.
27
- Example:
28
- ```python
29
- >>> from collections.abc import Iterable
30
- >>> import pyochain as pc
31
- >>> from operator import add, mul
32
- >>>
33
- >>> def is_even(x: int) -> bool:
34
- ... return x % 2 == 0
35
- >>>
36
- >>> def group_reduce(data: Iterable[int]) -> int:
37
- ... return pc.Iter.from_(data).reduce(add)
38
- >>>
39
- >>> data = pc.Seq([1, 2, 3, 4, 5])
40
- >>> data.iter().reduce_by(is_even, add).unwrap()
41
- {False: 9, True: 6}
42
- >>> data.iter().group_by(is_even).map_values(group_reduce).unwrap()
43
- {False: 9, True: 6}
44
-
45
- ```
46
- But the former does not build the intermediate groups, allowing it to operate in much less space.
47
-
48
- This makes it suitable for larger datasets that do not fit comfortably in memory
49
-
50
- Simple Examples:
51
- ```python
52
- >>> pc.Iter.from_([1, 2, 3, 4, 5]).reduce_by(is_even, add).unwrap()
53
- {False: 9, True: 6}
54
- >>> pc.Iter.from_([1, 2, 3, 4, 5]).reduce_by(is_even, mul).unwrap()
55
- {False: 15, True: 8}
56
-
57
- ```
58
- """
59
- from .._dict import Dict
60
-
61
- return Dict(self.into(partial(cz.itertoolz.reduceby, key, binop)))
62
-
63
- def group_by[K](self, on: Callable[[T], K]) -> Dict[K, list[T]]:
64
- """
65
- Group elements by key function and return a Dict result.
66
-
67
- Args:
68
- on: Function to compute the key for grouping.
69
- Example:
70
- ```python
71
- >>> import pyochain as pc
72
- >>> names = [
73
- ... "Alice",
74
- ... "Bob",
75
- ... "Charlie",
76
- ... "Dan",
77
- ... "Edith",
78
- ... "Frank",
79
- ... ]
80
- >>> pc.Iter.from_(names).group_by(len).sort()
81
- ... # doctest: +NORMALIZE_WHITESPACE
82
- Dict({
83
- 3: ['Bob', 'Dan'],
84
- 5: ['Alice', 'Edith', 'Frank'],
85
- 7: ['Charlie']
86
- })
87
- >>>
88
- >>> iseven = lambda x: x % 2 == 0
89
- >>> pc.Iter.from_([1, 2, 3, 4, 5, 6, 7, 8]).group_by(iseven)
90
- ... # doctest: +NORMALIZE_WHITESPACE
91
- Dict({
92
- False: [1, 3, 5, 7],
93
- True: [2, 4, 6, 8]
94
- })
95
-
96
- ```
97
- Non-callable keys imply grouping on a member.
98
- ```python
99
- >>> data = [
100
- ... {"name": "Alice", "gender": "F"},
101
- ... {"name": "Bob", "gender": "M"},
102
- ... {"name": "Charlie", "gender": "M"},
103
- ... ]
104
- >>> pc.Iter.from_(data).group_by("gender").sort()
105
- ... # doctest: +NORMALIZE_WHITESPACE
106
- Dict({
107
- 'F': [
108
- {'name': 'Alice', 'gender': 'F'}
109
- ],
110
- 'M': [
111
- {'name': 'Bob', 'gender': 'M'},
112
- {'name': 'Charlie', 'gender': 'M'}
113
- ]
114
- })
115
-
116
- ```
117
- """
118
- from .._dict import Dict
119
-
120
- return Dict(self.into(partial(cz.itertoolz.groupby, on)))
121
-
122
- def frequencies(self) -> Dict[T, int]:
123
- """
124
- Find number of occurrences of each value in the iterable.
125
- ```python
126
- >>> import pyochain as pc
127
- >>> data = ["cat", "cat", "ox", "pig", "pig", "cat"]
128
- >>> pc.Iter.from_(data).frequencies().unwrap()
129
- {'cat': 3, 'ox': 1, 'pig': 2}
130
-
131
- ```
132
- """
133
- from .._dict import Dict
134
-
135
- return Dict(self.into(cz.itertoolz.frequencies))
136
-
137
- def count_by[K](self, key: Callable[[T], K]) -> Dict[K, int]:
138
- """
139
- Count elements of a collection by a key function.
140
-
141
- Args:
142
- key: Function to compute the key for counting.
143
- Example:
144
- ```python
145
- >>> import pyochain as pc
146
- >>> pc.Iter.from_(["cat", "mouse", "dog"]).count_by(len).unwrap()
147
- {3: 2, 5: 1}
148
- >>> def iseven(x):
149
- ... return x % 2 == 0
150
- >>> pc.Iter.from_([1, 2, 3]).count_by(iseven).unwrap()
151
- {False: 2, True: 1}
152
-
153
- ```
154
- """
155
- from .._dict import Dict
156
-
157
- return Dict(self.into(partial(cz.recipes.countby, key)))
158
-
159
- @overload
160
- def group_by_transform(
161
- self,
162
- keyfunc: None = None,
163
- valuefunc: None = None,
164
- reducefunc: None = None,
165
- ) -> Iter[tuple[T, Iterator[T]]]: ...
166
- @overload
167
- def group_by_transform[U](
168
- self,
169
- keyfunc: Callable[[T], U],
170
- valuefunc: None,
171
- reducefunc: None,
172
- ) -> Iter[tuple[U, Iterator[T]]]: ...
173
- @overload
174
- def group_by_transform[V](
175
- self,
176
- keyfunc: None,
177
- valuefunc: Callable[[T], V],
178
- reducefunc: None,
179
- ) -> Iter[tuple[T, Iterator[V]]]: ...
180
- @overload
181
- def group_by_transform[U, V](
182
- self,
183
- keyfunc: Callable[[T], U],
184
- valuefunc: Callable[[T], V],
185
- reducefunc: None,
186
- ) -> Iter[tuple[U, Iterator[V]]]: ...
187
- @overload
188
- def group_by_transform[W](
189
- self,
190
- keyfunc: None,
191
- valuefunc: None,
192
- reducefunc: Callable[[Iterator[T]], W],
193
- ) -> Iter[tuple[T, W]]: ...
194
- @overload
195
- def group_by_transform[U, W](
196
- self,
197
- keyfunc: Callable[[T], U],
198
- valuefunc: None,
199
- reducefunc: Callable[[Iterator[T]], W],
200
- ) -> Iter[tuple[U, W]]: ...
201
- @overload
202
- def group_by_transform[V, W](
203
- self,
204
- keyfunc: None,
205
- valuefunc: Callable[[T], V],
206
- reducefunc: Callable[[Iterator[V]], W],
207
- ) -> Iter[tuple[T, W]]: ...
208
- @overload
209
- def group_by_transform[U, V, W](
210
- self,
211
- keyfunc: Callable[[T], U],
212
- valuefunc: Callable[[T], V],
213
- reducefunc: Callable[[Iterator[V]], W],
214
- ) -> Iter[tuple[U, W]]: ...
215
- def group_by_transform[U, V](
216
- self,
217
- keyfunc: Callable[[T], U] | None = None,
218
- valuefunc: Callable[[T], V] | None = None,
219
- reducefunc: Any = None,
220
- ) -> Iter[tuple[Any, ...]]:
221
- """
222
- An extension of itertools.groupby that can apply transformations to the grouped data.
223
-
224
- Args:
225
- keyfunc: Function to compute the key for grouping. Defaults to None.
226
- valuefunc: Function to transform individual items after grouping. Defaults to None.
227
- reducefunc: Function to transform each group of items. Defaults to None.
228
-
229
- Example:
230
- ```python
231
- >>> import pyochain as pc
232
- >>> data = pc.Iter.from_("aAAbBBcCC")
233
- >>> data.group_by_transform(
234
- ... lambda k: k.upper(), lambda v: v.lower(), lambda g: "".join(g)
235
- ... ).into(list)
236
- [('A', 'aaa'), ('B', 'bbb'), ('C', 'ccc')]
237
-
238
- ```
239
- Each optional argument defaults to an identity function if not specified.
240
-
241
- group_by_transform is useful when grouping elements of an iterable using a separate iterable as the key.
242
-
243
- To do this, zip the iterables and pass a keyfunc that extracts the first element and a valuefunc that extracts the second element:
244
-
245
- Note that the order of items in the iterable is significant.
246
-
247
- Only adjacent items are grouped together, so if you don't want any duplicate groups, you should sort the iterable by the key function.
248
-
249
- Example:
250
- ```python
251
- >>> from operator import itemgetter
252
- >>> data = pc.Iter.from_([0, 0, 1, 1, 1, 2, 2, 2, 3])
253
- >>> data.zip("abcdefghi").group_by_transform(itemgetter(0), itemgetter(1)).map(
254
- ... lambda kv: (kv[0], "".join(kv[1]))
255
- ... ).into(list)
256
- [(0, 'ab'), (1, 'cde'), (2, 'fgh'), (3, 'i')]
257
-
258
- ```
259
- """
260
-
261
- def _group_by_transform(data: Iterable[T]) -> Iterator[tuple[Any, ...]]:
262
- return mit.groupby_transform(data, keyfunc, valuefunc, reducefunc)
263
-
264
- return self.apply(_group_by_transform)
@@ -1,33 +0,0 @@
1
- pyochain/__init__.py,sha256=f5iynOtt1j-5GNsyBlThe4KHLqnDXXxxSfrBr72KRjM,152
2
- pyochain/_core/__init__.py,sha256=J91CV32HesQdEK8D5iwLoWW9LRGbtN4wqHY6c12eZo8,451
3
- pyochain/_core/_main.py,sha256=LdcJRYGvHfGYTmY0avTm7M2ykPDspfO_R7wKDxQ3oQY,5468
4
- pyochain/_core/_protocols.py,sha256=D0t1-amduqjN27qoKESApBr7YPx6Dt5kB6oH9VTDq8o,941
5
- pyochain/_dict/__init__.py,sha256=PdRjmX-7GTlZy3qbeS1IFO6QZNgjhVnhrUje-EvNZfw,89
6
- pyochain/_dict/_exprs.py,sha256=KJwVXZS64GjpbjtHVl0PPTY1KvZVG-qo6HPSvigg0NU,3137
7
- pyochain/_dict/_filters.py,sha256=E7aykSNn2N74n2HS2DhG_intEU7KoNQHFxUYDEVydV8,8234
8
- pyochain/_dict/_funcs.py,sha256=zse4gAU7Zk6TnKvxvyyJtWwvydJ59gDqMOWOukh5T94,2209
9
- pyochain/_dict/_groups.py,sha256=vw4lmBORvJ5w3u3rnG0G1xa4LONF5rthl5qVy7luhhw,6122
10
- pyochain/_dict/_iter.py,sha256=LwylGaaQeB2Xx94U4UgVH9f66jzBeCx2THuv60iV3DY,2463
11
- pyochain/_dict/_joins.py,sha256=wvexoINtPpO5zZvuorJJ2RYLFOE3JbSZ-19S3QHk1F0,4409
12
- pyochain/_dict/_main.py,sha256=Jkb84P-5WqZqsZPfxnk_nWcrZPIXFE3iTn40-lYjALc,9130
13
- pyochain/_dict/_nested.py,sha256=vjuh-J7KIPOiTKCwZdHLobXnrgxWxJyFJ8VXDjXHkM4,7929
14
- pyochain/_dict/_process.py,sha256=B5ivADOuwShODeg7Kh4fMwvGQb1vRXISHTtUi-DkvWs,5443
15
- pyochain/_iter/__init__.py,sha256=a8YS8Yx_UbLXdzM70YQrt6gyv1v7QW_16i2ydsyGGV8,56
16
- pyochain/_iter/_aggregations.py,sha256=LLdeK8GwdMV8-ljlOCQPF3i90OUoAg8ImGU06pVu6O8,8583
17
- pyochain/_iter/_booleans.py,sha256=3wOlvCqIV20sKwyPlwUq57cWpUbiKNYm3CMU6gtuYVY,7207
18
- pyochain/_iter/_constructors.py,sha256=sEB8W6r22kjBuu0lUvyPKwHLXbPc9L9diK5UYMSTGU0,5348
19
- pyochain/_iter/_eager.py,sha256=1GklA98m7rRYKh6NVqHnwcwxt4eDSkVnyakcATmH-Rc,5562
20
- pyochain/_iter/_filters.py,sha256=e9F8j5VgPqD4jPwCwPMdjbi5I8bb-mjzBXKyYIiwiTU,15321
21
- pyochain/_iter/_groups.py,sha256=oUkxgNXqC9je2XRCpV8MVQYiVwp4t8pKejiPBtfY340,8327
22
- pyochain/_iter/_joins.py,sha256=dF28_muvSxMnShauTEQ0KkcA1b9JZia1ieAhWBQ_nk8,13134
23
- pyochain/_iter/_lists.py,sha256=N71EV_ha3vjnMOcRVLZy4-Sk2mf7rDxmL_GLgRtBU8k,11101
24
- pyochain/_iter/_main.py,sha256=1_XL9boT5GKi6SwYBIXoIcO5P9tqFGV1nWWs6qX0BV8,7191
25
- pyochain/_iter/_maps.py,sha256=5W92MYGCKWK2d-byoomXktVPJZa5sCFVHTb6a5Duqko,11395
26
- pyochain/_iter/_partitions.py,sha256=y97P9Y7wMX8qcSfdly6PVleB9D8gfAWeBAcNlDKZuOo,5098
27
- pyochain/_iter/_process.py,sha256=obpPnz4uYmdikPLJGlcyWVGmU2k0bEkU6nMi5zZdatY,11449
28
- pyochain/_iter/_rolling.py,sha256=lcqtkTJtHtEwpZaj_d3ILoCKHh_GVJhOg7L9BE0rWq4,7053
29
- pyochain/_iter/_tuples.py,sha256=FeKFQ5KAZZaPlnykE3AD4MEaZLaPVFw3w9LEqR_BVWE,7477
30
- pyochain/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
31
- pyochain-0.5.1.dist-info/WHEEL,sha256=eh7sammvW2TypMMMGKgsM83HyA_3qQ5Lgg3ynoecH3M,79
32
- pyochain-0.5.1.dist-info/METADATA,sha256=DKVxsP-GAC8ZRZTVc-piPzQv6y9kWbYaJK1GQMS_nSI,11108
33
- pyochain-0.5.1.dist-info/RECORD,,