diff options
author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> | 2006-06-20 10:58:19 +0200 |
---|---|---|
committer | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> | 2006-06-20 10:58:19 +0200 |
commit | aafd84cd45312cd197afcb01ac4d605d52d1209f (patch) | |
tree | 346567fd1fbc51cdef8f7525f41fdd15d96d4cea | |
parent | 9bf7768129e98d76aa87552844fe56a49a291bcf (diff) | |
download | logilab-common-aafd84cd45312cd197afcb01ac4d605d52d1209f.tar.gz |
adds any/all builtins
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | compat.py | 25 | ||||
-rw-r--r-- | table.py | 2 | ||||
-rw-r--r-- | test/unittest_compat.py | 47 |
4 files changed, 72 insertions, 6 deletions
@@ -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 @@ -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 @@ -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() |