A more or less complete user-defined wrapper around list objects.
| 1226 | ################################################################################ |
| 1227 | |
| 1228 | class UserList(_collections_abc.MutableSequence): |
| 1229 | """A more or less complete user-defined wrapper around list objects.""" |
| 1230 | |
| 1231 | def __init__(self, initlist=None): |
| 1232 | self.data = [] |
| 1233 | if initlist is not None: |
| 1234 | # XXX should this accept an arbitrary sequence? |
| 1235 | if type(initlist) == type(self.data): |
| 1236 | self.data[:] = initlist |
| 1237 | elif isinstance(initlist, UserList): |
| 1238 | self.data[:] = initlist.data[:] |
| 1239 | else: |
| 1240 | self.data = list(initlist) |
| 1241 | |
| 1242 | def __repr__(self): |
| 1243 | return repr(self.data) |
| 1244 | |
| 1245 | def __lt__(self, other): |
| 1246 | return self.data < self.__cast(other) |
| 1247 | |
| 1248 | def __le__(self, other): |
| 1249 | return self.data <= self.__cast(other) |
| 1250 | |
| 1251 | def __eq__(self, other): |
| 1252 | return self.data == self.__cast(other) |
| 1253 | |
| 1254 | def __gt__(self, other): |
| 1255 | return self.data > self.__cast(other) |
| 1256 | |
| 1257 | def __ge__(self, other): |
| 1258 | return self.data >= self.__cast(other) |
| 1259 | |
| 1260 | def __cast(self, other): |
| 1261 | return other.data if isinstance(other, UserList) else other |
| 1262 | |
| 1263 | def __contains__(self, item): |
| 1264 | return item in self.data |
| 1265 | |
| 1266 | def __len__(self): |
| 1267 | return len(self.data) |
| 1268 | |
| 1269 | def __getitem__(self, i): |
| 1270 | if isinstance(i, slice): |
| 1271 | return self.__class__(self.data[i]) |
| 1272 | else: |
| 1273 | return self.data[i] |
| 1274 | |
| 1275 | def __setitem__(self, i, item): |
| 1276 | self.data[i] = item |
| 1277 | |
| 1278 | def __delitem__(self, i): |
| 1279 | del self.data[i] |
| 1280 | |
| 1281 | def __add__(self, other): |
| 1282 | if isinstance(other, UserList): |
| 1283 | return self.__class__(self.data + other.data) |
| 1284 | elif isinstance(other, type(self.data)): |
| 1285 | return self.__class__(self.data + other) |
no outgoing calls