Mercurial > cpython-lru_cache-weakref
annotate Lib/UserList.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 7527da2b05e8 |
| children |
| rev | line source |
|---|---|
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10481
diff
changeset
|
1 """A more or less complete user-defined wrapper around list objects.""" |
|
1416
d6f6a1cb7d79
* test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
45212
feb6dd36d53f
Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs.
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
3 import collections |
|
feb6dd36d53f
Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs.
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
4 |
|
feb6dd36d53f
Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs.
Raymond Hettinger <python@rcn.com>
parents:
31210
diff
changeset
|
5 class UserList(collections.MutableSequence): |
|
12134
c160749acff8
robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
6 def __init__(self, initlist=None): |
|
10481
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
7 self.data = [] |
|
12134
c160749acff8
robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
8 if initlist is not None: |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
12134
diff
changeset
|
9 # XXX should this accept an arbitrary sequence? |
|
12134
c160749acff8
robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
10 if type(initlist) == type(self.data): |
|
c160749acff8
robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
11 self.data[:] = initlist |
|
c160749acff8
robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
12 elif isinstance(initlist, UserList): |
|
c160749acff8
robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
13 self.data[:] = initlist.data[:] |
|
10481
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
14 else: |
|
12134
c160749acff8
robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
15 self.data = list(initlist) |
|
10481
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
16 def __repr__(self): return repr(self.data) |
|
15990
3f4b6002c07d
Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents:
15060
diff
changeset
|
17 def __lt__(self, other): return self.data < self.__cast(other) |
|
3f4b6002c07d
Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents:
15060
diff
changeset
|
18 def __le__(self, other): return self.data <= self.__cast(other) |
|
3f4b6002c07d
Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents:
15060
diff
changeset
|
19 def __eq__(self, other): return self.data == self.__cast(other) |
|
3f4b6002c07d
Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents:
15060
diff
changeset
|
20 def __ne__(self, other): return self.data != self.__cast(other) |
|
3f4b6002c07d
Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents:
15060
diff
changeset
|
21 def __gt__(self, other): return self.data > self.__cast(other) |
|
3f4b6002c07d
Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents:
15060
diff
changeset
|
22 def __ge__(self, other): return self.data >= self.__cast(other) |
|
3f4b6002c07d
Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents:
15060
diff
changeset
|
23 def __cast(self, other): |
|
3f4b6002c07d
Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents:
15060
diff
changeset
|
24 if isinstance(other, UserList): return other.data |
|
3f4b6002c07d
Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents:
15060
diff
changeset
|
25 else: return other |
|
10481
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
26 def __cmp__(self, other): |
|
18063
bbac2a6a5d16
Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents:
16657
diff
changeset
|
27 return cmp(self.data, self.__cast(other)) |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
45283
diff
changeset
|
28 __hash__ = None # Mutable sequence, so not hashable |
|
14692
c6f43c6036ba
SF patch 101391: implemented UserList.__contains__.
Tim Peters <tim.peters@gmail.com>
parents:
14164
diff
changeset
|
29 def __contains__(self, item): return item in self.data |
|
10481
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
30 def __len__(self): return len(self.data) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
31 def __getitem__(self, i): return self.data[i] |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
32 def __setitem__(self, i, item): self.data[i] = item |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
33 def __delitem__(self, i): del self.data[i] |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
34 def __getslice__(self, i, j): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
35 i = max(i, 0); j = max(j, 0) |
|
15060
177ae1c52037
__getslice__(): Make this use the constructor form that gets a sequence
Fred Drake <fdrake@acm.org>
parents:
14692
diff
changeset
|
36 return self.__class__(self.data[i:j]) |
|
10481
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
37 def __setslice__(self, i, j, other): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
38 i = max(i, 0); j = max(j, 0) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
39 if isinstance(other, UserList): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
40 self.data[i:j] = other.data |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
41 elif isinstance(other, type(self.data)): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
42 self.data[i:j] = other |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
43 else: |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
44 self.data[i:j] = list(other) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
45 def __delslice__(self, i, j): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
46 i = max(i, 0); j = max(j, 0) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
47 del self.data[i:j] |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
48 def __add__(self, other): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
49 if isinstance(other, UserList): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
50 return self.__class__(self.data + other.data) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
51 elif isinstance(other, type(self.data)): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
52 return self.__class__(self.data + other) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
53 else: |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
54 return self.__class__(self.data + list(other)) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
55 def __radd__(self, other): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
56 if isinstance(other, UserList): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
57 return self.__class__(other.data + self.data) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
58 elif isinstance(other, type(self.data)): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
59 return self.__class__(other + self.data) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
60 else: |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
61 return self.__class__(list(other) + self.data) |
|
14164
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
62 def __iadd__(self, other): |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
63 if isinstance(other, UserList): |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
64 self.data += other.data |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
65 elif isinstance(other, type(self.data)): |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
66 self.data += other |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
67 else: |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
68 self.data += list(other) |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
69 return self |
|
10481
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
70 def __mul__(self, n): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
71 return self.__class__(self.data*n) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
72 __rmul__ = __mul__ |
|
14164
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
73 def __imul__(self, n): |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
74 self.data *= n |
|
dba4aec970e6
Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
75 return self |
|
10481
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
76 def append(self, item): self.data.append(item) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
77 def insert(self, i, item): self.data.insert(i, item) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
78 def pop(self, i=-1): return self.data.pop(i) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
79 def remove(self, item): self.data.remove(item) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
80 def count(self, item): return self.data.count(item) |
|
29510
01e925371d7c
SF #754014: list.index() should accept optional start, end arguments
Raymond Hettinger <python@rcn.com>
parents:
28089
diff
changeset
|
81 def index(self, item, *args): return self.data.index(item, *args) |
|
10481
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
82 def reverse(self): self.data.reverse() |
|
30698
3e3fd56ce595
Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents:
29510
diff
changeset
|
83 def sort(self, *args, **kwds): self.data.sort(*args, **kwds) |
|
10481
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
84 def extend(self, other): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
85 if isinstance(other, UserList): |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
86 self.data.extend(other.data) |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
87 else: |
|
bae650f57c41
Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents:
9778
diff
changeset
|
88 self.data.extend(other) |
