annotate Lib/UserList.py @ 15842:f6cd36498a50 legacy-trunk

Document extensions to .pth files.
author Martin v. Löwis <martin@v.loewis.de>
date Sat, 13 Jan 2001 09:54:41 +0000
parents 177ae1c52037
children 3f4b6002c07d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 class UserList:
12134
c160749acff8 robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
4 def __init__(self, initlist=None):
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
5 self.data = []
12134
c160749acff8 robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
6 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
7 # 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
8 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
9 self.data[:] = initlist
c160749acff8 robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
10 elif isinstance(initlist, UserList):
c160749acff8 robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
11 self.data[:] = initlist.data[:]
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
12 else:
12134
c160749acff8 robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
13 self.data = list(initlist)
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
14 def __repr__(self): return repr(self.data)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
15 def __cmp__(self, other):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
16 if isinstance(other, UserList):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
17 return cmp(self.data, other.data)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
18 else:
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
19 return cmp(self.data, other)
14692
c6f43c6036ba SF patch 101391: implemented UserList.__contains__.
Tim Peters <tim.peters@gmail.com>
parents: 14164
diff changeset
20 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
21 def __len__(self): return len(self.data)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
22 def __getitem__(self, i): return self.data[i]
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
23 def __setitem__(self, i, item): self.data[i] = item
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
24 def __delitem__(self, i): del self.data[i]
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
25 def __getslice__(self, i, j):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
26 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
27 return self.__class__(self.data[i:j])
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
28 def __setslice__(self, i, j, other):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
29 i = max(i, 0); j = max(j, 0)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
30 if isinstance(other, UserList):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
31 self.data[i:j] = other.data
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
32 elif isinstance(other, type(self.data)):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
33 self.data[i:j] = other
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
34 else:
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
35 self.data[i:j] = list(other)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
36 def __delslice__(self, i, j):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
37 i = max(i, 0); j = max(j, 0)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
38 del self.data[i:j]
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
39 def __add__(self, other):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
40 if isinstance(other, UserList):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
41 return self.__class__(self.data + other.data)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
42 elif isinstance(other, type(self.data)):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
43 return self.__class__(self.data + other)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
44 else:
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
45 return self.__class__(self.data + list(other))
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
46 def __radd__(self, other):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
47 if isinstance(other, UserList):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
48 return self.__class__(other.data + self.data)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
49 elif isinstance(other, type(self.data)):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
50 return self.__class__(other + self.data)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
51 else:
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
52 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
53 def __iadd__(self, other):
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
54 if isinstance(other, UserList):
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
55 self.data += other.data
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
56 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
57 self.data += other
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
58 else:
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
59 self.data += list(other)
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
60 return self
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
61 def __mul__(self, n):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
62 return self.__class__(self.data*n)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
63 __rmul__ = __mul__
14164
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
64 def __imul__(self, n):
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
65 self.data *= n
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
66 return self
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
67 def append(self, item): self.data.append(item)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
68 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
69 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
70 def remove(self, item): self.data.remove(item)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
71 def count(self, item): return self.data.count(item)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
72 def index(self, item): return self.data.index(item)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
73 def reverse(self): self.data.reverse()
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
74 def sort(self, *args): apply(self.data.sort, args)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
75 def extend(self, other):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
76 if isinstance(other, UserList):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
77 self.data.extend(other.data)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
78 else:
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
79 self.data.extend(other)