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

@@ -31,7 +31,6 @@ class BasePartitions[T](IterWrapper[T]):
31
31
 
32
32
  Args:
33
33
  length: The length of each window.
34
-
35
34
  ```python
36
35
  >>> import pyochain as pc
37
36
  >>> pc.Iter.from_([1, 2, 3, 4]).windows(2).into(list)
@@ -47,7 +46,7 @@ class BasePartitions[T](IterWrapper[T]):
47
46
 
48
47
  ```
49
48
  """
50
- return self.apply(partial(cz.itertoolz.sliding_window, length))
49
+ return self._lazy(partial(cz.itertoolz.sliding_window, length))
51
50
 
52
51
  @overload
53
52
  def partition(self, n: Literal[1], pad: None = None) -> Iter[tuple[T]]: ...
@@ -70,7 +69,6 @@ class BasePartitions[T](IterWrapper[T]):
70
69
  Args:
71
70
  n: Length of each partition.
72
71
  pad: Value to pad the last partition if needed.
73
-
74
72
  ```python
75
73
  >>> import pyochain as pc
76
74
  >>> pc.Iter.from_([1, 2, 3, 4]).partition(2).into(list)
@@ -85,7 +83,7 @@ class BasePartitions[T](IterWrapper[T]):
85
83
  ```
86
84
  """
87
85
 
88
- return self.apply(partial(cz.itertoolz.partition, n, pad=pad))
86
+ return self._lazy(partial(cz.itertoolz.partition, n, pad=pad))
89
87
 
90
88
  def partition_all(self, n: int) -> Iter[tuple[T, ...]]:
91
89
  """
@@ -93,7 +91,6 @@ class BasePartitions[T](IterWrapper[T]):
93
91
 
94
92
  Args:
95
93
  n: Maximum length of each partition.
96
-
97
94
  The final tuple may be shorter to accommodate extra elements.
98
95
  ```python
99
96
  >>> import pyochain as pc
@@ -104,17 +101,17 @@ class BasePartitions[T](IterWrapper[T]):
104
101
 
105
102
  ```
106
103
  """
107
- return self.apply(partial(cz.itertoolz.partition_all, n))
104
+ return self._lazy(partial(cz.itertoolz.partition_all, n))
108
105
 
109
106
  def partition_by(self, predicate: Callable[[T], bool]) -> Iter[tuple[T, ...]]:
110
107
  """
111
108
  Partition the `iterable` into a sequence of `tuples` according to a predicate function.
112
109
 
113
- Args:
114
- predicate: Function to determine partition boundaries.
115
-
116
110
  Every time the output of `predicate` changes, a new `tuple` is started,
117
111
  and subsequent items are collected into that `tuple`.
112
+
113
+ Args:
114
+ predicate: Function to determine partition boundaries.
118
115
  ```python
119
116
  >>> import pyochain as pc
120
117
  >>> pc.Iter.from_("I have space").partition_by(lambda c: c == " ").into(list)
@@ -126,18 +123,18 @@ class BasePartitions[T](IterWrapper[T]):
126
123
 
127
124
  ```
128
125
  """
129
- return self.apply(partial(cz.recipes.partitionby, predicate))
126
+ return self._lazy(partial(cz.recipes.partitionby, predicate))
130
127
 
131
128
  def batch(self, n: int) -> Iter[tuple[T, ...]]:
132
129
  """
133
130
  Batch elements into tuples of length n and return a new Iter.
134
131
 
135
- Args:
136
- n: Number of elements in each batch.
137
-
138
132
  - The last batch may be shorter than n.
139
133
  - The data is consumed lazily, just enough to fill a batch.
140
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.
141
138
  ```python
142
139
  >>> import pyochain as pc
143
140
  >>> pc.Iter.from_("ABCDEFG").batch(3).into(list)
@@ -145,4 +142,4 @@ class BasePartitions[T](IterWrapper[T]):
145
142
 
146
143
  ```
147
144
  """
148
- return self.apply(itertools.batched, n)
145
+ return self._lazy(itertools.batched, n)
@@ -9,7 +9,7 @@ from typing import TYPE_CHECKING, Any
9
9
  import cytoolz as cz
10
10
  import more_itertools as mit
11
11
 
12
- from .._core import IterWrapper, Peeked
12
+ from .._core import IterWrapper, peek, peekn
13
13
 
14
14
  if TYPE_CHECKING:
15
15
  from ._main import Iter
@@ -46,7 +46,7 @@ class BaseProcess[T](IterWrapper[T]):
46
46
 
