enable slicing of SSZ lists/vectors

This commit is contained in:
protolambda 2019-06-20 22:34:19 +02:00
parent 4dcfee2d2c
commit d8f470bb4a
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
1 changed files with 8 additions and 7 deletions

View File

@ -295,13 +295,14 @@ class BaseList(list, Elements):
cls = self.__class__ cls = self.__class__
return f"{cls.__name__}[{cls.elem_type.__name__}, {cls.length}]({', '.join(str(v) for v in self)})" return f"{cls.__name__}[{cls.elem_type.__name__}, {cls.length}]({', '.join(str(v) for v in self)})"
def __getitem__(self, i) -> SSZValue: def __getitem__(self, k) -> SSZValue:
if i < 0: if isinstance(k, int): # check if we are just doing a lookup, and not slicing
raise IndexError(f"cannot get item in type {self.__class__} at negative index {i}") if k < 0:
if i > len(self): raise IndexError(f"cannot get item in type {self.__class__} at negative index {k}")
if k > len(self):
raise IndexError(f"cannot get item in type {self.__class__}" raise IndexError(f"cannot get item in type {self.__class__}"
f" at out of bounds index {i}") f" at out of bounds index {k}")
return super().__getitem__(i) return super().__getitem__(k)
def __setitem__(self, k, v): def __setitem__(self, k, v):
if k < 0: if k < 0: