Add two new functions, any() and all().

This commit is contained in:
Raymond Hettinger 2005-03-11 06:49:40 +00:00
parent 26e512a04f
commit 96229b1918
4 changed files with 129 additions and 0 deletions

View file

@ -60,6 +60,32 @@ def my_import(name):
complex number, its magnitude is returned. complex number, its magnitude is returned.
\end{funcdesc} \end{funcdesc}
\begin{funcdesc}{all}{iterable}
Return True if all elements of the \var{iterable} are true.
Equivalent to:
\begin{verbatim}
def all(iterable):
for element in iterable:
if not element:
return False
return True
\end{verbatim}
\versionadded{2.5}
\end{funcdesc}
\begin{funcdesc}{any}{iterable}
Return True if any element of the \var{iterable} is true.
Equivalent to:
\begin{verbatim}
def any(iterable):
for element in iterable:
if element:
return True
return False
\end{verbatim}
\versionadded{2.5}
\end{funcdesc}
\begin{funcdesc}{basestring}{} \begin{funcdesc}{basestring}{}
This abstract type is the superclass for \class{str} and \class{unicode}. This abstract type is the superclass for \class{str} and \class{unicode}.
It cannot be called or instantiated, but it can be used to test whether It cannot be called or instantiated, but it can be used to test whether

View file

@ -92,6 +92,14 @@ if have_unicode:
(unichr(0x200), ValueError), (unichr(0x200), ValueError),
] ]
class TestFailingBool:
def __nonzero__(self):
raise RuntimeError
class TestFailingIter:
def __iter__(self):
raise RuntimeError
class BuiltinTest(unittest.TestCase): class BuiltinTest(unittest.TestCase):
def test_import(self): def test_import(self):
@ -117,6 +125,34 @@ class BuiltinTest(unittest.TestCase):
# str # str
self.assertRaises(TypeError, abs, 'a') self.assertRaises(TypeError, abs, 'a')
def test_all(self):
self.assertEqual(all([2, 4, 6]), True)
self.assertEqual(all([2, None, 6]), False)
self.assertRaises(RuntimeError, all, [2, TestFailingBool(), 6])
self.assertRaises(RuntimeError, all, TestFailingIter())
self.assertRaises(TypeError, all, 10) # Non-iterable
self.assertRaises(TypeError, all) # No args
self.assertRaises(TypeError, all, [2, 4, 6], []) # Too many args
self.assertEqual(all([]), True) # Empty iterator
S = [50, 60]
self.assertEqual(all(x > 42 for x in S), True)
S = [50, 40, 60]
self.assertEqual(all(x > 42 for x in S), False)
def test_any(self):
self.assertEqual(any([None, None, None]), False)
self.assertEqual(any([None, 4, None]), True)
self.assertRaises(RuntimeError, any, [None, TestFailingBool(), 6])
self.assertRaises(RuntimeError, all, TestFailingIter())
self.assertRaises(TypeError, any, 10) # Non-iterable
self.assertRaises(TypeError, any) # No args
self.assertRaises(TypeError, any, [2, 4, 6], []) # Too many args
self.assertEqual(any([]), False) # Empty iterator
S = [40, 60, 30]
self.assertEqual(any(x > 42 for x in S), True)
S = [10, 20, 30]
self.assertEqual(any(x > 42 for x in S), False)
def test_apply(self): def test_apply(self):
def f0(*args): def f0(*args):
self.assertEqual(args, ()) self.assertEqual(args, ())

View file

@ -10,6 +10,8 @@ What's New in Python 2.5 alpha 1?
Core and builtins Core and builtins
----------------- -----------------
- Added two new builtins, any() and all().
- Defining a class with empty parentheses is now allowed - Defining a class with empty parentheses is now allowed
(e.g., ``class C(): pass`` is no longer a syntax error) (e.g., ``class C(): pass`` is no longer a syntax error)

View file

@ -68,6 +68,69 @@ PyDoc_STRVAR(abs_doc,
\n\ \n\
Return the absolute value of the argument."); Return the absolute value of the argument.");
static PyObject *
builtin_all(PyObject *self, PyObject *v)
{
PyObject *it, *item;
it = PyObject_GetIter(v);
if (it == NULL)
return NULL;
while ((item = PyIter_Next(it)) != NULL) {
int cmp = PyObject_IsTrue(item);
Py_DECREF(item);
if (cmp < 0) {
Py_DECREF(it);
return NULL;
}
if (cmp == 0) {
Py_DECREF(it);
Py_RETURN_FALSE;
}
}
Py_DECREF(it);
if (PyErr_Occurred())
return NULL;
Py_RETURN_TRUE;
}
PyDoc_STRVAR(all_doc,
"all(iterable) -> bool\n\
\n\
Return True if bool(x) is True for all values x in the iterable.");
static PyObject *
builtin_any(PyObject *self, PyObject *v)
{
PyObject *it, *item;
it = PyObject_GetIter(v);
if (it == NULL)
return NULL;
while ((item = PyIter_Next(it)) != NULL) {
int cmp = PyObject_IsTrue(item);
Py_DECREF(item);
if (cmp < 0) {
Py_DECREF(it);
return NULL;
}
if (cmp == 1) {
Py_DECREF(it);
Py_RETURN_TRUE;
}
}
Py_DECREF(it);
if (PyErr_Occurred())
return NULL;
Py_RETURN_FALSE;
}
PyDoc_STRVAR(any_doc,
"any(iterable) -> bool\n\
\n\
Return True if bool(x) is True for any x in the iterable.");
static PyObject * static PyObject *
builtin_apply(PyObject *self, PyObject *args) builtin_apply(PyObject *self, PyObject *args)
@ -2125,6 +2188,8 @@ in length to the length of the shortest argument sequence.");
static PyMethodDef builtin_methods[] = { static PyMethodDef builtin_methods[] = {
{"__import__", builtin___import__, METH_VARARGS, import_doc}, {"__import__", builtin___import__, METH_VARARGS, import_doc},
{"abs", builtin_abs, METH_O, abs_doc}, {"abs", builtin_abs, METH_O, abs_doc},
{"all", builtin_all, METH_O, all_doc},
{"any", builtin_any, METH_O, any_doc},
{"apply", builtin_apply, METH_VARARGS, apply_doc}, {"apply", builtin_apply, METH_VARARGS, apply_doc},
{"callable", builtin_callable, METH_O, callable_doc}, {"callable", builtin_callable, METH_O, callable_doc},
{"chr", builtin_chr, METH_VARARGS, chr_doc}, {"chr", builtin_chr, METH_VARARGS, chr_doc},