diff options
author | Raymond Hettinger <python@rcn.com> | 2008-02-12 20:03:09 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-02-12 20:03:09 (GMT) |
commit | 53dbe39b46ef42aeef12c6f46f8575a794e20440 (patch) | |
tree | 99f15bb4ba51d6e88ad50f462af0ed0235b1d6d5 /Lib | |
parent | 4513ef8b7a4a684deea0dda23a760f4596a1097c (diff) | |
download | cpython-53dbe39b46ef42aeef12c6f46f8575a794e20440.zip cpython-53dbe39b46ef42aeef12c6f46f8575a794e20440.tar.gz cpython-53dbe39b46ef42aeef12c6f46f8575a794e20440.tar.bz2 |
Move UserList to collections.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/UserList.py | 73 | ||||
-rw-r--r-- | Lib/collections.py | 74 | ||||
-rw-r--r-- | Lib/test/string_tests.py | 2 | ||||
-rw-r--r-- | Lib/test/test_bisect.py | 2 | ||||
-rw-r--r-- | Lib/test/test_builtin.py | 4 | ||||
-rw-r--r-- | Lib/test/test_extcall.py | 3 | ||||
-rw-r--r-- | Lib/test/test_file.py | 2 | ||||
-rw-r--r-- | Lib/test/test_fileio.py | 2 | ||||
-rw-r--r-- | Lib/test/test_richcmp.py | 2 | ||||
-rw-r--r-- | Lib/test/test_userlist.py | 2 | ||||
-rw-r--r-- | Lib/test/test_weakref.py | 8 |
11 files changed, 87 insertions, 87 deletions
diff --git a/Lib/UserList.py b/Lib/UserList.py deleted file mode 100644 index 7617a08..0000000 --- a/Lib/UserList.py +++ /dev/null @@ -1,73 +0,0 @@ -"""A more or less complete user-defined wrapper around list objects.""" - -import collections - -class UserList(collections.MutableSequence): - def __init__(self, initlist=None): - self.data = [] - if initlist is not None: - # XXX should this accept an arbitrary sequence? - if type(initlist) == type(self.data): - self.data[:] = initlist - elif isinstance(initlist, UserList): - self.data[:] = initlist.data[:] - else: - self.data = list(initlist) - def __repr__(self): return repr(self.data) - def __lt__(self, other): return self.data < self.__cast(other) - def __le__(self, other): return self.data <= self.__cast(other) - def __eq__(self, other): return self.data == self.__cast(other) - def __ne__(self, other): return self.data != self.__cast(other) - def __gt__(self, other): return self.data > self.__cast(other) - def __ge__(self, other): return self.data >= self.__cast(other) - def __cast(self, other): - if isinstance(other, UserList): return other.data - else: return other - def __cmp__(self, other): - return cmp(self.data, self.__cast(other)) - def __contains__(self, item): return item in self.data - def __len__(self): return len(self.data) - def __getitem__(self, i): return self.data[i] - def __setitem__(self, i, item): self.data[i] = item - def __delitem__(self, i): del self.data[i] - def __add__(self, other): - if isinstance(other, UserList): - return self.__class__(self.data + other.data) - elif isinstance(other, type(self.data)): - return self.__class__(self.data + other) - else: - return self.__class__(self.data + list(other)) - def __radd__(self, other): - if isinstance(other, UserList): - return self.__class__(other.data + self.data) - elif isinstance(other, type(self.data)): - return self.__class__(other + self.data) - else: - return self.__class__(list(other) + self.data) - def __iadd__(self, other): - if isinstance(other, UserList): - self.data += other.data - elif isinstance(other, type(self.data)): - self.data += other - else: - self.data += list(other) - return self - def __mul__(self, n): - return self.__class__(self.data*n) - __rmul__ = __mul__ - def __imul__(self, n): - self.data *= n - return self - def append(self, item): self.data.append(item) - def insert(self, i, item): self.data.insert(i, item) - def pop(self, i=-1): return self.data.pop(i) - def remove(self, item): self.data.remove(item) - def count(self, item): return self.data.count(item) - 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 extend(self, other): - if isinstance(other, UserList): - self.data.extend(other.data) - else: - self.data.extend(other) diff --git a/Lib/collections.py b/Lib/collections.py index abde718..93194e0 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -163,6 +163,80 @@ class UserDict(MutableMapping): ################################################################################ +### UserList +################################################################################ + +class UserList(MutableSequence): + """A more or less complete user-defined wrapper around list objects.""" + def __init__(self, initlist=None): + self.data = [] + if initlist is not None: + # XXX should this accept an arbitrary sequence? + if type(initlist) == type(self.data): + self.data[:] = initlist + elif isinstance(initlist, UserList): + self.data[:] = initlist.data[:] + else: + self.data = list(initlist) + def __repr__(self): return repr(self.data) + def __lt__(self, other): return self.data < self.__cast(other) + def __le__(self, other): return self.data <= self.__cast(other) + def __eq__(self, other): return self.data == self.__cast(other) + def __ne__(self, other): return self.data != self.__cast(other) + def __gt__(self, other): return self.data > self.__cast(other) + def __ge__(self, other): return self.data >= self.__cast(other) + def __cast(self, other): + return other.data if isinstance(other, UserList) else other + def __cmp__(self, other): + return cmp(self.data, self.__cast(other)) + def __contains__(self, item): return item in self.data + def __len__(self): return len(self.data) + def __getitem__(self, i): return self.data[i] + def __setitem__(self, i, item): self.data[i] = item + def __delitem__(self, i): del self.data[i] + def __add__(self, other): + if isinstance(other, UserList): + return self.__class__(self.data + other.data) + elif isinstance(other, type(self.data)): + return self.__class__(self.data + other) + return self.__class__(self.data + list(other)) + def __radd__(self, other): + if isinstance(other, UserList): + return self.__class__(other.data + self.data) + elif isinstance(other, type(self.data)): + return self.__class__(other + self.data) + return self.__class__(list(other) + self.data) + def __iadd__(self, other): + if isinstance(other, UserList): + self.data += other.data + elif isinstance(other, type(self.data)): + self.data += other + else: + self.data += list(other) + return self + def __mul__(self, n): + return self.__class__(self.data*n) + __rmul__ = __mul__ + def __imul__(self, n): + self.data *= n + return self + def append(self, item): self.data.append(item) + def insert(self, i, item): self.data.insert(i, item) + def pop(self, i=-1): return self.data.pop(i) + def remove(self, item): self.data.remove(item) + def count(self, item): return self.data.count(item) + 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 extend(self, other): + if isinstance(other, UserList): + self.data.extend(other.data) + else: + self.data.extend(other) + + + +################################################################################ ### Simple tests ################################################################################ diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 80fe475..24fca59 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -4,7 +4,7 @@ Common tests shared by test_str, test_unicode, test_userstring and test_string. import unittest, string, sys, struct from test import test_support -from UserList import UserList +from collections import UserList class Sequence: def __init__(self, seq='wxyz'): self.seq = seq diff --git a/Lib/test/test_bisect.py b/Lib/test/test_bisect.py index 95eafbe..dc18a0e 100644 --- a/Lib/test/test_bisect.py +++ b/Lib/test/test_bisect.py @@ -1,7 +1,7 @@ import unittest from test import test_support from bisect import bisect_right, bisect_left, insort_left, insort_right, insort, bisect -from UserList import UserList +from collections import UserList class TestBisect(unittest.TestCase): diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index f781db3..762afad 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -5,7 +5,7 @@ from test.test_support import fcmp, TESTFN, unlink, run_unittest, \ run_with_locale from operator import neg -import sys, warnings, random, collections, io, rational, fractions +import sys, warnings, random, collections, io, fractions warnings.filterwarnings("ignore", "hex../oct.. of negative int", FutureWarning, __name__) warnings.filterwarnings("ignore", "integer argument expected", @@ -210,7 +210,7 @@ class BuiltinTest(unittest.TestCase): # verify that circular objects are not handled a = []; a.append(a) b = []; b.append(b) - from UserList import UserList + from collections import UserList c = UserList(); c.append(c) self.assertRaises(RuntimeError, cmp, a, b) self.assertRaises(RuntimeError, cmp, b, c) diff --git a/Lib/test/test_extcall.py b/Lib/test/test_extcall.py index 2dc87b9..596a3ef 100644 --- a/Lib/test/test_extcall.py +++ b/Lib/test/test_extcall.py @@ -1,6 +1,5 @@ from test.test_support import verify, verbose, TestFailed, sortdict -from UserList import UserList -from collections import UserDict +from collections import UserDict, UserList def e(a, b): print(a, b) diff --git a/Lib/test/test_file.py b/Lib/test/test_file.py index 5da2da9..5102de3 100644 --- a/Lib/test/test_file.py +++ b/Lib/test/test_file.py @@ -5,7 +5,7 @@ from array import array from weakref import proxy from test.test_support import TESTFN, findfile, run_unittest -from UserList import UserList +from collections import UserList class AutoFileTests(unittest.TestCase): # file tests for which a test file is automatically set up diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index 7f7332e..2f9c23f 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -7,7 +7,7 @@ from array import array from weakref import proxy from test.test_support import TESTFN, findfile, run_unittest -from UserList import UserList +from collections import UserList import _fileio diff --git a/Lib/test/test_richcmp.py b/Lib/test/test_richcmp.py index 3f97ece..7b6f9be 100644 --- a/Lib/test/test_richcmp.py +++ b/Lib/test/test_richcmp.py @@ -254,7 +254,7 @@ class MiscTest(unittest.TestCase): def test_recursion(self): # Check that comparison for recursive objects fails gracefully - from UserList import UserList + from collections import UserList a = UserList() b = UserList() a.append(b) diff --git a/Lib/test/test_userlist.py b/Lib/test/test_userlist.py index 32c7733..9d012e0 100644 --- a/Lib/test/test_userlist.py +++ b/Lib/test/test_userlist.py @@ -1,6 +1,6 @@ # Check every path through every method of UserList -from UserList import UserList +from collections import UserList import unittest from test import test_support, list_tests diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index 922b293..7de7b77 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -1,7 +1,7 @@ import gc import sys import unittest -import UserList +import collections import weakref from test import test_support @@ -157,7 +157,7 @@ class ReferencesTestCase(TestBase): o = C() self.check_proxy(o, weakref.proxy(o)) - L = UserList.UserList() + L = collections.UserList() p = weakref.proxy(L) self.failIf(p, "proxy for empty UserList should be false") p.append(12) @@ -171,11 +171,11 @@ class ReferencesTestCase(TestBase): p[1] = 5 self.assertEqual(L[1], 5) self.assertEqual(p[1], 5) - L2 = UserList.UserList(L) + L2 = collections.UserList(L) p2 = weakref.proxy(L2) self.assertEqual(p, p2) ## self.assertEqual(repr(L2), repr(p2)) - L3 = UserList.UserList(range(10)) + L3 = collections.UserList(range(10)) p3 = weakref.proxy(L3) self.assertEqual(L3[:], p3[:]) self.assertEqual(L3[5:], p3[5:]) |