def test_mix_slice_and_int(): """Test mixing slices and simple int indices.""" _test_mixing(slice(1, 8), 3) _test_mixing(slice(3, 6), 2) # Index reaching beyond sequence: eq_(mix_slices(slice(2, 5), 7), 2 + 7)
def __getitem__(self, k): """Do a lazy slice of myself, or return a single item from my results. If ``k`` is a number, then it returns the object at index k. If ``k`` is a slice, return a new ``S`` with the requested slice bounds taken into account. If my results have already been fetched, return a real list of results indexed/sliced as requested. :arg k: index or slice to retrieve from the results. Haven't bothered to do the thinking to support slice steps or negative slice components yet. """ if self._raw_cache is not None: return self._results(k) new = self._clone() # Compute a single slice out of any we already have & the new one: new._slice = mix_slices(new._slice, k) if isinstance(k, slice): # k is a slice, so we can be lazy. return new else: # k is a number; we must fetch results. # _sphinx() responds to _slice being a number by getting a single # result, which we return (or have an IndexError about): return list(new)[0]
def _test_mixing(j, k): """Assert that ``[0..20][mix_slices(j, k)] == [0..20][j][k]``.""" seq = range(10) eq_(seq[mix_slices(j, k)], seq[j][k])