1c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro"""A more or less complete user-defined wrapper around list objects."""
2c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro
3c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaroimport collections
4c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro
5c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaroclass UserList(collections.MutableSequence):
6c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __init__(self, initlist=None):
7c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro        self.data = []
8c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro        if initlist is not None:
9c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro            # XXX should this accept an arbitrary sequence?
10c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro            if type(initlist) == type(self.data):
11c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro                self.data[:] = initlist
12c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro            elif isinstance(initlist, UserList):
13c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro                self.data[:] = initlist.data[:]
14c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro            else:
15c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro                self.data = list(initlist)
16c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __repr__(self): return repr(self.data)
17c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __lt__(self, other): return self.data <  self.__cast(other)
18c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __le__(self, other): return self.data <= self.__cast(other)
19c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __eq__(self, other): return self.data == self.__cast(other)
20c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __ne__(self, other): return self.data != self.__cast(other)
21c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __gt__(self, other): return self.data >  self.__cast(other)
22c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __ge__(self, other): return self.data >= self.__cast(other)
23c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __cast(self, other):
24c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro        if isinstance(other, UserList): return other.data
25c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro        else: return other
26c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __cmp__(self, other):
27c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro        return cmp(self.data, self.__cast(other))
28c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    __hash__ = None # Mutable sequence, so not hashable
29c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __contains__(self, item): return item in self.data
30c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __len__(self): return len(self.data)
31c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __getitem__(self, i): return self.data[i]
32c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __setitem__(self, i, item): self.data[i] = item
33c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __delitem__(self, i): del self.data[i]
34c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __getslice__(self, i, j):
35c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro        i = max(i, 0); j = max(j, 0)
36c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro        return self.__class__(self.data[i:j])
37c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro    def __setslice__(self, i, j, other):
38c3d4ca9f2df5ddf9894b36d1554fdfc95d625d3fNick Vaccaro        i = max(i, 0); j = max(j, 0)
39        if isinstance(other, UserList):
40            self.data[i:j] = other.data
41        elif isinstance(other, type(self.data)):
42            self.data[i:j] = other
43        else:
44            self.data[i:j] = list(other)
45    def __delslice__(self, i, j):
46        i = max(i, 0); j = max(j, 0)
47        del self.data[i:j]
48    def __add__(self, other):
49        if isinstance(other, UserList):
50            return self.__class__(self.data + other.data)
51        elif isinstance(other, type(self.data)):
52            return self.__class__(self.data + other)
53        else:
54            return self.__class__(self.data + list(other))
55    def __radd__(self, other):
56        if isinstance(other, UserList):
57            return self.__class__(other.data + self.data)
58        elif isinstance(other, type(self.data)):
59            return self.__class__(other + self.data)
60        else:
61            return self.__class__(list(other) + self.data)
62    def __iadd__(self, other):
63        if isinstance(other, UserList):
64            self.data += other.data
65        elif isinstance(other, type(self.data)):
66            self.data += other
67        else:
68            self.data += list(other)
69        return self
70    def __mul__(self, n):
71        return self.__class__(self.data*n)
72    __rmul__ = __mul__
73    def __imul__(self, n):
74        self.data *= n
75        return self
76    def append(self, item): self.data.append(item)
77    def insert(self, i, item): self.data.insert(i, item)
78    def pop(self, i=-1): return self.data.pop(i)
79    def remove(self, item): self.data.remove(item)
80    def count(self, item): return self.data.count(item)
81    def index(self, item, *args): return self.data.index(item, *args)
82    def reverse(self): self.data.reverse()
83    def sort(self, *args, **kwds): self.data.sort(*args, **kwds)
84    def extend(self, other):
85        if isinstance(other, UserList):
86            self.data.extend(other.data)
87        else:
88            self.data.extend(other)
89