Complete worked example
python
def inspect_example(rows, size):
if type(size) is not int or size <= 0:
raise ValueError('positive page size required')
ordered = sorted(rows, key=lambda r: (r['tick'], r['id']))
cursor = (-1, '')
pages = []
while True:
page = [r for r in ordered if (r['tick'], r['id']) > cursor][:size]
if not page:
break
pages.append([r['id'] for r in page])
cursor = (page[-1]['tick'], page[-1]['id'])
return {'pages': pages, 'cursor': list(cursor)}- Reasoning
- The program sorts records by tick and id, then selects the next size records whose tuple is strictly greater than the current cursor. After a nonempty page, the last record's tuple becomes the next cursor. An empty page terminates the extraction, preserving source order across pages.