47
47
  ```
48
48
  """
49
- return self.apply(itertools.cycle)
49
+ return self._lazy(itertools.cycle)
50
50
 
51
51
  def interpose(self, element: T) -> Iter[T]:
52
52
  """
@@ -54,7 +54,6 @@ class BaseProcess[T](IterWrapper[T]):
54
54
 
55
55
  Args:
56
56
  element: The element to interpose between items.
57
-
58
57
  Example:
59
58
  ```python
60
59
  >>> import pyochain as pc
@@ -63,7 +62,7 @@ class BaseProcess[T](IterWrapper[T]):
63
62
 
64
63
  ```
65
64
  """
66
- return self.apply(partial(cz.itertoolz.interpose, element))
65
+ return self._lazy(partial(cz.itertoolz.interpose, element))
67
66
 
68
67
  def random_sample(
69
68
  self, probability: float, state: Random | int | None = None
@@ -71,15 +70,15 @@ class BaseProcess[T](IterWrapper[T]):
71
70
  """
72
71
  Return elements from a sequence with probability of prob.
73
72
 
74
- Args:
75
- probability: The probability of including each element.
76
- state: Random state or seed for deterministic sampling.
77
-
78
73
  Returns a lazy iterator of random items from seq.
79
74
 
80
75
  random_sample considers each item independently and without replacement.
81
76
 
82
77
  See below how the first time it returned 13 items and the next time it returned 6 items.
78
+
79
+ Args:
80
+ probability: The probability of including each element.
81
+ state: Random state or seed for deterministic sampling.
83
82
  ```python
84
83
  >>> import pyochain as pc
85
84
  >>> data = pc.Seq(list(range(100)))
@@ -108,7 +107,7 @@ class BaseProcess[T](IterWrapper[T]):
108
107
  ```
109
108
  """
110
109
 
111
- return self.apply(
110
+ return self._lazy(
112
111
  partial(cz.itertoolz.random_sample, probability, random_state=state)
113
112
  )
114
113
 
@@ -118,7 +117,6 @@ class BaseProcess[T](IterWrapper[T]):
118
117
 
119
118
  Args:
120
119
  func: A binary function to apply cumulatively.
121
-
122
120
  ```python
123
121
  >>> import pyochain as pc
124
122
  >>> pc.Iter.from_([1, 2, 3]).accumulate(lambda a, b: a + b).into(list)
@@ -126,7 +124,7 @@ class BaseProcess[T](IterWrapper[T]):
126
124
 
127
125
  ```
128
126
  """
129
- return self.apply(partial(cz.itertoolz.accumulate, func))
127
+ return self._lazy(partial(cz.itertoolz.accumulate, func))
130
128
 
131
129
  def insert_left(self, value: T) -> Iter[T]:
132
130
  """
@@ -134,7 +132,6 @@ class BaseProcess[T](IterWrapper[T]):
134
132
 
135
133
  Args:
136
134
  value: The value to prepend.
137
-
138
135
  ```python
139
136
  >>> import pyochain as pc
140
137
  >>> pc.Iter.from_([2, 3]).insert_left(1).into(list)
@@ -142,7 +139,7 @@ class BaseProcess[T](IterWrapper[T]):
142
139
 
143
140
  ```
144
141
  """
145
- return self.apply(partial(cz.itertoolz.cons, value))
142
+ return self._lazy(partial(cz.itertoolz.cons, value))
146
143
 
147
144
  def peekn(self, n: int) -> Iter[T]:
148
145
  """
@@ -150,7 +147,6 @@ class BaseProcess[T](IterWrapper[T]):
150
147
 
151
148
  Args:
152
149
  n: Number of items to peek.
153
-
154
150
  ```python
155
151
  >>> import pyochain as pc
156
152
  >>> pc.Iter.from_([1, 2, 3]).peekn(2).into(list)
@@ -159,13 +155,7 @@ class BaseProcess[T](IterWrapper[T]):
159
155
 
160
156
  ```
161
157
  """
162
-
163
- def _peekn(data: Iterable[T]) -> Iterator[T]:
164
- peeked = Peeked(*cz.itertoolz.peekn(n, data))
165
- print(f"Peeked {n} values: {peeked.value}")
166
- return peeked.sequence
167
-
168
- return self.apply(_peekn)
158
+ return self._lazy(peekn, n)
169
159
 
170
160
  def peek(self) -> Iter[T]:
171
161
  """
@@ -178,13 +168,7 @@ class BaseProcess[T](IterWrapper[T]):
178
168
 
179
169
  ```
