Created
February 22, 2018 16:03
-
-
Save lambdamusic/de961458321bc7d9b2c461e2e0667266 to your computer and use it in GitHub Desktop.
Python Index Slicing implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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