Skip to main content

Paginate with iter

For result sets larger than a single page, page through the collection instead of bumping limit. Search has a lazy iter generator, and documents, alerts and the admin sub-managers carry exhaustive list_all / max_items helpers.

import sasvi

client = sasvi.SVIClient.from_env()

for hit in client.search.types("alerts").text("invoice").iter(page_size=100):
process(hit)

Search.iter(...) is a lazy generator that pulls one page at a time and yields each SearchHit. page_size sets the batch size. Stop iterating to stop paging.

When SAND reports a hit count, iteration runs to that count and logs a WARNING if it ends short. When SAND reports no count, iteration stops at the first short page, and a page cap plus a repeated-page check keep a non-advancing server from looping forever.

Document collection​

For every document of a type, use list_all. It pages with batch_size, follows the server's reported total, and raises IncompleteListingError when the server stops advancing:

for doc in client.documents.list_all(
entity_type_name="<your-entity-type>",
batch_size=200,
):
process(doc)

documents.list returns one window, set by start and limit. Do not end a hand-written loop at the first page shorter than limit: Data Hub can serve fewer rows than you asked for and still have more, so that loop can stop after the first page. If you page by hand, advance start by the rows you received, stop on an empty page, and cap the number of pages:

start, page_size, max_pages = 0, 200, 1000
for _ in range(max_pages):
page = client.documents.list(
entity_type_name="<your-entity-type>",
start=start,
limit=page_size,
)
if not page:
break
for doc in page:
process(doc)
start += len(page)
else:
raise RuntimeError(f"stopped after {max_pages} pages; the listing may be incomplete")

Alerts​

for alert in client.alerts.list_all(
batch_size=100,
sort_by="creationTimeStamp:descending",
):
process(alert)

list_all is eager. It pages until the service is exhausted and materializes the full list before returning. To process only a prefix of an already-fetched result, slice it:

# First 250 of the fetched result:
for alert in client.alerts.list_all(batch_size=100)[:250]:
process(alert)

Reference list items​

record = client.admin.reference_lists.get(name="<list-name>")
for item in record.items.list():
print(item.code, item.label)

items.list() returns the full set in one call, because reference lists are typically small. record.items.list_async() is the same call on the async path; it does not page either.

Cap with max_items​

For services that take a max_items kwarg (reference lists, shapes, synonyms):

records = client.admin.reference_lists.list(page_size=200, max_items=1000)

The pager stops fetching at max_items, no matter how many pages remain.

When an exhaustive listing cannot be finished​

The exhaustive list helpers (list_all and the max_items pagers) prefer the server's reported total over a page-size heuristic. If the server stops advancing, or the page cap is reached, they raise IncompleteListingError rather than handing back a short list that looks complete:

from sasvi.errors import IncompleteListingError

try:
documents = client.documents.list_all(entity_type_name="<your-entity-type>")
except IncompleteListingError as exc:
print(exc.reason, exc.next_start, exc.server_total)

Search.iter(...) and SearchResults.iter_all() follow the same rule: a server that stops advancing, or a page ceiling reached, raises rather than handing back a plausible-looking prefix.

Running out of rows before the reported total is the one case that only logs a WARNING by default, because a server-side count can include rows you are not allowed to see, so a short read is not proof of a broken listing. Services that write back what they read opt into raising instead.

Async​

import asyncio
import sasvi


async def main():
async with sasvi.SVIClient.from_env() as client:
async for hit in client.search.text("foo").iter_async(page_size=200):
process(hit)


asyncio.run(main())

Go further​