7
7
by Raymond D. Hettinger <[email protected] >
8
8
*/
9
9
10
+ /*[clinic input]
11
+ module itertools
12
+ class itertools.groupby "groupbyobject *" "&groupby_type"
13
+ class itertools._grouper "_grouperobject *" "&_grouper_type"
14
+ [clinic start generated code]*/
15
+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9d506f5bb9177570]*/
16
+
10
17
11
18
/* groupby object ************************************************************/
12
19
@@ -23,26 +30,34 @@ typedef struct {
23
30
static PyTypeObject groupby_type ;
24
31
static PyObject * _grouper_create (groupbyobject * , PyObject * );
25
32
33
+ /*[clinic input]
34
+ @classmethod
35
+ itertools.groupby.__new__
36
+
37
+ iterable: object
38
+ Elements to divide into groups according to the key function.
39
+ key: object = None
40
+ A function computing a key value for each element. If None, key
41
+ defaults to an identity function and returns the element unchanged.
42
+
43
+ make an iterator that returns consecutive keys and groups from the iterable
44
+ [clinic start generated code]*/
45
+
26
46
static PyObject *
27
- groupby_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
47
+ itertools_groupby_impl (PyTypeObject * type , PyObject * iterable , PyObject * key )
48
+ /*[clinic end generated code: output=83016d6c995ed8a5 input=dc17e1c0520e528f]*/
28
49
{
29
- static char * kwargs [] = {"iterable" , "key" , NULL };
30
50
groupbyobject * gbo ;
31
- PyObject * it , * keyfunc = Py_None ;
32
-
33
- if (!PyArg_ParseTupleAndKeywords (args , kwds , "O|O:groupby" , kwargs ,
34
- & it , & keyfunc ))
35
- return NULL ;
36
51
37
52
gbo = (groupbyobject * )type -> tp_alloc (type , 0 );
38
53
if (gbo == NULL )
39
54
return NULL ;
40
55
gbo -> tgtkey = NULL ;
41
56
gbo -> currkey = NULL ;
42
57
gbo -> currvalue = NULL ;
43
- gbo -> keyfunc = keyfunc ;
44
- Py_INCREF (keyfunc );
45
- gbo -> it = PyObject_GetIter (it );
58
+ gbo -> keyfunc = key ;
59
+ Py_INCREF (key );
60
+ gbo -> it = PyObject_GetIter (iterable );
46
61
if (gbo -> it == NULL ) {
47
62
Py_DECREF (gbo );
48
63
return NULL ;
@@ -178,63 +193,6 @@ groupby_setstate(groupbyobject *lz, PyObject *state)
178
193
179
194
PyDoc_STRVAR (setstate_doc , "Set state information for unpickling." );
180
195
181
- static PyMethodDef groupby_methods [] = {
182
- {"__reduce__" , (PyCFunction )groupby_reduce , METH_NOARGS ,
183
- reduce_doc },
184
- {"__setstate__" , (PyCFunction )groupby_setstate , METH_O ,
185
- setstate_doc },
186
- {NULL , NULL } /* sentinel */
187
- };
188
-
189
- PyDoc_STRVAR (groupby_doc ,
190
- "groupby(iterable, key=None) -> make an iterator that returns consecutive\n\
191
- keys and groups from the iterable. If the key function is not specified or\n\
192
- is None, the element itself is used for grouping.\n" );
193
-
194
- static PyTypeObject groupby_type = {
195
- PyVarObject_HEAD_INIT (NULL , 0 )
196
- "itertools.groupby" , /* tp_name */
197
- sizeof (groupbyobject ), /* tp_basicsize */
198
- 0 , /* tp_itemsize */
199
- /* methods */
200
- (destructor )groupby_dealloc , /* tp_dealloc */
201
- 0 , /* tp_print */
202
- 0 , /* tp_getattr */
203
- 0 , /* tp_setattr */
204
- 0 , /* tp_reserved */
205
- 0 , /* tp_repr */
206
- 0 , /* tp_as_number */
207
- 0 , /* tp_as_sequence */
208
- 0 , /* tp_as_mapping */
209
- 0 , /* tp_hash */
210
- 0 , /* tp_call */
211
- 0 , /* tp_str */
212
- PyObject_GenericGetAttr , /* tp_getattro */
213
- 0 , /* tp_setattro */
214
- 0 , /* tp_as_buffer */
215
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
216
- Py_TPFLAGS_BASETYPE , /* tp_flags */
217
- groupby_doc , /* tp_doc */
218
- (traverseproc )groupby_traverse , /* tp_traverse */
219
- 0 , /* tp_clear */
220
- 0 , /* tp_richcompare */
221
- 0 , /* tp_weaklistoffset */
222
- PyObject_SelfIter , /* tp_iter */
223
- (iternextfunc )groupby_next , /* tp_iternext */
224
- groupby_methods , /* tp_methods */
225
- 0 , /* tp_members */
226
- 0 , /* tp_getset */
227
- 0 , /* tp_base */
228
- 0 , /* tp_dict */
229
- 0 , /* tp_descr_get */
230
- 0 , /* tp_descr_set */
231
- 0 , /* tp_dictoffset */
232
- 0 , /* tp_init */
233
- 0 , /* tp_alloc */
234
- groupby_new , /* tp_new */
235
- PyObject_GC_Del , /* tp_free */
236
- };
237
-
238
196
239
197
/* _grouper object (internal) ************************************************/
240
198
@@ -246,14 +204,20 @@ typedef struct {
246
204
247
205
static PyTypeObject _grouper_type ;
248
206
249
- static PyObject *
250
- _grouper_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
251
- {
252
- PyObject * parent , * tgtkey ;
207
+ /*[clinic input]
208
+ @classmethod
209
+ itertools._grouper.__new__
253
210
254
- if (!PyArg_ParseTuple (args , "O!O" , & groupby_type , & parent , & tgtkey ))
255
- return NULL ;
211
+ parent: object(subclass_of='&groupby_type')
212
+ tgtkey: object
213
+ /
214
+ [clinic start generated code]*/
256
215
216
+ static PyObject *
217
+ itertools__grouper_impl (PyTypeObject * type , PyObject * parent ,
218
+ PyObject * tgtkey )
219
+ /*[clinic end generated code: output=462efb1cdebb5914 input=dc180d7771fc8c59]*/
220
+ {
257
221
return _grouper_create ((groupbyobject * ) parent , tgtkey );
258
222
}
259
223
@@ -328,56 +292,6 @@ _grouper_reduce(_grouperobject *lz, PyObject *Py_UNUSED(ignored))
328
292
return Py_BuildValue ("O(OO)" , Py_TYPE (lz ), lz -> parent , lz -> tgtkey );
329
293
}
330
294
331
- static PyMethodDef _grouper_methods [] = {
332
- {"__reduce__" , (PyCFunction )_grouper_reduce , METH_NOARGS ,
333
- reduce_doc },
334
- {NULL , NULL } /* sentinel */
335
- };
336
-
337
-
338
- static PyTypeObject _grouper_type = {
339
- PyVarObject_HEAD_INIT (NULL , 0 )
340
- "itertools._grouper" , /* tp_name */
341
- sizeof (_grouperobject ), /* tp_basicsize */
342
- 0 , /* tp_itemsize */
343
- /* methods */
344
- (destructor )_grouper_dealloc , /* tp_dealloc */
345
- 0 , /* tp_print */
346
- 0 , /* tp_getattr */
347
- 0 , /* tp_setattr */
348
- 0 , /* tp_reserved */
349
- 0 , /* tp_repr */
350
- 0 , /* tp_as_number */
351
- 0 , /* tp_as_sequence */
352
- 0 , /* tp_as_mapping */
353
- 0 , /* tp_hash */
354
- 0 , /* tp_call */
355
- 0 , /* tp_str */
356
- PyObject_GenericGetAttr , /* tp_getattro */
357
- 0 , /* tp_setattro */
358
- 0 , /* tp_as_buffer */
359
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC , /* tp_flags */
360
- 0 , /* tp_doc */
361
- (traverseproc )_grouper_traverse , /* tp_traverse */
362
- 0 , /* tp_clear */
363
- 0 , /* tp_richcompare */
364
- 0 , /* tp_weaklistoffset */
365
- PyObject_SelfIter , /* tp_iter */
366
- (iternextfunc )_grouper_next , /* tp_iternext */
367
- _grouper_methods , /* tp_methods */
368
- 0 , /* tp_members */
369
- 0 , /* tp_getset */
370
- 0 , /* tp_base */
371
- 0 , /* tp_dict */
372
- 0 , /* tp_descr_get */
373
- 0 , /* tp_descr_set */
374
- 0 , /* tp_dictoffset */
375
- 0 , /* tp_init */
376
- 0 , /* tp_alloc */
377
- _grouper_new , /* tp_new */
378
- PyObject_GC_Del , /* tp_free */
379
- };
380
-
381
295
382
296
/* tee object and with supporting function and objects ***********************/
383
297
@@ -4601,6 +4515,117 @@ static PyTypeObject ziplongest_type = {
4601
4515
PyObject_GC_Del , /* tp_free */
4602
4516
};
4603
4517
4518
+
4519
+ #include "clinic/itertoolsmodule.c.h"
4520
+
4521
+
4522
+ /* groupby object method and type definitons ********************************/
4523
+
4524
+ static PyMethodDef groupby_methods [] = {
4525
+ {"__reduce__" , (PyCFunction )groupby_reduce , METH_NOARGS ,
4526
+ reduce_doc },
4527
+ {"__setstate__" , (PyCFunction )groupby_setstate , METH_O ,
4528
+ setstate_doc },
4529
+ {NULL , NULL } /* sentinel */
4530
+ };
4531
+
4532
+ static PyTypeObject groupby_type = {
4533
+ PyVarObject_HEAD_INIT (NULL , 0 )
4534
+ "itertools.groupby" , /* tp_name */
4535
+ sizeof (groupbyobject ), /* tp_basicsize */
4536
+ 0 , /* tp_itemsize */
4537
+ /* methods */
4538
+ (destructor )groupby_dealloc , /* tp_dealloc */
4539
+ 0 , /* tp_print */
4540
+ 0 , /* tp_getattr */
4541
+ 0 , /* tp_setattr */
4542
+ 0 , /* tp_reserved */
4543
+ 0 , /* tp_repr */
4544
+ 0 , /* tp_as_number */
4545
+ 0 , /* tp_as_sequence */
4546
+ 0 , /* tp_as_mapping */
4547
+ 0 , /* tp_hash */
4548
+ 0 , /* tp_call */
4549
+ 0 , /* tp_str */
4550
+ PyObject_GenericGetAttr , /* tp_getattro */
4551
+ 0 , /* tp_setattro */
4552
+ 0 , /* tp_as_buffer */
4553
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
4554
+ Py_TPFLAGS_BASETYPE , /* tp_flags */
4555
+ itertools_groupby__doc__ , /* tp_doc */
4556
+ (traverseproc )groupby_traverse , /* tp_traverse */
4557
+ 0 , /* tp_clear */
4558
+ 0 , /* tp_richcompare */
4559
+ 0 , /* tp_weaklistoffset */
4560
+ PyObject_SelfIter , /* tp_iter */
4561
+ (iternextfunc )groupby_next , /* tp_iternext */
4562
+ groupby_methods , /* tp_methods */
4563
+ 0 , /* tp_members */
4564
+ 0 , /* tp_getset */
4565
+ 0 , /* tp_base */
4566
+ 0 , /* tp_dict */
4567
+ 0 , /* tp_descr_get */
4568
+ 0 , /* tp_descr_set */
4569
+ 0 , /* tp_dictoffset */
4570
+ 0 , /* tp_init */
4571
+ 0 , /* tp_alloc */
4572
+ itertools_groupby , /* tp_new */
4573
+ PyObject_GC_Del , /* tp_free */
4574
+ };
4575
+
4576
+
4577
+ /* _grouper object (internal) method and type definitons *********************/
4578
+
4579
+ static PyMethodDef _grouper_methods [] = {
4580
+ {"__reduce__" , (PyCFunction )_grouper_reduce , METH_NOARGS ,
4581
+ reduce_doc },
4582
+ {NULL , NULL } /* sentinel */
4583
+ };
4584
+
4585
+ static PyTypeObject _grouper_type = {
4586
+ PyVarObject_HEAD_INIT (NULL , 0 )
4587
+ "itertools._grouper" , /* tp_name */
4588
+ sizeof (_grouperobject ), /* tp_basicsize */
4589
+ 0 , /* tp_itemsize */
4590
+ /* methods */
4591
+ (destructor )_grouper_dealloc , /* tp_dealloc */
4592
+ 0 , /* tp_print */
4593
+ 0 , /* tp_getattr */
4594
+ 0 , /* tp_setattr */
4595
+ 0 , /* tp_reserved */
4596
+ 0 , /* tp_repr */
4597
+ 0 , /* tp_as_number */
4598
+ 0 , /* tp_as_sequence */
4599
+ 0 , /* tp_as_mapping */
4600
+ 0 , /* tp_hash */
4601
+ 0 , /* tp_call */
4602
+ 0 , /* tp_str */
4603
+ PyObject_GenericGetAttr , /* tp_getattro */
4604
+ 0 , /* tp_setattro */
4605
+ 0 , /* tp_as_buffer */
4606
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC , /* tp_flags */
4607
+ 0 , /* tp_doc */
4608
+ (traverseproc )_grouper_traverse , /* tp_traverse */
4609
+ 0 , /* tp_clear */
4610
+ 0 , /* tp_richcompare */
4611
+ 0 , /* tp_weaklistoffset */
4612
+ PyObject_SelfIter , /* tp_iter */
4613
+ (iternextfunc )_grouper_next , /* tp_iternext */
4614
+ _grouper_methods , /* tp_methods */
4615
+ 0 , /* tp_members */
4616
+ 0 , /* tp_getset */
4617
+ 0 , /* tp_base */
4618
+ 0 , /* tp_dict */
4619
+ 0 , /* tp_descr_get */
4620
+ 0 , /* tp_descr_set */
4621
+ 0 , /* tp_dictoffset */
4622
+ 0 , /* tp_init */
4623
+ 0 , /* tp_alloc */
4624
+ itertools__grouper , /* tp_new */
4625
+ PyObject_GC_Del , /* tp_free */
4626
+ };
4627
+
4628
+
4604
4629
/* module level code ********************************************************/
4605
4630
4606
4631
PyDoc_STRVAR (module_doc ,
0 commit comments