annotate Lib/UserList.py @ 3546:3f7cdf1663f7 legacy-trunk

A version of swed that can import html files. Also demonstrates implementing object handlers in Python.
author Jack Jansen <jack.jansen@cwi.nl>
date Tue, 07 May 1996 15:25:36 +0000
parents 1940892d2fe3
children 3440777f0e65
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1416
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # A more or less complete user-defined wrapper around list objects
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:
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
4 def __init__(self, list = None):
1416
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 self.data = []
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
6 if list is not None:
1416
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 if type(list) == type(self.data):
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 self.data[:] = list
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 else:
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 self.data[:] = list.data[:]
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 def __repr__(self): return repr(self.data)
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 def __cmp__(self, list):
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 if type(list) == type(self.data):
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 return cmp(self.data, list)
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 else:
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 return cmp(self.data, list.data)
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 def __len__(self): return len(self.data)
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 def __getitem__(self, i): return self.data[i]
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 def __setitem__(self, i, item): self.data[i] = item
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 def __delitem__(self, i): del self.data[i]
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 def __getslice__(self, i, j):
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 userlist = UserList()
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 userlist.data[:] = self.data[i:j]
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 return userlist
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 def __setslice__(self, i, j, list):
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 if type(list) == type(self.data):
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 self.data[i:j] = list
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 else:
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 self.data[i:j] = list.data
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 def __delslice__(self, i, j): del self.data[i:j]
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 def append(self, item): self.data.append(item)
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 def insert(self, i, item): self.data.insert(i, item)
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 def remove(self, item): self.data.remove(item)
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 def count(self, item): return self.data.count(item)
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 def index(self, item): return self.data.index(item)
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 def reverse(self): self.data.reverse()
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 def sort(self, *args): apply(self.data.sort, args)