Skip to content

Instantly share code, notes, and snippets.

@lambdamusic
Created February 22, 2018 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambdamusic/de961458321bc7d9b2c461e2e0667266 to your computer and use it in GitHub Desktop.
Save lambdamusic/de961458321bc7d9b2c461e2e0667266 to your computer and use it in GitHub Desktop.
Python Index Slicing implementation
def __getitem__(self, key):
"""
Basic slicing support up to 100 (QueryManager.BATCH_DEFAULT_SIZE)
https://docs.python.org/3/reference/datamodel.html#object.__getitem__
This is just quick way to get samples items, as it works only for the initial batch of results.
Use the next() method to go through all the resultset.
"""
if isinstance( key, slice ) :
#Get the start, stop, and step from the slice
return [self.model(payload=self.hits[ii]) for ii in xrange(*key.indices(self.hits_total))]
elif isinstance( key, int ) :
if key < self.hits_total:
return self.model(payload=self.hits[key])
else:
printDebug("Slicing should be used only for sampling first batch of records (max 100). Use next() method instead.")
raise IndexError, "The index (%d) is out of range."%key
else:
raise TypeError, "Invalid argument type."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment