Add keyword arg support to itertools.compress().

This commit is contained in:
Raymond Hettinger 2009-02-19 02:15:14 +00:00
parent 08259e8f25
commit 2e2909f584
2 changed files with 5 additions and 6 deletions

View File

@ -306,6 +306,7 @@ class TestBasicOps(unittest.TestCase):
self.assertEqual(comb, sorted(set(cwr) & set(perm))) # comb: both a cwr and a perm self.assertEqual(comb, sorted(set(cwr) & set(perm))) # comb: both a cwr and a perm
def test_compress(self): def test_compress(self):
self.assertEqual(list(compress(data='ABCDEF', selectors=[1,0,1,0,1,1])), list('ACEF'))
self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF')) self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF'))
self.assertEqual(list(compress('ABCDEF', [0,0,0,0,0,0])), list('')) self.assertEqual(list(compress('ABCDEF', [0,0,0,0,0,0])), list(''))
self.assertEqual(list(compress('ABCDEF', [1,1,1,1,1,1])), list('ABCDEF')) self.assertEqual(list(compress('ABCDEF', [1,1,1,1,1,1])), list('ABCDEF'))

View File

@ -2782,11 +2782,9 @@ compress_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyObject *seq1, *seq2; PyObject *seq1, *seq2;
PyObject *data=NULL, *selectors=NULL; PyObject *data=NULL, *selectors=NULL;
compressobject *lz; compressobject *lz;
static char *kwargs[] = {"data", "selectors", NULL};
if (type == &compress_type && !_PyArg_NoKeywords("compress()", kwds)) if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:compress", kwargs, &seq1, &seq2))
return NULL;
if (!PyArg_UnpackTuple(args, "compress", 2, 2, &seq1, &seq2))
return NULL; return NULL;
data = PyObject_GetIter(seq1); data = PyObject_GetIter(seq1);
@ -2864,7 +2862,7 @@ compress_next(compressobject *lz)
} }
PyDoc_STRVAR(compress_doc, PyDoc_STRVAR(compress_doc,
"compress(data sequence, selector sequence) --> iterator over selected data\n\ "compress(data, selectors) --> iterator over selected data\n\
\n\ \n\
Return data elements corresponding to true selector elements.\n\ Return data elements corresponding to true selector elements.\n\
Forms a shorter iterator from selected data elements using the\n\ Forms a shorter iterator from selected data elements using the\n\