180
170
  """
181
-
182
- def _peek(data: Iterable[T]) -> Iterator[T]:
183
- peeked = Peeked(*cz.itertoolz.peek(data))
184
- print(f"Peeked value: {peeked.value}")
185
- return peeked.sequence
186
-
187
- return self.apply(_peek)
171
+ return self._lazy(peek)
188
172
 
189
173
  def merge_sorted(
190
174
  self, *others: Iterable[T], sort_on: Callable[[T], Any] | None = None
@@ -195,7 +179,6 @@ class BaseProcess[T](IterWrapper[T]):
195
179
  Args:
196
180
  others: Other sorted iterables to merge.
197
181
  sort_on: Optional key function for sorting.
198
-
199
182
  ```python
200
183
  >>> import pyochain as pc
201
184
  >>> pc.Iter.from_([1, 3]).merge_sorted([2, 4]).into(list)
@@ -203,7 +186,7 @@ class BaseProcess[T](IterWrapper[T]):
203
186
 
204
187
  ```
205
188
  """
206
- return self.apply(cz.itertoolz.merge_sorted, *others, key=sort_on)
189
+ return self._lazy(cz.itertoolz.merge_sorted, *others, key=sort_on)
207
190
 
208
191
  def interleave(self, *others: Iterable[T]) -> Iter[T]:
209
192
  """
@@ -211,7 +194,6 @@ class BaseProcess[T](IterWrapper[T]):
211
194
 
212
195
  Args:
213
196
  others: Other iterables to interleave.
214
-
215
197
  ```python
216
198
  >>> import pyochain as pc
217
199
  >>> pc.Iter.from_([1, 2]).interleave([3, 4]).into(list)
@@ -223,18 +205,18 @@ class BaseProcess[T](IterWrapper[T]):
223
205
  def _interleave(data: Iterable[T]) -> Iterator[T]:
224
206
  return cz.itertoolz.interleave((data, *others))
225
207
 
226
- return self.apply(_interleave)
208
+ return self._lazy(_interleave)
227
209
 
228
210
  def chain(self, *others: Iterable[T]) -> Iter[T]:
229
211
  """
230
212
  Concatenate zero or more iterables, any of which may be infinite.
231
213
 
232
- Args:
233
- others: Other iterables to concatenate.
234
-
235
214
  An infinite sequence will prevent the rest of the arguments from being included.
236
215
 
237
216
  We use chain.from_iterable rather than chain(*seqs) so that seqs can be a generator.
217
+
218
+ Args:
219
+ others: Other iterables to concatenate.
238
220
  ```python
239
221
  >>> import pyochain as pc
240
222
  >>> pc.Iter.from_([1, 2]).chain([3, 4], [5]).into(list)
@@ -244,9 +226,9 @@ class BaseProcess[T](IterWrapper[T]):
244
226
  """
245
227
 
246
228
  def _chain(data: Iterable[T]) -> Iterator[T]:
247
- return itertools.chain.from_iterable((data, *others))
229
+ return cz.itertoolz.concat((data, *others))
248
230
 
249
- return self.apply(_chain)
231
+ return self._lazy(_chain)
250
232
 
251
233
  def elements(self) -> Iter[T]:
252
234
  """
@@ -275,7 +257,7 @@ class BaseProcess[T](IterWrapper[T]):
275
257
  def _elements(data: Iterable[T]) -> Iterator[T]:
276
258
  return Counter(data).elements()
277
259
 
278
- return self.apply(_elements)
260
+ return self._lazy(_elements)
279
261
 
280
262
  def reverse(self) -> Iter[T]:
281
263
  """
@@ -296,7 +278,7 @@ class BaseProcess[T](IterWrapper[T]):
296
278
  def _reverse(data: Iterable[T]) -> Iterator[T]:
297
279
  return reversed(list(data))
298
280
 
299
- return self.apply(_reverse)
281
+ return self._lazy(_reverse)
300
282
 
301
283
  def is_strictly_n(
302
284
  self,
@@ -307,14 +289,14 @@ class BaseProcess[T](IterWrapper[T]):
307
289
  """
308
290
  Validate that *iterable* has exactly *n* items and return them if it does.
309
291
 
292
+ If it has fewer than *n* items, call function *too_short* with the actual number of items.
293
+
294
+ If it has more than *n* items, call function *too_long* with the number `n + 1`.
295
+
310
296
  Args:
311
297
  n: The exact number of items expected.
312
298
  too_short: Function to call if there are too few items.
313
299
  too_long: Function to call if there are too many items.
314
-
315
- If it has fewer than *n* items, call function *too_short* with the actual number of items.
316
-
317
- If it has more than *n* items, call function *too_long* with the number `n + 1`.
318
300
  ```python
