Retrieves an item or slice from the set of results.
(self, k)
| 274 | return True |
| 275 | |
| 276 | def __getitem__(self, k): |
| 277 | """ |
| 278 | Retrieves an item or slice from the set of results. |
| 279 | """ |
| 280 | if not isinstance(k, (slice, int)): |
| 281 | raise TypeError |
| 282 | assert (not isinstance(k, slice) and (k >= 0)) or ( |
| 283 | isinstance(k, slice) |
| 284 | and (k.start is None or k.start >= 0) |
| 285 | and (k.stop is None or k.stop >= 0) |
| 286 | ), "Negative indexing is not supported." |
| 287 | |
| 288 | # Remember if it's a slice or not. We're going to treat everything as |
| 289 | # a slice to simply the logic and will `.pop()` at the end as needed. |
| 290 | if isinstance(k, slice): |
| 291 | is_slice = True |
| 292 | start = k.start |
| 293 | |
| 294 | if k.stop is not None: |
| 295 | bound = int(k.stop) |
| 296 | else: |
| 297 | bound = None |
| 298 | else: |
| 299 | is_slice = False |
| 300 | start = k |
| 301 | bound = k + 1 |
| 302 | |
| 303 | # We need check to see if we need to populate more of the cache. |
| 304 | if len(self._result_cache) <= 0 or ( |
| 305 | None in self._result_cache[start:bound] and not self._cache_is_full() |
| 306 | ): |
| 307 | try: |
| 308 | self._fill_cache(start, bound) |
| 309 | except StopIteration: |
| 310 | # There's nothing left, even though the bound is higher. |
| 311 | pass |
| 312 | |
| 313 | # Cache should be full enough for our needs. |
| 314 | if is_slice: |
| 315 | return self._result_cache[start:bound] |
| 316 | return self._result_cache[start] |
| 317 | |
| 318 | # Methods that return a SearchQuerySet. |
| 319 | def all(self): # noqa A003 |