support slices in FeaturesArray and KeysArray

This commit is contained in:
Daniel Pavel 2012-12-13 03:34:39 +02:00
parent 2d338ffbfb
commit b99ccdf612
1 changed files with 26 additions and 18 deletions

View File

@ -182,7 +182,7 @@ class FeaturesArray(object):
def __getitem__(self, index):
if self._check():
assert type(index) == int
if isinstance(index, int):
if index < 0 or index >= len(self.features):
raise IndexError(index)
@ -194,6 +194,10 @@ class FeaturesArray(object):
return self.features[index]
elif isinstance(index, slice):
indices = index.indices(len(self.features))
return [self.__getitem__(i) for i in range(*indices)]
def __contains__(self, value):
if self._check():
ivalue = int(value)
@ -262,7 +266,7 @@ class KeysArray(object):
self.keys = [None] * count
def __getitem__(self, index):
assert type(index) == int
if isinstance(index, int):
if index < 0 or index >= len(self.keys):
raise IndexError(index)
@ -274,6 +278,10 @@ class KeysArray(object):
return self.keys[index]
elif isinstance(index, slice):
indices = index.indices(len(self.keys))
return [self.__getitem__(i) for i in range(*indices)]
def index(self, value):
for index, k in enumerate(self.keys):
if k is not None and int(value) == int(k.key):