def __getitem__(self, key):
bounds, values = self._bounds, self._values
if hasattr(key, 'start') and hasattr(key, 'stop'):
# we are indexing with an interval. we only return the value
# if that interval is contained within one of our intervals.
start, stop = key.start, key.stop
lindex = bisect_right(bounds, start) if start is not None else 0
rindex = (bisect_left(bounds, stop)
if stop is not None else len(bounds))
if lindex != rindex:
raise KeyError(key)
return values[lindex]
else:
# We are indexing with a single element.
result = values[bisect_right(bounds, key)]
if result is self.nothing:
raise KeyError(key)
return result
评论列表
文章目录