Last active
December 31, 2015 06:29
-
-
Save jcarbaugh/7947803 to your computer and use it in GitHub Desktop.
A paging wrapper for python-sunlight?
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
import sunlight.services.congress | |
class PseudoService(object): | |
def __init__(self): | |
self.service = self.service_class() | |
def __getattr__(self, name): | |
attr = getattr(self.service, name) | |
if callable(attr): | |
def paginator(*args, **kwargs): | |
count = 0 | |
per_page = int(kwargs.get('per_page', 50)) | |
limit = int(kwargs.pop('limit', per_page)) | |
kwargs['per_page'] = per_page | |
kwargs['page'] = 1 | |
stopthepresses = False | |
while 1: | |
kwargs['page'] += 1 | |
resp = attr(*args, **kwargs) | |
for rec in resp: | |
yield rec | |
count += 1 | |
if count >= limit: | |
stopthepresses = True | |
break | |
if resp._meta['page']['count'] < resp._meta['page']['per_page']: | |
stopthepresses = True | |
if stopthepresses: | |
break | |
return paginator | |
return callable | |
class Congress(PseudoService): | |
service_class = sunlight.services.congress.Congress | |
congress = Congress() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment