Rename ifilterfalse() to filterfalse() and izip_longest() to zip_longest().

This commit is contained in:
Raymond Hettinger 2008-03-13 01:41:43 +00:00
parent a6c6037f88
commit b0002d2a07
4 changed files with 93 additions and 93 deletions

View File

@ -233,13 +233,13 @@ loops that truncate the stream.
self.currkey = self.keyfunc(self.currvalue)
.. function:: ifilterfalse(predicate, iterable)
.. function:: filterfalse(predicate, iterable)
Make an iterator that filters elements from iterable returning only those for
which the predicate is ``False``. If *predicate* is ``None``, return the items
that are false. Equivalent to::
def ifilterfalse(predicate, iterable):
def filterfalse(predicate, iterable):
if predicate is None:
predicate = bool
for x in iterable:
@ -292,16 +292,16 @@ loops that truncate the stream.
:func:`izip` should only be used with unequal length inputs when you don't
care about trailing, unmatched values from the longer iterables. If those
values are important, use :func:`izip_longest` instead.
values are important, use :func:`zip_longest` instead.
.. function:: izip_longest(*iterables[, fillvalue])
.. function:: zip_longest(*iterables[, fillvalue])
Make an iterator that aggregates elements from each of the iterables. If the
iterables are of uneven length, missing values are filled-in with *fillvalue*.
Iteration continues until the longest iterable is exhausted. Equivalent to::
def izip_longest(*args, **kwds):
def zip_longest(*args, **kwds):
fillvalue = kwds.get('fillvalue')
def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
yield counter() # yields the fillvalue, or raises IndexError
@ -313,7 +313,7 @@ loops that truncate the stream.
except IndexError:
pass
If one of the iterables is potentially infinite, then the :func:`izip_longest`
If one of the iterables is potentially infinite, then the :func:`zip_longest`
function should be wrapped with something that limits the number of calls (for
example :func:`islice` or :func:`takewhile`).
@ -568,7 +568,7 @@ which incur interpreter overhead. ::
def all(seq, pred=None):
"Returns True if pred(x) is true for every element in the iterable"
for elem in ifilterfalse(pred, seq):
for elem in filterfalse(pred, seq):
return False
return True

View File

@ -12,7 +12,7 @@ Functions:
import os
import stat
import warnings
from itertools import ifilterfalse, izip
from itertools import filterfalse, izip
__all__ = ["cmp","dircmp","cmpfiles"]
@ -133,8 +133,8 @@ class dircmp:
a = dict(izip(map(os.path.normcase, self.left_list), self.left_list))
b = dict(izip(map(os.path.normcase, self.right_list), self.right_list))
self.common = list(map(a.__getitem__, filter(b.__contains__, a)))
self.left_only = list(map(a.__getitem__, ifilterfalse(b.__contains__, a)))
self.right_only = list(map(b.__getitem__, ifilterfalse(a.__contains__, b)))
self.left_only = list(map(a.__getitem__, filterfalse(b.__contains__, a)))
self.right_only = list(map(b.__getitem__, filterfalse(a.__contains__, b)))
def phase2(self): # Distinguish files, directories, funnies
self.common_dirs = []
@ -276,7 +276,7 @@ def _cmp(a, b, sh, abs=abs, cmp=cmp):
# Return a copy with items that occur in skip removed.
#
def _filter(flist, skip):
return list(ifilterfalse(skip.__contains__, flist))
return list(filterfalse(skip.__contains__, flist))
# Demonstration and testing.

View File

@ -324,16 +324,16 @@ class TestBasicOps(unittest.TestCase):
self.assertRaises(TypeError, ifilter, isEven, 3)
self.assertRaises(TypeError, next, ifilter(range(6), range(6)))
def test_ifilterfalse(self):
self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5])
self.assertEqual(list(ifilterfalse(None, [0,1,0,2,0])), [0,0,0])
self.assertEqual(list(ifilterfalse(bool, [0,1,0,2,0])), [0,0,0])
self.assertEqual(take(4, ifilterfalse(isEven, count())), [1,3,5,7])
self.assertRaises(TypeError, ifilterfalse)
self.assertRaises(TypeError, ifilterfalse, lambda x:x)
self.assertRaises(TypeError, ifilterfalse, lambda x:x, range(6), 7)
self.assertRaises(TypeError, ifilterfalse, isEven, 3)
self.assertRaises(TypeError, next, ifilterfalse(range(6), range(6)))
def test_filterfalse(self):
self.assertEqual(list(filterfalse(isEven, range(6))), [1,3,5])
self.assertEqual(list(filterfalse(None, [0,1,0,2,0])), [0,0,0])
self.assertEqual(list(filterfalse(bool, [0,1,0,2,0])), [0,0,0])
self.assertEqual(take(4, filterfalse(isEven, count())), [1,3,5,7])
self.assertRaises(TypeError, filterfalse)
self.assertRaises(TypeError, filterfalse, lambda x:x)
self.assertRaises(TypeError, filterfalse, lambda x:x, range(6), 7)
self.assertRaises(TypeError, filterfalse, isEven, 3)
self.assertRaises(TypeError, next, filterfalse(range(6), range(6)))
def test_izip(self):
# XXX This is rather silly now that builtin zip() calls izip()...
@ -366,25 +366,25 @@ class TestBasicOps(unittest.TestCase):
]:
target = [tuple([arg[i] if i < len(arg) else None for arg in args])
for i in range(max(map(len, args)))]
self.assertEqual(list(izip_longest(*args)), target)
self.assertEqual(list(izip_longest(*args, **{})), target)
self.assertEqual(list(zip_longest(*args)), target)
self.assertEqual(list(zip_longest(*args, **{})), target)
target = [tuple((e is None and 'X' or e) for e in t) for t in target] # Replace None fills with 'X'
self.assertEqual(list(izip_longest(*args, **dict(fillvalue='X'))), target)
self.assertEqual(list(zip_longest(*args, **dict(fillvalue='X'))), target)
self.assertEqual(take(3,izip_longest('abcdef', count())), list(zip('abcdef', range(3)))) # take 3 from infinite input
self.assertEqual(take(3,zip_longest('abcdef', count())), list(zip('abcdef', range(3)))) # take 3 from infinite input
self.assertEqual(list(izip_longest()), list(zip()))
self.assertEqual(list(izip_longest([])), list(zip([])))
self.assertEqual(list(izip_longest('abcdef')), list(zip('abcdef')))
self.assertEqual(list(zip_longest()), list(zip()))
self.assertEqual(list(zip_longest([])), list(zip([])))
self.assertEqual(list(zip_longest('abcdef')), list(zip('abcdef')))
self.assertEqual(list(izip_longest('abc', 'defg', **{})),
self.assertEqual(list(zip_longest('abc', 'defg', **{})),
list(izip(list('abc')+[None], 'defg'))) # empty keyword dict
self.assertRaises(TypeError, izip_longest, 3)
self.assertRaises(TypeError, izip_longest, range(3), 3)
self.assertRaises(TypeError, zip_longest, 3)
self.assertRaises(TypeError, zip_longest, range(3), 3)
for stmt in [
"izip_longest('abc', fv=1)",
"izip_longest('abc', fillvalue=1, bogus_keyword=None)",
"zip_longest('abc', fv=1)",
"zip_longest('abc', fillvalue=1, bogus_keyword=None)",
]:
try:
eval(stmt, globals(), locals())
@ -394,13 +394,13 @@ class TestBasicOps(unittest.TestCase):
self.fail('Did not raise Type in: ' + stmt)
# Check tuple re-use (implementation detail)
self.assertEqual([tuple(list(pair)) for pair in izip_longest('abc', 'def')],
self.assertEqual([tuple(list(pair)) for pair in zip_longest('abc', 'def')],
list(zip('abc', 'def')))
self.assertEqual([pair for pair in izip_longest('abc', 'def')],
self.assertEqual([pair for pair in zip_longest('abc', 'def')],
list(zip('abc', 'def')))
ids = list(map(id, izip_longest('abc', 'def')))
ids = list(map(id, zip_longest('abc', 'def')))
self.assertEqual(min(ids), max(ids))
ids = list(map(id, list(izip_longest('abc', 'def'))))
ids = list(map(id, list(zip_longest('abc', 'def'))))
self.assertEqual(len(dict.fromkeys(ids)), len(ids))
def test_product(self):
@ -659,7 +659,7 @@ class TestBasicOps(unittest.TestCase):
self.assertRaises(StopIteration, next, repeat(None, 0))
for f in (ifilter, ifilterfalse, imap, takewhile, dropwhile, starmap):
for f in (ifilter, filterfalse, imap, takewhile, dropwhile, starmap):
self.assertRaises(StopIteration, next, f(lambda x:x, []))
self.assertRaises(StopIteration, next, f(lambda x:x, StopNow()))
@ -690,9 +690,9 @@ class TestGC(unittest.TestCase):
a = []
self.makecycle(ifilter(lambda x:True, [a]*2), a)
def test_ifilterfalse(self):
def test_filterfalse(self):
a = []
self.makecycle(ifilterfalse(lambda x:False, a), a)
self.makecycle(filterfalse(lambda x:False, a), a)
def test_izip(self):
a = []
@ -840,14 +840,14 @@ class TestVariousIteratorArgs(unittest.TestCase):
self.assertRaises(TypeError, ifilter, isEven, N(s))
self.assertRaises(ZeroDivisionError, list, ifilter(isEven, E(s)))
def test_ifilterfalse(self):
def test_filterfalse(self):
for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
for g in (G, I, Ig, S, L, R):
self.assertEqual(list(ifilterfalse(isEven, g(s))),
self.assertEqual(list(filterfalse(isEven, g(s))),
[x for x in g(s) if isOdd(x)])
self.assertRaises(TypeError, ifilterfalse, isEven, X(s))
self.assertRaises(TypeError, ifilterfalse, isEven, N(s))
self.assertRaises(ZeroDivisionError, list, ifilterfalse(isEven, E(s)))
self.assertRaises(TypeError, filterfalse, isEven, X(s))
self.assertRaises(TypeError, filterfalse, isEven, N(s))
self.assertRaises(ZeroDivisionError, list, filterfalse(isEven, E(s)))
def test_izip(self):
for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
@ -861,11 +861,11 @@ class TestVariousIteratorArgs(unittest.TestCase):
def test_iziplongest(self):
for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
for g in (G, I, Ig, S, L, R):
self.assertEqual(list(izip_longest(g(s))), list(zip(g(s))))
self.assertEqual(list(izip_longest(g(s), g(s))), list(zip(g(s), g(s))))
self.assertRaises(TypeError, izip_longest, X(s))
self.assertRaises(TypeError, izip_longest, N(s))
self.assertRaises(ZeroDivisionError, list, izip_longest(E(s)))
self.assertEqual(list(zip_longest(g(s))), list(zip(g(s))))
self.assertEqual(list(zip_longest(g(s), g(s))), list(zip(g(s), g(s))))
self.assertRaises(TypeError, zip_longest, X(s))
self.assertRaises(TypeError, zip_longest, N(s))
self.assertRaises(ZeroDivisionError, list, zip_longest(E(s)))
def test_imap(self):
for s in (range(10), range(0), range(100), (7,11), range(20,50,5)):
@ -1001,7 +1001,7 @@ class RegressionTests(unittest.TestCase):
class SubclassWithKwargsTest(unittest.TestCase):
def test_keywords_in_subclass(self):
# count is not subclassable...
for cls in (repeat, izip, ifilter, ifilterfalse, chain, imap,
for cls in (repeat, izip, ifilter, filterfalse, chain, imap,
starmap, islice, takewhile, dropwhile, cycle):
class Subclass(cls):
def __init__(self, newarg=None, *args):
@ -1085,7 +1085,7 @@ Samuele
>>> def all(seq, pred=None):
... "Returns True if pred(x) is true for every element in the iterable"
... for elem in ifilterfalse(pred, seq):
... for elem in filterfalse(pred, seq):
... return False
... return True

View File

@ -2059,28 +2059,28 @@ static PyTypeObject combinations_type = {
};
/* ifilterfalse object ************************************************************/
/* filterfalse object ************************************************************/
typedef struct {
PyObject_HEAD
PyObject *func;
PyObject *it;
} ifilterfalseobject;
} filterfalseobject;
static PyTypeObject ifilterfalse_type;
static PyTypeObject filterfalse_type;
static PyObject *
ifilterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
filterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *func, *seq;
PyObject *it;
ifilterfalseobject *lz;
filterfalseobject *lz;
if (type == &ifilterfalse_type &&
!_PyArg_NoKeywords("ifilterfalse()", kwds))
if (type == &filterfalse_type &&
!_PyArg_NoKeywords("filterfalse()", kwds))
return NULL;
if (!PyArg_UnpackTuple(args, "ifilterfalse", 2, 2, &func, &seq))
if (!PyArg_UnpackTuple(args, "filterfalse", 2, 2, &func, &seq))
return NULL;
/* Get iterator. */
@ -2088,8 +2088,8 @@ ifilterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (it == NULL)
return NULL;
/* create ifilterfalseobject structure */
lz = (ifilterfalseobject *)type->tp_alloc(type, 0);
/* create filterfalseobject structure */
lz = (filterfalseobject *)type->tp_alloc(type, 0);
if (lz == NULL) {
Py_DECREF(it);
return NULL;
@ -2102,7 +2102,7 @@ ifilterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
static void
ifilterfalse_dealloc(ifilterfalseobject *lz)
filterfalse_dealloc(filterfalseobject *lz)
{
PyObject_GC_UnTrack(lz);
Py_XDECREF(lz->func);
@ -2111,7 +2111,7 @@ ifilterfalse_dealloc(ifilterfalseobject *lz)
}
static int
ifilterfalse_traverse(ifilterfalseobject *lz, visitproc visit, void *arg)
filterfalse_traverse(filterfalseobject *lz, visitproc visit, void *arg)
{
Py_VISIT(lz->it);
Py_VISIT(lz->func);
@ -2119,7 +2119,7 @@ ifilterfalse_traverse(ifilterfalseobject *lz, visitproc visit, void *arg)
}
static PyObject *
ifilterfalse_next(ifilterfalseobject *lz)
filterfalse_next(filterfalseobject *lz)
{
PyObject *item;
PyObject *it = lz->it;
@ -2152,19 +2152,19 @@ ifilterfalse_next(ifilterfalseobject *lz)
}
}
PyDoc_STRVAR(ifilterfalse_doc,
"ifilterfalse(function or None, sequence) --> ifilterfalse object\n\
PyDoc_STRVAR(filterfalse_doc,
"filterfalse(function or None, sequence) --> filterfalse object\n\
\n\
Return those items of sequence for which function(item) is false.\n\
If function is None, return the items that are false.");
static PyTypeObject ifilterfalse_type = {
static PyTypeObject filterfalse_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"itertools.ifilterfalse", /* tp_name */
sizeof(ifilterfalseobject), /* tp_basicsize */
"itertools.filterfalse", /* tp_name */
sizeof(filterfalseobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)ifilterfalse_dealloc, /* tp_dealloc */
(destructor)filterfalse_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
@ -2181,13 +2181,13 @@ static PyTypeObject ifilterfalse_type = {
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
ifilterfalse_doc, /* tp_doc */
(traverseproc)ifilterfalse_traverse, /* tp_traverse */
filterfalse_doc, /* tp_doc */
(traverseproc)filterfalse_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)ifilterfalse_next, /* tp_iternext */
(iternextfunc)filterfalse_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
@ -2198,7 +2198,7 @@ static PyTypeObject ifilterfalse_type = {
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
ifilterfalse_new, /* tp_new */
filterfalse_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
};
@ -2691,7 +2691,7 @@ typedef struct {
static PyTypeObject iziplongest_type;
static PyObject *
izip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
iziplongestobject *lz;
Py_ssize_t i;
@ -2704,7 +2704,7 @@ izip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
fillvalue = PyDict_GetItemString(kwds, "fillvalue");
if (fillvalue == NULL || PyDict_Size(kwds) > 1) {
PyErr_SetString(PyExc_TypeError,
"izip_longest() got an unexpected keyword argument");
"zip_longest() got an unexpected keyword argument");
return NULL;
}
}
@ -2722,7 +2722,7 @@ izip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (it == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError))
PyErr_Format(PyExc_TypeError,
"izip_longest argument #%zd must support iteration",
"zip_longest argument #%zd must support iteration",
i+1);
Py_DECREF(ittuple);
return NULL;
@ -2758,7 +2758,7 @@ izip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
static void
izip_longest_dealloc(iziplongestobject *lz)
zip_longest_dealloc(iziplongestobject *lz)
{
PyObject_GC_UnTrack(lz);
Py_XDECREF(lz->ittuple);
@ -2768,7 +2768,7 @@ izip_longest_dealloc(iziplongestobject *lz)
}
static int
izip_longest_traverse(iziplongestobject *lz, visitproc visit, void *arg)
zip_longest_traverse(iziplongestobject *lz, visitproc visit, void *arg)
{
Py_VISIT(lz->ittuple);
Py_VISIT(lz->result);
@ -2777,7 +2777,7 @@ izip_longest_traverse(iziplongestobject *lz, visitproc visit, void *arg)
}
static PyObject *
izip_longest_next(iziplongestobject *lz)
zip_longest_next(iziplongestobject *lz)
{
Py_ssize_t i;
Py_ssize_t tuplesize = lz->tuplesize;
@ -2848,10 +2848,10 @@ izip_longest_next(iziplongestobject *lz)
return result;
}
PyDoc_STRVAR(izip_longest_doc,
"izip_longest(iter1 [,iter2 [...]], [fillvalue=None]) --> izip_longest object\n\
PyDoc_STRVAR(zip_longest_doc,
"zip_longest(iter1 [,iter2 [...]], [fillvalue=None]) --> zip_longest object\n\
\n\
Return an izip_longest object whose .__next__() method returns a tuple where\n\
Return an zip_longest object whose .__next__() method returns a tuple where\n\
the i-th element comes from the i-th iterable argument. The .__next__()\n\
method continues until the longest iterable in the argument sequence\n\
is exhausted and then it raises StopIteration. When the shorter iterables\n\
@ -2861,11 +2861,11 @@ defaults to None or can be specified by a keyword argument.\n\
static PyTypeObject iziplongest_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"itertools.izip_longest", /* tp_name */
"itertools.zip_longest", /* tp_name */
sizeof(iziplongestobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)izip_longest_dealloc, /* tp_dealloc */
(destructor)zip_longest_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
@ -2882,13 +2882,13 @@ static PyTypeObject iziplongest_type = {
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
izip_longest_doc, /* tp_doc */
(traverseproc)izip_longest_traverse, /* tp_traverse */
zip_longest_doc, /* tp_doc */
(traverseproc)zip_longest_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)izip_longest_next, /* tp_iternext */
(iternextfunc)zip_longest_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
@ -2899,7 +2899,7 @@ static PyTypeObject iziplongest_type = {
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
izip_longest_new, /* tp_new */
zip_longest_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
};
@ -2915,8 +2915,8 @@ repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times\n\
\n\
Iterators terminating on the shortest input sequence:\n\
izip(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\
izip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\
ifilterfalse(pred, seq) --> elements of seq where pred(elem) is False\n\
zip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\
filterfalse(pred, seq) --> elements of seq where pred(elem) is False\n\
islice(seq, [start,] stop [, step]) --> elements from\n\
seq[start:stop:step]\n\
starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\
@ -2947,7 +2947,7 @@ inititertools(void)
&islice_type,
&starmap_type,
&chain_type,
&ifilterfalse_type,
&filterfalse_type,
&count_type,
&izip_type,
&iziplongest_type,