A more or less complete user-defined wrapper around list objects.
| 1266 | ################################################################################ |
| 1267 | |
| 1268 | class UserList(_collections_abc.MutableSequence): |
| 1269 | """A more or less complete user-defined wrapper around list objects.""" |
| 1270 | |
| 1271 | def __init__(self, initlist=None): |
| 1272 | self.data = [] |
| 1273 | if initlist is not None: |
| 1274 | # XXX should this accept an arbitrary sequence? |
| 1275 | if type(initlist) == type(self.data): |
| 1276 | self.data[:] = initlist |
| 1277 | elif isinstance(initlist, UserList): |
| 1278 | self.data[:] = initlist.data[:] |
| 1279 | else: |
| 1280 | self.data = list(initlist) |
| 1281 | |
| 1282 | def __repr__(self): |
| 1283 | return repr(self.data) |
| 1284 | |
| 1285 | def __lt__(self, other): |
| 1286 | return self.data < self.__cast(other) |
| 1287 | |
| 1288 | def __le__(self, other): |
| 1289 | return self.data <= self.__cast(other) |
| 1290 | |
| 1291 | def __eq__(self, other): |
| 1292 | return self.data == self.__cast(other) |
| 1293 | |
| 1294 | def __gt__(self, other): |
| 1295 | return self.data > self.__cast(other) |
| 1296 | |
| 1297 | def __ge__(self, other): |
| 1298 | return self.data >= self.__cast(other) |
| 1299 | |
| 1300 | def __cast(self, other): |
| 1301 | return other.data if isinstance(other, UserList) else other |
| 1302 | |
| 1303 | def __contains__(self, item): |
| 1304 | return item in self.data |
| 1305 | |
| 1306 | def __len__(self): |
| 1307 | return len(self.data) |
| 1308 | |
| 1309 | def __getitem__(self, i): |
| 1310 | if isinstance(i, slice): |
| 1311 | return self.__class__(self.data[i]) |
| 1312 | else: |
| 1313 | return self.data[i] |
| 1314 | |
| 1315 | def __setitem__(self, i, item): |
| 1316 | self.data[i] = item |
| 1317 | |
| 1318 | def __delitem__(self, i): |
| 1319 | del self.data[i] |
| 1320 | |
| 1321 | def __add__(self, other): |
| 1322 | if isinstance(other, UserList): |
| 1323 | return self.__class__(self.data + other.data) |
| 1324 | elif isinstance(other, type(self.data)): |
| 1325 | return self.__class__(self.data + other) |
no outgoing calls
searching dependent graphs…