Last active
January 4, 2017 08:27
-
-
Save obmarg/78453ef18c63b5e72540 to your computer and use it in GitHub Desktop.
Google app engine python issue #7746 workaround
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
from toolz import concat | |
def page_iterator(query, page_size=999, **kwargs): | |
''' | |
Returns an iterator over pages of a query. | |
Can be used to work-around the 1000 entity limit in remote_api_shell | |
:params query: The query we're using. | |
:params page_size: The page size to return. | |
:params qwargs: Additional options for fetch_page | |
''' | |
more = True | |
cursor = None | |
while more: | |
page, cursor, more = query.fetch_page(page_size, start_cursor=cursor, | |
**kwargs) | |
if page: | |
yield page | |
def fetch_all(query): | |
''' | |
Fetches all the results for a query, using page_iterator | |
Should be used to work-around the 1000 entity limit in remote_api_shell. | |
''' | |
return list(concat(page_iterator(query))) | |
def count_results(query): | |
''' | |
Equivalent to query.count() but that works around 1000 limit. | |
''' | |
return reduce(lambda total, r: total + len(r), | |
page_iterator(query, keys_only=True), 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment