@@ -162,6 +162,80 @@ def fromkeys(cls, iterable, value=None):
162162
163163
164164
165+ ################################################################################
166+ ### UserList
167+ ################################################################################
168+
169+ class UserList (MutableSequence ):
170+ """A more or less complete user-defined wrapper around list objects."""
171+ def __init__ (self , initlist = None ):
172+ self .data = []
173+ if initlist is not None :
174+ # XXX should this accept an arbitrary sequence?
175+ if type (initlist ) == type (self .data ):
176+ self .data [:] = initlist
177+ elif isinstance (initlist , UserList ):
178+ self .data [:] = initlist .data [:]
179+ else :
180+ self .data = list (initlist )
181+ def __repr__ (self ): return repr (self .data )
182+ def __lt__ (self , other ): return self .data < self .__cast (other )
183+ def __le__ (self , other ): return self .data <= self .__cast (other )
184+ def __eq__ (self , other ): return self .data == self .__cast (other )
185+ def __ne__ (self , other ): return self .data != self .__cast (other )
186+ def __gt__ (self , other ): return self .data > self .__cast (other )
187+ def __ge__ (self , other ): return self .data >= self .__cast (other )
188+ def __cast (self , other ):
189+ return other .data if isinstance (other , UserList ) else other
190+ def __cmp__ (self , other ):
191+ return cmp (self .data , self .__cast (other ))
192+ def __contains__ (self , item ): return item in self .data
193+ def __len__ (self ): return len (self .data )
194+ def __getitem__ (self , i ): return self .data [i ]
195+ def __setitem__ (self , i , item ): self .data [i ] = item
196+ def __delitem__ (self , i ): del self .data [i ]
197+ def __add__ (self , other ):
198+ if isinstance (other , UserList ):
199+ return self .__class__ (self .data + other .data )
200+ elif isinstance (other , type (self .data )):
201+ return self .__class__ (self .data + other )
202+ return self .__class__ (self .data + list (other ))
203+ def __radd__ (self , other ):
204+ if isinstance (other , UserList ):
205+ return self .__class__ (other .data + self .data )
206+ elif isinstance (other , type (self .data )):
207+ return self .__class__ (other + self .data )
208+ return self .__class__ (list (other ) + self .data )
209+ def __iadd__ (self , other ):
210+ if isinstance (other , UserList ):
211+ self .data += other .data
212+ elif isinstance (other , type (self .data )):
213+ self .data += other
214+ else :
215+ self .data += list (other )
216+ return self
217+ def __mul__ (self , n ):
218+ return self .__class__ (self .data * n )
219+ __rmul__ = __mul__
220+ def __imul__ (self , n ):
221+ self .data *= n
222+ return self
223+ def append (self , item ): self .data .append (item )
224+ def insert (self , i , item ): self .data .insert (i , item )
225+ def pop (self , i = - 1 ): return self .data .pop (i )
226+ def remove (self , item ): self .data .remove (item )
227+ def count (self , item ): return self .data .count (item )
228+ def index (self , item , * args ): return self .data .index (item , * args )
229+ def reverse (self ): self .data .reverse ()
230+ def sort (self , * args , ** kwds ): self .data .sort (* args , ** kwds )
231+ def extend (self , other ):
232+ if isinstance (other , UserList ):
233+ self .data .extend (other .data )
234+ else :
235+ self .data .extend (other )
236+
237+
238+
165239################################################################################
166240### Simple tests
167241################################################################################
0 commit comments