319
301
  >>> import pyochain as pc
320
302
  >>> iterable = ["a", "b", "c", "d"]
@@ -381,4 +363,4 @@ class BaseProcess[T](IterWrapper[T]):
381
363
  too_long(n + 1)
382
364
  return
383
365
 
384
- return self.apply(strictly_n_)
366
+ return self._lazy(strictly_n_)
@@ -18,7 +18,6 @@ class BaseRolling[T](IterWrapper[T]):
18
18
 
19
19
  Args:
20
20
  window_size: Size of the rolling window.
21
-
22
21
  ```python
23
22
  >>> import pyochain as pc
24
23
  >>> pc.Iter.from_([1, 2, 3, 4, 5]).rolling_mean(3).into(list)
@@ -26,7 +25,7 @@ class BaseRolling[T](IterWrapper[T]):
26
25
 
27
26
  ```
28
27
  """
29
- return self.apply(rolling.Mean, window_size)
28
+ return self._lazy(rolling.Mean, window_size)
30
29
 
31
30
  def rolling_median(self, window_size: int) -> Iter[T]:
32
31
  """
@@ -34,7 +33,6 @@ class BaseRolling[T](IterWrapper[T]):
34
33
 
35
34
  Args:
36
35
  window_size: Size of the rolling window.
37
-
38
36
  ```python
39
37
  >>> import pyochain as pc
40
38
  >>> pc.Iter.from_([1, 3, 2, 5, 4]).rolling_median(3).into(list)
@@ -42,16 +40,16 @@ class BaseRolling[T](IterWrapper[T]):
42
40
 
43
41
  ```
44
42
  """
45
- return self.apply(rolling.Median, window_size)
43
+ return self._lazy(rolling.Median, window_size)
46
44
 
47
45
  def rolling_sum(self, window_size: int) -> Iter[T]:
48
46
  """
49
47
  Compute the rolling sum.
50
48
 
49
+ Will return integers if the input is integers, floats if the input is floats or mixed.
50
+
51
51
  Args:
52
52
  window_size: Size of the rolling window.
53
-
54
- Will return integers if the input is integers, floats if the input is floats or mixed.
55
53
  ```python
56
54
  >>> import pyochain as pc
57
55
  >>> pc.Iter.from_([1.0, 2, 3, 4, 5]).rolling_sum(3).into(list)
@@ -59,7 +57,7 @@ class BaseRolling[T](IterWrapper[T]):
59
57
 
60
58
  ```
61
59
  """
62
- return self.apply(rolling.Sum, window_size)
60
+ return self._lazy(rolling.Sum, window_size)
63
61
 
64
62
  def rolling_min(self, window_size: int) -> Iter[T]:
65
63
  """
@@ -74,7 +72,7 @@ class BaseRolling[T](IterWrapper[T]):
74
72
 
75
73
  ```
76
74
  """
77
- return self.apply(rolling.Min, window_size)
75
+ return self._lazy(rolling.Min, window_size)
78
76
 
79
77
  def rolling_max(self, window_size: int) -> Iter[T]:
80
78
  """
@@ -89,7 +87,7 @@ class BaseRolling[T](IterWrapper[T]):
89
87
 
90
88
  ```
91
89
  """
92
- return self.apply(rolling.Max, window_size)
90
+ return self._lazy(rolling.Max, window_size)
93
91
 
94
92
  def rolling_var(self, window_size: int) -> Iter[float]:
95
93
  """
@@ -104,7 +102,7 @@ class BaseRolling[T](IterWrapper[T]):
104
102
 
105
103
  ```
106
104
  """
107
- return self.apply(rolling.Var, window_size)
105
+ return self._lazy(rolling.Var, window_size)
108
106
 
109
107
  def rolling_std(self, window_size: int) -> Iter[float]:
110
108
  """
@@ -119,16 +117,14 @@ class BaseRolling[T](IterWrapper[T]):
119
117
 
120
118
  ```
121
119
  """
122
- return self.apply(rolling.Std, window_size)
120
+ return self._lazy(rolling.Std, window_size)
123
121
 
124
122
  def rolling_kurtosis(self, window_size: int) -> Iter[float]:
125
123
  """
126
124
  Compute the rolling kurtosis.
127
125
 
128
126
  Args:
