1
# -*- test-case-name: epsilon.test.test_view -*-
4
Utility functionality for creating wrapping sequences so as to transform
5
their indices in some manner.
8
class SlicedView(object):
10
Wrapper around a sequence which allows indexing and non-extended
11
slicing, adjusting all indices using a transformation defined by a
17
t = SlicedView(s, slice(1, None))
20
@ivar sequence: The underlying sequence from which to retrieve elements.
21
@ivar bounds: A C{slice} instance defining the boundaries of this view.
24
def __init__(self, sequence, bounds):
25
self.sequence = sequence
29
def _getIndices(self):
30
start, stop, step = self.bounds.indices(len(self.sequence))
31
indices = xrange(start, stop, step)
35
def __getitem__(self, index):
37
Compute the index in the underlying sequence of the given view index
38
and return the corresponding element.
40
@raise IndexError: If C{index} is out of bounds for the view.
41
@raise ValueError: If C{self.bounds} is out of bounds for
44
if isinstance(index, slice):
45
return SlicedView(self, index)
46
return self.sequence[self._getIndices()[index]]
51
Compute the length of this view onto the sequence and return it.
53
return len(self._getIndices())