Plan offset pagination with an explicit bounded result count
Python
Calculate pages and the final partial page without contacting an API.
Calculate pages and the final partial page without contacting an API. This stdin-based Python CLI processes one bounded JSON request and returns the documented JSON result. It does not execute jobs or contact external services. Inspect its limits before adapting it to an application.
Before you start
Python 3.12 standard library; no third-party packages. Verified with the recorded pinned runtime.
One UTF-8 JSON object on stdin, no command-line arguments. Maximum encoded request size is 20000 bytes.
Build and check
Save the source as plan-offset-pagination-python.py.
Save the example JSON below as request.json.
Run python3 plan-offset-pagination-python.py < request.json.
Compare the parsed JSON result with the expected output. Invalid input exits 2 with empty stdout and a generic stderr message.
Limits to understand
This is a planning helper only: it does not contact an API or create a consistent snapshot of a changing data source.
The supplied total must already be known and within the specified bound.
Output {"pages":ceil(total/page_size),"last_offset":null if total=0 else (pages-1)*page_size,"last_count":0 if total=0 else total-last_offset}. No floating-point arithmetic.
A planning helper only: it does not create a consistent snapshot of a changing data source.
The complete encoded input is limited to 20000 bytes; one additional byte can be read to detect overflow.
This is a bounded local helper. Passing the recorded cases does not verify every input, every Python version or an external integration.
This browser preview builds code locally. It does not contact an API or run the code.
Default example ready.
Python example
import json
import sys
def reject_number(_text):
raise ValueError('invalid input')
def unique_object(pairs):
result = {}
for key, value in pairs:
if key in result:
raise ValueError('invalid input')
result[key] = value
return result
def main():
try:
if len(sys.argv) != 1:
raise ValueError('invalid input')
raw = sys.stdin.buffer.read(20001)
if len(raw) > 20000:
raise ValueError('invalid input')
text = raw.decode('utf-8', errors='strict')
data = json.loads(
text,
object_pairs_hook=unique_object,
parse_float=reject_number,
parse_constant=reject_number,
)
if type(data) is not dict or set(data) != {'total', 'page_size'}:
raise ValueError('invalid input')
total = data['total']
page_size = data['page_size']
if type(total) is not int or not 0 <= total <= 1000000:
raise ValueError('invalid input')
if type(page_size) is not int or not 1 <= page_size <= 1000:
raise ValueError('invalid input')
pages = (total + page_size - 1) // page_size
last_offset = (pages - 1) * page_size if total else None
last_count = total - last_offset if total else 0
output = json.dumps(
{
'pages': pages,
'last_offset': last_offset,
'last_count': last_count,
},
separators=(',', ':'),
allow_nan=False,
)
except (ValueError, RecursionError, OverflowError):
sys.stderr.write('Invalid input\n')
return 2
sys.stdout.write(output + '\n')
return 0
if __name__ == '__main__':
sys.exit(main())
What to verify after running
Run the documented example and compare the JSON result.
Confirm malformed requests exit 2 without copying input data into diagnostic messages.
20 independent CLI cases passed on this exact source, including an unexpected-argument check.
Request or format preview
Illustrative preview. No provider response, price or balance is fetched.
Code license: MIT. Keep the license notice with your copy.
Example version 1.0.0 · Documentation reviewed 2026-11-15.
20 recorded CLI cases passed in rootless Docker on the pinned Python 3.12 runtime, with 512 MiB RAM, 1 CPU, no network, and read-only source. Other runtimes and all possible inputs are not verified.