Skip to content

Instantly share code, notes, and snippets.

@johanste
Created May 31, 2019 03:00
Show Gist options
  • Save johanste/02a73944f670c6ba8b2105642f4c0813 to your computer and use it in GitHub Desktop.
Save johanste/02a73944f670c6ba8b2105642f4c0813 to your computer and use it in GitHub Desktop.
Provide a paged pageable over paged
class FakePagePaged:
""" Iterable over pages of responses
Only intended to show a straw-man for how it could be implemented
"""
def __init__(self, item_paged):
self._item_paged = item_paged
def __iter__(self):
return self
def __next__(self):
self._item_paged._fetch_more()
self._item_paged._count -= len(self._item_paged.items)
return self._item_paged.items
class FakeItemPaged:
def __init__(self, *, diagnostics_response_hook=None, max_items=97):
self._diagnostics_response_hook=diagnostics_response_hook
self.items = []
self._index = -1
self._count = max_items
def _fetch_more(self):
self.items = list(range(20))
if self._count < 0:
# Added to make sure that the pagedpaged iterator will terminate.
# Not correctly implemented - only here to allow showcasing how the
# diagnostics hook can be used for pagedpaged...
raise StopIteration()
if self._diagnostics_response_hook:
self._diagnostics_response_hook(self.items)
def __next__(self):
self._index += 1
self._count -= 1
if self._count < 0:
raise StopIteration()
if self._index >= len(self.items):
self._fetch_more()
self._index = 0
return self.items[self._index]
def __iter__(self):
return self
def by_page(self):
return FakePagePaged(self)
class FakeService:
def list_things(self, diagnostics_response_hook=None):
return iter(FakeItemPaged(diagnostics_response_hook=diagnostics_response_hook))
class ItemCountHook:
def __init__(self):
self.page_count = 0
self.count = 0
def __call__(self, items):
self.page_count += 1
self.count += len(items)
svc = FakeService()
hook = ItemCountHook()
for thing in svc.list_things(diagnostics_response_hook=hook).by_page():
print(thing)
print(hook.count)
print(hook.page_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment