Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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