summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>2006-06-20 10:58:19 +0200
committerAdrien Di Mascio <Adrien.DiMascio@logilab.fr>2006-06-20 10:58:19 +0200
commitaafd84cd45312cd197afcb01ac4d605d52d1209f (patch)
tree346567fd1fbc51cdef8f7525f41fdd15d96d4cea
parent9bf7768129e98d76aa87552844fe56a49a291bcf (diff)
downloadlogilab-common-aafd84cd45312cd197afcb01ac4d605d52d1209f.tar.gz
adds any/all builtins
-rw-r--r--ChangeLog4
-rw-r--r--compat.py25
-rw-r--r--table.py2
-rw-r--r--test/unittest_compat.py47
4 files changed, 72 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index 9a47af2..a626852 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,10 @@
ChangeLog for logilab.common
============================
+ --
+ * python2.5 compatibility (testlib.py + compat.py)
+
+
2006-06-08 -- 0.16.1
* testlib: improved test collections
* compat: added cmp argument to sorted
diff --git a/compat.py b/compat.py
index 6100070..4a34e38 100644
--- a/compat.py
+++ b/compat.py
@@ -188,3 +188,28 @@ except NameError:
l2 = list(l)
l2.reverse()
return l2
+
+# Python2.5 builtins
+try:
+ any = any
+ all = all
+except NameError:
+ def any(iterable):
+ """any(iterable) -> bool
+
+ Return True if bool(x) is True for any x in the iterable.
+ """
+ for elt in iterable:
+ if bool(elt):
+ return True
+ return False
+
+ def all(iterable):
+ """all(iterable) -> bool
+
+ Return True if bool(x) is True for all values x in the iterable.
+ """
+ for elt in iterable:
+ if not bool(elt):
+ return False
+ return True
diff --git a/table.py b/table.py
index 2851f7c..e3bb79c 100644
--- a/table.py
+++ b/table.py
@@ -188,7 +188,7 @@ class Table(object):
row_name = row_name or self._next_row_name()
self.row_names.append(row_name)
self.data.append(row_data)
-
+ return len(self.data) - 1
def insert_row(self, index, row_data, row_name=None):
"""Appends row_data before 'index' in the table. To make 'insert'
diff --git a/test/unittest_compat.py b/test/unittest_compat.py
index bbf534f..1558ba8 100644
--- a/test/unittest_compat.py
+++ b/test/unittest_compat.py
@@ -2,7 +2,7 @@
__revision__ = '$Id: unittest_compat.py,v 1.3 2005-08-08 10:44:10 adim Exp $'
-import unittest
+from logilab.common.testlib import TestCase, unittest_main
import sys
import types
import __builtin__
@@ -23,6 +23,7 @@ class CompatTCMixIn:
del sys.modules[modname]
for funcname, func in self.builtins_backup.items():
setattr(__builtin__, funcname, func)
+ # delattr(__builtin__, 'builtin_%s' % funcname)
for modname, mod in self.modules_backup.items():
sys.modules[modname] = mod
try:
@@ -36,7 +37,8 @@ class CompatTCMixIn:
if func is not None:
self.builtins_backup[builtin] = func
delattr(__builtin__, builtin)
-
+ # setattr(__builtin__, 'builtin_%s' % builtin, func)
+
def remove_modules(self):
for modname in self.MODNAMES:
if modname in sys.modules:
@@ -57,7 +59,7 @@ class CompatTCMixIn:
self.assertRaises(ImportError, eval, code)
-class Py23CompatTC(CompatTCMixIn, unittest.TestCase):
+class Py23CompatTC(CompatTCMixIn, TestCase):
BUILTINS = ('enumerate', 'sum')
MODNAMES = {
'sets' : ('Set', 'ImmutableSet'),
@@ -113,7 +115,7 @@ class Py23CompatTC(CompatTCMixIn, unittest.TestCase):
self.assertEquals(d[s], 'bar')
-class Py24CompatTC(CompatTCMixIn, unittest.TestCase):
+class Py24CompatTC(CompatTCMixIn, TestCase):
BUILTINS = ('reversed', 'sorted', 'set', 'frozenset',)
def test_sorted(self):
@@ -147,6 +149,41 @@ class Py24CompatTC(CompatTCMixIn, unittest.TestCase):
+
+class Py25CompatTC(CompatTCMixIn, TestCase):
+ BUILTINS = ('any', 'all',)
+
+ def test_any(self):
+ from logilab.common.compat import any
+ testdata = ([], (), '', 'abc', xrange(0, 10), xrange(0, -10, -1))
+ self.assertEquals(any([]), False)
+ self.assertEquals(any(()), False)
+ self.assertEquals(any(''), False)
+ self.assertEquals(any('abc'), True)
+ self.assertEquals(any(xrange(10)), True)
+ self.assertEquals(any(xrange(0, -10, -1)), True)
+ # python2.5's any consumes iterables
+ irange = iter(range(10))
+ self.assertEquals(any(irange), True)
+ self.assertEquals(irange.next(), 2)
+
+
+ def test_all(self):
+ from logilab.common.compat import all
+ testdata = ([], (), '', 'abc', xrange(0, 10), xrange(0, -10, -1))
+ self.assertEquals(all([]), True)
+ self.assertEquals(all(()), True)
+ self.assertEquals(all(''), True)
+ self.assertEquals(all('abc'), True)
+ self.assertEquals(all(xrange(10)), False)
+ self.assertEquals(all(xrange(0, -10, -1)), False)
+ # python2.5's all consumes iterables
+ irange = iter(range(10))
+ self.assertEquals(all(irange), False)
+ self.assertEquals(irange.next(), 1)
+
+
+
if __name__ == '__main__':
- unittest.main()
+ unittest_main()