Skip to content

Instantly share code, notes, and snippets.

@jg75
Last active August 29, 2015 14:26
Show Gist options
  • Save jg75/fd12540850a60c5b9387 to your computer and use it in GitHub Desktop.
Save jg75/fd12540850a60c5b9387 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Just to get a random list of numbers
import random
id_list = range(1, 101)
find_list = random.sample(id_list, random.randint(1, len(id_list)))
find_list.sort()
def get_ids(offset, limit):
if offset + limit <= len(id_list):
return id_list[offset:offset + limit]
elif offset <= len(id_list):
return id_list[offset:]
else:
return []
def get_by_slices():
offset = 0
chunk_count = 0
chunk_size = 10
while True:
results = get_ids(offset, chunk_size)
offset += chunk_size
chunk_count += 1
if not results:
break;
print "%" * 20
print "Start Chunk: %s" % chunk_count
print "%" * 20
for result in results:
if result in find_list:
print result
def get_ids_improved(filters={}):
numbers = filters.get('numbers')
if numbers:
return [ number for number in id_list if number in numbers ]
return id_list
if __name__ == '__main__':
print "We're looking for these numbers: ", find_list
print
print "%" * 50
print "Example 1: How you would think getting slices by"
print "an offset and limit would work"
print "%" * 50
get_by_slices()
print
print "%" * 50
print "Example 2: Amazon's API's typically provide"
print "an optional filters argument like this"
print "%" * 50
print get_ids_improved(filters={ "numbers": find_list })
print
print "%" * 50
print "Same as above with no filter returns everything"
print "%" * 50
print get_ids_improved()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment