Pagination
The server sends long lists a page at a time. The library hides that. You loop over a list as if it were one long list, and the library fetches the next page when it needs one.
Loop over everything
Every list method returns a pager. Loop over it:
from thrustlab import Client
client = Client()
for project in client.projects.list():
print(project["name"])The loop fetches the first page, gives you its items one by one, then fetches the next page, and so on until there are none left. A pager is lazy, so a loop you stop early doesn't fetch the rest.
Take only a few
To stop after a few items, use islice:
from itertools import islice
for motor in islice(client.components.list(type="motor"), 5):
print(motor["name"])Read one page
.data is the first page as a list, and .has_more tells you whether more pages exist:
page = client.components.list(type="propeller", limit=4)
props = page.data
print(len(props), page.has_more)Use this when you only want a few items and a single request is enough.
Change the page size
limit is how many items each page holds. It is also how many items one request returns, so a bigger limit means fewer requests for a long list. Most lists allow 1 to 100. Runs and sweep points allow up to 1000.
for point in client.sweeps.list_points(sweep_id, limit=1000):
...The default in the library is 20 for most lists, 25 for starred parts and 100 for sweep points.
Get one match
Two helpers read only the first page:
first()returns the first item, orNoneif there are none.one()returns the only match. It raises aNotFoundErrorif there is none, and anAmbiguousComponentErrorif there is more than one.
motor = client.components.list(type="motor", search="2826").first()client.components.find(...) is the same as list(...).one().
Good to know
- A pager can be looped over once. To go through the list again, call
listagain. - It is not a list. To get every item in a list, use
list(client.projects.list()).