Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Created February 22, 2019 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miraculixx/230a9bbc895a86f785597b1b8298b4f4 to your computer and use it in GitHub Desktop.
Save miraculixx/230a9bbc895a86f785597b1b8298b4f4 to your computer and use it in GitHub Desktop.
sodapy.Socrata extenion to page through results using a single call to get
from sodapy import Socrata
class SocrataPager(Socrata):
def get(self, dataset, *args, limit=None, offset=0, where=None, order=None, page_size=None, **kwargs):
""" get dataset by pages or within limit
uses Socrata.get to page through results until limit is reached. This adds the page_size parameter
which is passed as the limit kwarg to Socrata.get. limit is the total number of records to
return, across all pages. Note that the method is a generator.
See https://dev.socrata.com/docs/paging.html
"""
done = False
while not done:
results = super().get(dataset, *args, limit=page_size, offset=offset, where=where, order=order, **kwargs)
for item in results[slice(0, limit)]:
yield item
offset += len(results)
done = (len(results) == 0) or (limit and offset >= limit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment