diff options
author | Raymond Hettinger <python@rcn.com> | 2003-12-17 20:43:33 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-12-17 20:43:33 (GMT) |
commit | 64958a15d7c03efdc3d2eddf247666e18d1fd910 (patch) | |
tree | bc135ae082f8635fa858b81f52f141d7ffbd4c78 /Lib | |
parent | df38ea9c29a431602704c6bd45ca7417225a61c4 (diff) | |
download | cpython-64958a15d7c03efdc3d2eddf247666e18d1fd910.zip cpython-64958a15d7c03efdc3d2eddf247666e18d1fd910.tar.gz cpython-64958a15d7c03efdc3d2eddf247666e18d1fd910.tar.bz2 |
Guido grants a Christmas wish:
sorted() becomes a regular function instead of a classmethod.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/UserList.py | 5 | ||||
-rw-r--r-- | Lib/pyclbr.py | 2 | ||||
-rw-r--r-- | Lib/test/test_builtin.py | 37 | ||||
-rw-r--r-- | Lib/test/test_descrtut.py | 3 | ||||
-rw-r--r-- | Lib/test/test_itertools.py | 10 | ||||
-rw-r--r-- | Lib/test/test_operator.py | 2 | ||||
-rw-r--r-- | Lib/test/test_set.py | 10 | ||||
-rw-r--r-- | Lib/test/test_sort.py | 55 |
8 files changed, 48 insertions, 76 deletions
diff --git a/Lib/UserList.py b/Lib/UserList.py index e8fd356..072f6a7 100644 --- a/Lib/UserList.py +++ b/Lib/UserList.py @@ -78,11 +78,6 @@ class UserList: def index(self, item, *args): return self.data.index(item, *args) def reverse(self): self.data.reverse() def sort(self, *args, **kwds): self.data.sort(*args, **kwds) - def sorted(cls, iterable, *args, **kwds): - s = cls(iterable) - s.sort(*args, **kwds) - return s - sorted = classmethod(sorted) def extend(self, other): if isinstance(other, UserList): self.data.extend(other.data) diff --git a/Lib/pyclbr.py b/Lib/pyclbr.py index 6674b71..9e6bcb0 100644 --- a/Lib/pyclbr.py +++ b/Lib/pyclbr.py @@ -327,7 +327,7 @@ def _main(): for obj in objs: if isinstance(obj, Class): print "class", obj.name, obj.super, obj.lineno - methods = list.sorted(obj.methods.iteritems(), key=itemgetter(1)) + methods = sorted(obj.methods.iteritems(), key=itemgetter(1)) for name, lineno in methods: if name != "__path__": print " def", name, lineno diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 1953f29..db823ff 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -3,7 +3,7 @@ import test.test_support, unittest from test.test_support import fcmp, have_unicode, TESTFN, unlink -import sys, warnings, cStringIO +import sys, warnings, cStringIO, random warnings.filterwarnings("ignore", "hex../oct.. of negative int", FutureWarning, __name__) warnings.filterwarnings("ignore", "integer argument expected", @@ -1153,8 +1153,41 @@ class BuiltinTest(unittest.TestCase): return i self.assertRaises(ValueError, zip, BadSeq(), BadSeq()) +class TestSorted(unittest.TestCase): + + def test_basic(self): + data = range(100) + copy = data[:] + random.shuffle(copy) + self.assertEqual(data, sorted(copy)) + self.assertNotEqual(data, copy) + + data.reverse() + random.shuffle(copy) + self.assertEqual(data, sorted(copy, cmp=lambda x, y: cmp(y,x))) + self.assertNotEqual(data, copy) + random.shuffle(copy) + self.assertEqual(data, sorted(copy, key=lambda x: -x)) + self.assertNotEqual(data, copy) + random.shuffle(copy) + self.assertEqual(data, sorted(copy, reverse=1)) + self.assertNotEqual(data, copy) + + def test_inputtypes(self): + s = 'abracadabra' + for T in [unicode, list, tuple]: + self.assertEqual(sorted(s), sorted(T(s))) + + s = ''.join(dict.fromkeys(s).keys()) # unique letters only + for T in [unicode, set, frozenset, list, tuple, dict.fromkeys]: + self.assertEqual(sorted(s), sorted(T(s))) + + def test_baddecorator(self): + data = 'The quick Brown fox Jumped over The lazy Dog'.split() + self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0) + def test_main(): - test.test_support.run_unittest(BuiltinTest) + test.test_support.run_unittest(BuiltinTest, TestSorted) if __name__ == "__main__": test_main() diff --git a/Lib/test/test_descrtut.py b/Lib/test/test_descrtut.py index b895d6d..58b7451 100644 --- a/Lib/test/test_descrtut.py +++ b/Lib/test/test_descrtut.py @@ -226,8 +226,7 @@ Instead, you can get the same information from the list type: 'pop', 'remove', 'reverse', - 'sort', - 'sorted'] + 'sort'] The new introspection API gives more information than the old one: in addition to the regular methods, it also shows the methods that are diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index b4c0a8b..31b1b7c 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -97,16 +97,16 @@ class TestBasicOps(unittest.TestCase): # Exercise pipes and filters style s = 'abracadabra' # sort s | uniq - r = [k for k, g in groupby(list.sorted(s))] + r = [k for k, g in groupby(sorted(s))] self.assertEqual(r, ['a', 'b', 'c', 'd', 'r']) # sort s | uniq -d - r = [k for k, g in groupby(list.sorted(s)) if list(islice(g,1,2))] + r = [k for k, g in groupby(sorted(s)) if list(islice(g,1,2))] self.assertEqual(r, ['a', 'b', 'r']) # sort s | uniq -c - r = [(len(list(g)), k) for k, g in groupby(list.sorted(s))] + r = [(len(list(g)), k) for k, g in groupby(sorted(s))] self.assertEqual(r, [(5, 'a'), (2, 'b'), (1, 'c'), (1, 'd'), (2, 'r')]) # sort s | uniq -c | sort -rn | head -3 - r = list.sorted([(len(list(g)) , k) for k, g in groupby(list.sorted(s))], reverse=True)[:3] + r = sorted([(len(list(g)) , k) for k, g in groupby(sorted(s))], reverse=True)[:3] self.assertEqual(r, [(5, 'a'), (2, 'r'), (2, 'b')]) # iter.next failure @@ -669,7 +669,7 @@ Samuele >>> from operator import itemgetter >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) ->>> di = list.sorted(d.iteritems(), key=itemgetter(1)) +>>> di = sorted(d.iteritems(), key=itemgetter(1)) >>> for k, g in groupby(di, itemgetter(1)): ... print k, map(itemgetter(0), g) ... diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py index e3a67f0..3263db2 100644 --- a/Lib/test/test_operator.py +++ b/Lib/test/test_operator.py @@ -263,7 +263,7 @@ class OperatorTestCase(unittest.TestCase): inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] getcount = operator.itemgetter(1) self.assertEqual(map(getcount, inventory), [3, 2, 5, 1]) - self.assertEqual(list.sorted(inventory, key=getcount), + self.assertEqual(sorted(inventory, key=getcount), [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]) def test_main(): diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index f3cdc17..5d37169 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -22,8 +22,8 @@ class TestJointOps(unittest.TestCase): self.d = dict.fromkeys(word) def test_uniquification(self): - actual = list.sorted(self.s) - expected = list.sorted(self.d) + actual = sorted(self.s) + expected = sorted(self.d) self.assertEqual(actual, expected) self.assertRaises(PassThru, self.thetype, check_pass_thru()) self.assertRaises(TypeError, self.thetype, [[]]) @@ -1241,7 +1241,7 @@ class TestVariousIteratorArgs(unittest.TestCase): for cons in (set, frozenset): for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): for g in (G, I, Ig, S, L, R): - self.assertEqual(list.sorted(cons(g(s))), list.sorted(g(s))) + self.assertEqual(sorted(cons(g(s))), sorted(g(s))) self.assertRaises(TypeError, cons , X(s)) self.assertRaises(TypeError, cons , N(s)) self.assertRaises(ZeroDivisionError, cons , E(s)) @@ -1253,7 +1253,7 @@ class TestVariousIteratorArgs(unittest.TestCase): for g in (G, I, Ig, L, R): expected = meth(data) actual = meth(G(data)) - self.assertEqual(list.sorted(actual), list.sorted(expected)) + self.assertEqual(sorted(actual), sorted(expected)) self.assertRaises(TypeError, meth, X(s)) self.assertRaises(TypeError, meth, N(s)) self.assertRaises(ZeroDivisionError, meth, E(s)) @@ -1267,7 +1267,7 @@ class TestVariousIteratorArgs(unittest.TestCase): t = s.copy() getattr(s, methname)(list(g(data))) getattr(t, methname)(g(data)) - self.assertEqual(list.sorted(s), list.sorted(t)) + self.assertEqual(sorted(s), sorted(t)) self.assertRaises(TypeError, getattr(set('january'), methname), X(data)) self.assertRaises(TypeError, getattr(set('january'), methname), N(data)) diff --git a/Lib/test/test_sort.py b/Lib/test/test_sort.py index 667c9ce..dedc41c 100644 --- a/Lib/test/test_sort.py +++ b/Lib/test/test_sort.py @@ -248,66 +248,11 @@ class TestDecorateSortUndecorate(unittest.TestCase): copy2.sort(key=lambda x: x[0], reverse=True) self.assertEqual(data, copy2) -class TestSorted(unittest.TestCase): - - def test_basic(self): - data = range(100) - copy = data[:] - random.shuffle(copy) - self.assertEqual(data, list.sorted(copy)) - self.assertNotEqual(data, copy) - - data.reverse() - random.shuffle(copy) - self.assertEqual(data, list.sorted(copy, cmp=lambda x, y: cmp(y,x))) - self.assertNotEqual(data, copy) - random.shuffle(copy) - self.assertEqual(data, list.sorted(copy, key=lambda x: -x)) - self.assertNotEqual(data, copy) - random.shuffle(copy) - self.assertEqual(data, list.sorted(copy, reverse=1)) - self.assertNotEqual(data, copy) - - def test_inputtypes(self): - s = 'abracadabra' - for T in [unicode, list, tuple]: - self.assertEqual(list.sorted(s), list.sorted(T(s))) - - s = ''.join(dict.fromkeys(s).keys()) # unique letters only - for T in [unicode, set, frozenset, list, tuple, dict.fromkeys]: - self.assertEqual(list.sorted(s), list.sorted(T(s))) - - def test_baddecorator(self): - data = 'The quick Brown fox Jumped over The lazy Dog'.split() - self.assertRaises(TypeError, list.sorted, data, None, lambda x,y: 0) - - def classmethods(self): - s = "hello world" - a = list.sorted(s) - b = UserList.sorted(s) - c = [].sorted(s) - d = UserList().sorted(s) - class Mylist(list): - def __new__(cls): - return UserList() - e = MyList.sorted(s) - f = MyList().sorted(s) - class Myuserlist(UserList): - def __new__(cls): - return [] - g = MyList.sorted(s) - h = MyList().sorted(s) - self.assert_(a == b == c == d == e == f == g == h) - self.assert_(b.__class__ == d.__class__ == UserList) - self.assert_(e.__class__ == f.__class__ == MyList) - self.assert_(g.__class__ == h.__class__ == Myuserlist) - #============================================================================== def test_main(verbose=None): test_classes = ( TestDecorateSortUndecorate, - TestSorted, TestBugs, ) |