129
- window_size: Size of the rolling window.
130
-
131
- The windows must have at least 4 observations.
127
+ window_size: Size of the rolling window. Must be at least 4.
132
128
  ```python
133
129
  >>> import pyochain as pc
134
130
  >>> pc.Iter.from_([1, 2, 4, 1, 4]).rolling_kurtosis(4).into(list)
@@ -136,16 +132,14 @@ class BaseRolling[T](IterWrapper[T]):
136
132
 
137
133
  ```
138
134
  """
139
- return self.apply(rolling.Kurtosis, window_size)
135
+ return self._lazy(rolling.Kurtosis, window_size)
140
136
 
141
137
  def rolling_skew(self, window_size: int) -> Iter[float]:
142
138
  """
143
139
  Compute the rolling skewness.
144
140
 
145
141
  Args:
146
- window_size: Size of the rolling window.
147
-
148
- The windows must have at least 3 observations.
142
+ window_size: Size of the rolling window. Must be at least 3.
149
143
  ```python
150
144
  >>> import pyochain as pc
151
145
  >>> pc.Iter.from_([1, 2, 4, 1, 4]).rolling_skew(3).round(2).into(list)
@@ -153,7 +147,7 @@ class BaseRolling[T](IterWrapper[T]):
153
147
 
154
148
  ```
155
149
  """
156
- return self.apply(rolling.Skew, window_size)
150
+ return self._lazy(rolling.Skew, window_size)
157
151
 
158
152
  def rolling_all(self, window_size: int) -> Iter[bool]:
159
153
  """
@@ -168,7 +162,7 @@ class BaseRolling[T](IterWrapper[T]):
168
162
 
169
163
  ```
170
164
  """
171
- return self.apply(rolling.All, window_size)
165
+ return self._lazy(rolling.All, window_size)
172
166
 
173
167
  def rolling_any(self, window_size: int) -> Iter[bool]:
174
168
  """
@@ -183,7 +177,7 @@ class BaseRolling[T](IterWrapper[T]):
183
177
 
184
178
  ```
185
179
  """
186
- return self.apply(rolling.Any, window_size)
180
+ return self._lazy(rolling.Any, window_size)
187
181
 
188
182
  def rolling_product(self, window_size: int) -> Iter[float]:
189
183
  """
@@ -198,7 +192,7 @@ class BaseRolling[T](IterWrapper[T]):
198
192
 
199
193
  ```
200
194
  """
201
- return self.apply(rolling.Product, window_size)
195
+ return self._lazy(rolling.Product, window_size)
202
196
 
203
197
  def rolling_apply[R](
204
198
  self, func: Callable[[Iterable[T]], R], window_size: int
@@ -206,11 +200,11 @@ class BaseRolling[T](IterWrapper[T]):
206
200
  """
207
201
  Apply a custom function to each rolling window.
208
202
 
203
+ The function should accept an iterable and return a single value.
204
+
209
205
  Args:
210
206
  func: Function to apply to each rolling window.
211
207
  window_size: Size of the rolling window.
212
-
213
- The function should accept an iterable and return a single value.
214
208
  ```python
215
209
  >>> import pyochain as pc
216
210
  >>> def range_func(window):
@@ -220,7 +214,7 @@ class BaseRolling[T](IterWrapper[T]):
220
214
 
221
215
  ```
222
216
  """
223
- return self.apply(rolling.Apply, window_size, "fixed", func)
217
+ return self._lazy(rolling.Apply, window_size, "fixed", func)
224
218
 
225
219
  def rolling_apply_pairwise[R](
226
220
  self, other: Iterable[T], func: Callable[[T, T], R], window_size: int
@@ -228,12 +222,12 @@ class BaseRolling[T](IterWrapper[T]):
228
222
  """
229
223
  Apply a custom pairwise function to each rolling window of size 2.
230
224
 
225
+ The function should accept two arguments and return a single value.
226
+
231
227
  Args:
232
228
  other: Second iterable to apply the pairwise function.
233
229
  func: Function to apply to each pair of elements.
234
230
  window_size: Size of the rolling window.
235
-
236
- The function should accept two arguments and return a single value.
237
231
  ```python
238
232
  >>> import pyochain as pc
239
233
  >>> from statistics import correlation as corr
@@ -244,4 +238,4 @@ class BaseRolling[T](IterWrapper[T]):
244
238
 
245
239
  ```
246
240
  """
247
- return self.apply(rolling.ApplyPairwise, other, window_size, func)
241
+ return self._lazy(rolling.ApplyPairwise, other, window_size, func)