Skip to content

Instantly share code, notes, and snippets.

@zhaowb
Last active June 5, 2019 22:53
Show Gist options
  • Save zhaowb/7d5eac4fc6ef20ee29fa6907e558438d to your computer and use it in GitHub Desktop.
Save zhaowb/7d5eac4fc6ef20ee29fa6907e558438d to your computer and use it in GitHub Desktop.
stripe pagination using auto_paging_iter()
In [2]: inv100 = stripe.Invoice.list(limit=100)
In [8]: from itertools import islice
In [9]: [(i, inv.id, inv.created) for i, inv in islice(enumerate(inv100.data), 20)]
Out[9]:
[(0, 'in_1EhxnEGObuJZja1rsNGjJcNe', 1559736000),
(1, 'in_1EhxnFGObuJZja1rT295LqNl', 1559736000),
(2, 'in_1EhxnFGObuJZja1r9xZutCpP', 1559736000),
(3, 'in_1EhxnGGObuJZja1riY3HzPDl', 1559736000),
(4, 'in_1EhxnHGObuJZja1rjPxGOAXv', 1559736000),
(5, 'in_1EhxnHGObuJZja1r26YTCOYo', 1559736000),
(6, 'in_1EhxnHGObuJZja1rtylL8r3s', 1559736000),
(7, 'in_1EhxnKGObuJZja1r8izx6ajn', 1559736000),
(8, 'in_1EhxnOGObuJZja1raR93Fhyj', 1559736000),
(9, 'in_1EhpoYGObuJZja1rAGjfllD5', 1559705366),
(10, 'in_1EhpZQGObuJZja1rzhCSDP52', 1559704428),
(11, 'in_1EhpQoGObuJZja1rKMUqhgzi', 1559703894),
(12, 'in_1EhpDmGObuJZja1rWY6p1bu4', 1559703086),
(13, 'in_1Ehof5GObuJZja1rdE8RvDct', 1559700935),
(14, 'in_1EhoUDGObuJZja1rRd7Eg6Wk', 1559700261),
(15, 'in_1EhoFKGObuJZja1rVFfyhiI2', 1559699338),
(16, 'in_1EhnrUGObuJZja1r4O4sanRZ', 1559697859),
(17, 'in_1EhnSVGObuJZja1rqEruzhKT', 1559696311),
(18, 'in_1EhnL6GObuJZja1rflxNPnuo', 1559695851),
(19, 'in_1EhkDiGObuJZja1rsEEvgM7i', 1559683862)]
In [10]: pg1_of_sz5 = stripe.Invoice.list(limit=5)
In [11]: pg2_of_sz5 = stripe.Invoice.list(limit=5, starting_after=pg1_of_sz5.data[-1].id)
In [12]: [(i, inv.id, inv.created) for i, inv in islice(enumerate(pg1_of_sz5.data), 20)]
Out[12]:
[(0, 'in_1EhxnEGObuJZja1rsNGjJcNe', 1559736000),
(1, 'in_1EhxnFGObuJZja1rT295LqNl', 1559736000),
(2, 'in_1EhxnFGObuJZja1r9xZutCpP', 1559736000),
(3, 'in_1EhxnGGObuJZja1riY3HzPDl', 1559736000),
(4, 'in_1EhxnHGObuJZja1rjPxGOAXv', 1559736000)]
# page 2 of size 5 skipped all records created at second 1559736000
In [13]: [(i, inv.id, inv.created) for i, inv in islice(enumerate(pg2_of_sz5.data), 20)]
Out[13]:
[(0, 'in_1EhpoYGObuJZja1rAGjfllD5', 1559705366),
(1, 'in_1EhpZQGObuJZja1rzhCSDP52', 1559704428),
(2, 'in_1EhpQoGObuJZja1rKMUqhgzi', 1559703894),
(3, 'in_1EhpDmGObuJZja1rWY6p1bu4', 1559703086),
(4, 'in_1Ehof5GObuJZja1rdE8RvDct', 1559700935)]
# auto_paging_iter() also skips records at second 1559736000 from page 2
In [14]: [(i, inv.id, inv.created) for i, inv in islice(enumerate(pg1_of_sz5.auto_paging_iter()), 10)]
Out[14]:
[(0, 'in_1EhxnEGObuJZja1rsNGjJcNe', 1559736000),
(1, 'in_1EhxnFGObuJZja1rT295LqNl', 1559736000),
(2, 'in_1EhxnFGObuJZja1r9xZutCpP', 1559736000),
(3, 'in_1EhxnGGObuJZja1riY3HzPDl', 1559736000),
(4, 'in_1EhxnHGObuJZja1rjPxGOAXv', 1559736000),
(5, 'in_1EhpoYGObuJZja1rAGjfllD5', 1559705366),
(6, 'in_1EhpZQGObuJZja1rzhCSDP52', 1559704428),
(7, 'in_1EhpQoGObuJZja1rKMUqhgzi', 1559703894),
(8, 'in_1EhpDmGObuJZja1rWY6p1bu4', 1559703086),
(9, 'in_1Ehof5GObuJZja1rdE8RvDct', 1559700935)]
# retry with page size 3
In [16]: [(i, inv.id, inv.created) for i, inv in islice(enumerate(stripe.Invoice.list(limit=3).auto_paging_iter()), 10)]
Out[16]:
[(0, 'in_1EhxnEGObuJZja1rsNGjJcNe', 1559736000),
(1, 'in_1EhxnFGObuJZja1rT295LqNl', 1559736000),
(2, 'in_1EhxnFGObuJZja1r9xZutCpP', 1559736000),
(3, 'in_1EhpoYGObuJZja1rAGjfllD5', 1559705366),
(4, 'in_1EhpZQGObuJZja1rzhCSDP52', 1559704428),
(5, 'in_1EhpQoGObuJZja1rKMUqhgzi', 1559703894),
(6, 'in_1EhpDmGObuJZja1rWY6p1bu4', 1559703086),
(7, 'in_1Ehof5GObuJZja1rdE8RvDct', 1559700935),
(8, 'in_1EhoUDGObuJZja1rRd7Eg6Wk', 1559700261),
(9, 'in_1EhoFKGObuJZja1rVFfyhiI2', 1559699338)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment