blob: 476adc1f5c5d7ad28a2577111647b18f5ea5ad1f [file] [log] [blame]
Tal Einat3286ce42018-09-10 21:33:08 +03001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(itertools_groupby__doc__,
6"groupby(iterable, key=None)\n"
7"--\n"
8"\n"
9"make an iterator that returns consecutive keys and groups from the iterable\n"
10"\n"
11" iterable\n"
12" Elements to divide into groups according to the key function.\n"
13" key\n"
14" A function for computing the group category for each element.\n"
15" If the key function is not specified or is None, the element itself\n"
16" is used for grouping.");
17
18static PyObject *
19itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc);
20
21static PyObject *
22itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs)
23{
24 PyObject *return_value = NULL;
25 static const char * const _keywords[] = {"iterable", "key", NULL};
26 static _PyArg_Parser _parser = {"O|O:groupby", _keywords, 0};
27 PyObject *it;
28 PyObject *keyfunc = Py_None;
29
30 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
31 &it, &keyfunc)) {
32 goto exit;
33 }
34 return_value = itertools_groupby_impl(type, it, keyfunc);
35
36exit:
37 return return_value;
38}
39
40static PyObject *
41itertools__grouper_impl(PyTypeObject *type, PyObject *parent,
42 PyObject *tgtkey);
43
44static PyObject *
45itertools__grouper(PyTypeObject *type, PyObject *args, PyObject *kwargs)
46{
47 PyObject *return_value = NULL;
48 PyObject *parent;
49 PyObject *tgtkey;
50
51 if ((type == &_grouper_type) &&
52 !_PyArg_NoKeywords("_grouper", kwargs)) {
53 goto exit;
54 }
55 if (!PyArg_ParseTuple(args, "O!O:_grouper",
56 &groupby_type, &parent, &tgtkey)) {
57 goto exit;
58 }
59 return_value = itertools__grouper_impl(type, parent, tgtkey);
60
61exit:
62 return return_value;
63}
Tal Einatc4bccd32018-09-12 00:49:13 +030064
65PyDoc_STRVAR(itertools_teedataobject__doc__,
66"teedataobject(iterable, values, next, /)\n"
67"--\n"
68"\n"
69"Data container common to multiple tee objects.");
70
71static PyObject *
72itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
73 PyObject *values, PyObject *next);
74
75static PyObject *
76itertools_teedataobject(PyTypeObject *type, PyObject *args, PyObject *kwargs)
77{
78 PyObject *return_value = NULL;
79 PyObject *it;
80 PyObject *values;
81 PyObject *next;
82
83 if ((type == &teedataobject_type) &&
84 !_PyArg_NoKeywords("teedataobject", kwargs)) {
85 goto exit;
86 }
87 if (!PyArg_ParseTuple(args, "OO!O:teedataobject",
88 &it, &PyList_Type, &values, &next)) {
89 goto exit;
90 }
91 return_value = itertools_teedataobject_impl(type, it, values, next);
92
93exit:
94 return return_value;
95}
96
97PyDoc_STRVAR(itertools__tee__doc__,
98"_tee(iterable, /)\n"
99"--\n"
100"\n"
101"Iterator wrapped to make it copyable.");
102
103static PyObject *
104itertools__tee_impl(PyTypeObject *type, PyObject *iterable);
105
106static PyObject *
107itertools__tee(PyTypeObject *type, PyObject *args, PyObject *kwargs)
108{
109 PyObject *return_value = NULL;
110 PyObject *iterable;
111
112 if ((type == &tee_type) &&
113 !_PyArg_NoKeywords("_tee", kwargs)) {
114 goto exit;
115 }
116 if (!PyArg_UnpackTuple(args, "_tee",
117 1, 1,
118 &iterable)) {
119 goto exit;
120 }
121 return_value = itertools__tee_impl(type, iterable);
122
123exit:
124 return return_value;
125}
126
127PyDoc_STRVAR(itertools_tee__doc__,
128"tee($module, iterable, n=2, /)\n"
129"--\n"
130"\n"
131"Returns a tuple of n independent iterators.");
132
133#define ITERTOOLS_TEE_METHODDEF \
134 {"tee", (PyCFunction)itertools_tee, METH_FASTCALL, itertools_tee__doc__},
135
136static PyObject *
137itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n);
138
139static PyObject *
140itertools_tee(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
141{
142 PyObject *return_value = NULL;
143 PyObject *iterable;
144 Py_ssize_t n = 2;
145
146 if (!_PyArg_ParseStack(args, nargs, "O|n:tee",
147 &iterable, &n)) {
148 goto exit;
149 }
150 return_value = itertools_tee_impl(module, iterable, n);
151
152exit:
153 return return_value;
154}
155
156PyDoc_STRVAR(itertools_cycle__doc__,
157"cycle(iterable, /)\n"
158"--\n"
159"\n"
160"Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely.");
161
162static PyObject *
163itertools_cycle_impl(PyTypeObject *type, PyObject *iterable);
164
165static PyObject *
166itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs)
167{
168 PyObject *return_value = NULL;
169 PyObject *iterable;
170
171 if ((type == &cycle_type) &&
172 !_PyArg_NoKeywords("cycle", kwargs)) {
173 goto exit;
174 }
175 if (!PyArg_UnpackTuple(args, "cycle",
176 1, 1,
177 &iterable)) {
178 goto exit;
179 }
180 return_value = itertools_cycle_impl(type, iterable);
181
182exit:
183 return return_value;
184}
185
186PyDoc_STRVAR(itertools_dropwhile__doc__,
187"dropwhile(predicate, iterable, /)\n"
188"--\n"
189"\n"
190"Drop items from the iterable while predicate(item) is true.\n"
191"\n"
192"Afterwards, return every element until the iterable is exhausted.");
193
194static PyObject *
195itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
196
197static PyObject *
198itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
199{
200 PyObject *return_value = NULL;
201 PyObject *func;
202 PyObject *seq;
203
204 if ((type == &dropwhile_type) &&
205 !_PyArg_NoKeywords("dropwhile", kwargs)) {
206 goto exit;
207 }
208 if (!PyArg_UnpackTuple(args, "dropwhile",
209 2, 2,
210 &func, &seq)) {
211 goto exit;
212 }
213 return_value = itertools_dropwhile_impl(type, func, seq);
214
215exit:
216 return return_value;
217}
218
219PyDoc_STRVAR(itertools_takewhile__doc__,
220"takewhile(predicate, iterable, /)\n"
221"--\n"
222"\n"
223"Return successive entries from an iterable as long as the predicate evaluates to true for each entry.");
224
225static PyObject *
226itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
227
228static PyObject *
229itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
230{
231 PyObject *return_value = NULL;
232 PyObject *func;
233 PyObject *seq;
234
235 if ((type == &takewhile_type) &&
236 !_PyArg_NoKeywords("takewhile", kwargs)) {
237 goto exit;
238 }
239 if (!PyArg_UnpackTuple(args, "takewhile",
240 2, 2,
241 &func, &seq)) {
242 goto exit;
243 }
244 return_value = itertools_takewhile_impl(type, func, seq);
245
246exit:
247 return return_value;
248}
249
250PyDoc_STRVAR(itertools_starmap__doc__,
251"starmap(function, iterable, /)\n"
252"--\n"
253"\n"
254"Return an iterator whose values are returned from the function evaluated with an argument tuple taken from the given sequence.");
255
256static PyObject *
257itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
258
259static PyObject *
260itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs)
261{
262 PyObject *return_value = NULL;
263 PyObject *func;
264 PyObject *seq;
265
266 if ((type == &starmap_type) &&
267 !_PyArg_NoKeywords("starmap", kwargs)) {
268 goto exit;
269 }
270 if (!PyArg_UnpackTuple(args, "starmap",
271 2, 2,
272 &func, &seq)) {
273 goto exit;
274 }
275 return_value = itertools_starmap_impl(type, func, seq);
276
277exit:
278 return return_value;
279}
280
281PyDoc_STRVAR(itertools_chain_from_iterable__doc__,
282"from_iterable($type, iterable, /)\n"
283"--\n"
284"\n"
285"Alternative chain() constructor taking a single iterable argument that evaluates lazily.");
286
287#define ITERTOOLS_CHAIN_FROM_ITERABLE_METHODDEF \
288 {"from_iterable", (PyCFunction)itertools_chain_from_iterable, METH_O|METH_CLASS, itertools_chain_from_iterable__doc__},
289
290PyDoc_STRVAR(itertools_combinations__doc__,
291"combinations(iterable, r)\n"
292"--\n"
293"\n"
294"Return successive r-length combinations of elements in the iterable.\n"
295"\n"
296"combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)");
297
298static PyObject *
299itertools_combinations_impl(PyTypeObject *type, PyObject *iterable,
300 Py_ssize_t r);
301
302static PyObject *
303itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
304{
305 PyObject *return_value = NULL;
306 static const char * const _keywords[] = {"iterable", "r", NULL};
307 static _PyArg_Parser _parser = {"On:combinations", _keywords, 0};
308 PyObject *iterable;
309 Py_ssize_t r;
310
311 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
312 &iterable, &r)) {
313 goto exit;
314 }
315 return_value = itertools_combinations_impl(type, iterable, r);
316
317exit:
318 return return_value;
319}
320
321PyDoc_STRVAR(itertools_combinations_with_replacement__doc__,
322"combinations_with_replacement(iterable, r)\n"
323"--\n"
324"\n"
325"Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.\n"
326"\n"
327"combinations_with_replacement(\'ABC\', 2) --> AA AB AC BB BC CC\"");
328
329static PyObject *
330itertools_combinations_with_replacement_impl(PyTypeObject *type,
331 PyObject *iterable,
332 Py_ssize_t r);
333
334static PyObject *
335itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs)
336{
337 PyObject *return_value = NULL;
338 static const char * const _keywords[] = {"iterable", "r", NULL};
339 static _PyArg_Parser _parser = {"On:combinations_with_replacement", _keywords, 0};
340 PyObject *iterable;
341 Py_ssize_t r;
342
343 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
344 &iterable, &r)) {
345 goto exit;
346 }
347 return_value = itertools_combinations_with_replacement_impl(type, iterable, r);
348
349exit:
350 return return_value;
351}
352
353PyDoc_STRVAR(itertools_permutations__doc__,
354"permutations(iterable, r=None)\n"
355"--\n"
356"\n"
357"Return successive r-length permutations of elements in the iterable.\n"
358"\n"
359"permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)");
360
361static PyObject *
362itertools_permutations_impl(PyTypeObject *type, PyObject *iterable,
363 PyObject *robj);
364
365static PyObject *
366itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
367{
368 PyObject *return_value = NULL;
369 static const char * const _keywords[] = {"iterable", "r", NULL};
370 static _PyArg_Parser _parser = {"O|O:permutations", _keywords, 0};
371 PyObject *iterable;
372 PyObject *robj = Py_None;
373
374 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
375 &iterable, &robj)) {
376 goto exit;
377 }
378 return_value = itertools_permutations_impl(type, iterable, robj);
379
380exit:
381 return return_value;
382}
383
384PyDoc_STRVAR(itertools_accumulate__doc__,
Lisa Roach9718b592018-09-23 17:34:59 -0700385"accumulate(iterable, func=None, *, initial=None)\n"
Tal Einatc4bccd32018-09-12 00:49:13 +0300386"--\n"
387"\n"
388"Return series of accumulated sums (or other binary function results).");
389
390static PyObject *
391itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable,
Lisa Roach9718b592018-09-23 17:34:59 -0700392 PyObject *binop, PyObject *initial);
Tal Einatc4bccd32018-09-12 00:49:13 +0300393
394static PyObject *
395itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs)
396{
397 PyObject *return_value = NULL;
Lisa Roach9718b592018-09-23 17:34:59 -0700398 static const char * const _keywords[] = {"iterable", "func", "initial", NULL};
399 static _PyArg_Parser _parser = {"O|O$O:accumulate", _keywords, 0};
Tal Einatc4bccd32018-09-12 00:49:13 +0300400 PyObject *iterable;
401 PyObject *binop = Py_None;
Lisa Roach9718b592018-09-23 17:34:59 -0700402 PyObject *initial = Py_None;
Tal Einatc4bccd32018-09-12 00:49:13 +0300403
404 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
Lisa Roach9718b592018-09-23 17:34:59 -0700405 &iterable, &binop, &initial)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300406 goto exit;
407 }
Lisa Roach9718b592018-09-23 17:34:59 -0700408 return_value = itertools_accumulate_impl(type, iterable, binop, initial);
Tal Einatc4bccd32018-09-12 00:49:13 +0300409
410exit:
411 return return_value;
412}
413
414PyDoc_STRVAR(itertools_compress__doc__,
415"compress(data, selectors)\n"
416"--\n"
417"\n"
418"Return data elements corresponding to true selector elements.\n"
419"\n"
420"Forms a shorter iterator from selected data elements using the selectors to\n"
421"choose the data elements.");
422
423static PyObject *
424itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2);
425
426static PyObject *
427itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs)
428{
429 PyObject *return_value = NULL;
430 static const char * const _keywords[] = {"data", "selectors", NULL};
431 static _PyArg_Parser _parser = {"OO:compress", _keywords, 0};
432 PyObject *seq1;
433 PyObject *seq2;
434
435 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
436 &seq1, &seq2)) {
437 goto exit;
438 }
439 return_value = itertools_compress_impl(type, seq1, seq2);
440
441exit:
442 return return_value;
443}
444
445PyDoc_STRVAR(itertools_filterfalse__doc__,
446"filterfalse(function, iterable, /)\n"
447"--\n"
448"\n"
449"Return those items of iterable for which function(item) is false.\n"
450"\n"
451"If function is None, return the items that are false.");
452
453static PyObject *
454itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
455
456static PyObject *
457itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs)
458{
459 PyObject *return_value = NULL;
460 PyObject *func;
461 PyObject *seq;
462
463 if ((type == &filterfalse_type) &&
464 !_PyArg_NoKeywords("filterfalse", kwargs)) {
465 goto exit;
466 }
467 if (!PyArg_UnpackTuple(args, "filterfalse",
468 2, 2,
469 &func, &seq)) {
470 goto exit;
471 }
472 return_value = itertools_filterfalse_impl(type, func, seq);
473
474exit:
475 return return_value;
476}
477
478PyDoc_STRVAR(itertools_count__doc__,
479"count(start=0, step=1)\n"
480"--\n"
481"\n"
482"Return a count object whose .__next__() method returns consecutive values.\n"
483"\n"
484"Equivalent to:\n"
485" def count(firstval=0, step=1):\n"
486" x = firstval\n"
487" while 1:\n"
488" yield x\n"
489" x += step");
490
491static PyObject *
492itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
493 PyObject *long_step);
494
495static PyObject *
496itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs)
497{
498 PyObject *return_value = NULL;
499 static const char * const _keywords[] = {"start", "step", NULL};
500 static _PyArg_Parser _parser = {"|OO:count", _keywords, 0};
501 PyObject *long_cnt = NULL;
502 PyObject *long_step = NULL;
503
504 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
505 &long_cnt, &long_step)) {
506 goto exit;
507 }
508 return_value = itertools_count_impl(type, long_cnt, long_step);
509
510exit:
511 return return_value;
512}
Lisa Roach9718b592018-09-23 17:34:59 -0700513/*[clinic end generated code: output=c8c47b766deeffc3 input=a9049054013a1b77]*/