icat.listproxy — Provide the ListProxy class

Note

This module is mostly intended for the internal use in python-icat. Most users will not need to use it directly or even care about it.

class icat.listproxy.ListProxy(target)

Bases: MutableSequence

A list that acts as a proxy to another list.

ListProxy mirrors a target list: all items are stored in the target and fetched back from the target on request. In all other aspects, ListProxy tries to mimic as close as possible the behavior of an ordinary list.

This class tries to be a minimal working implementation. Methods like append() and extent() have deliberately not been implemented here. These operations fall back on the versions inherited from collections.MutableSequence that are based on the elementary methods. This may be less efficient then proxying the operations directly to the target, but this way its easier to override the elementary methods.

>>> l = [ 0, 1, 2, 3, 4 ]
>>> lp = ListProxy(l)
>>> lp
[0, 1, 2, 3, 4]
>>> lp[2]
2
>>> lp[2:4]
[2, 3]
>>> lp == l
True
>>> lp < l
False
>>> l < lp
False
>>> lp < [0, 1, 2, 3, 4, 0]
True
>>> [0, 1, 2, 3, 3] > lp
False
>>> lp[2:4] = ["two", "three"]
>>> lp
[0, 1, 'two', 'three', 4]
>>> l
[0, 1, 'two', 'three', 4]
>>> lp *= 2
>>> l
[0, 1, 'two', 'three', 4, 0, 1, 'two', 'three', 4]
>>> del lp[5:]
>>> l
[0, 1, 'two', 'three', 4]
>>> lp += ['...', 'and', 'so', 'on']
>>> l
[0, 1, 'two', 'three', 4, '...', 'and', 'so', 'on']
>>> l[0:] = [ 1, 'b', 'iii' ]
>>> ll = [ 'x', 'y' ]
>>> lp + ll
[1, 'b', 'iii', 'x', 'y']
>>> ll + lp
['x', 'y', 1, 'b', 'iii']
>>> lp * 3
[1, 'b', 'iii', 1, 'b', 'iii', 1, 'b', 'iii']
insert(index, value)

S.insert(index, value) – insert value before index