references/pagination.md
2.4 KB查看排版效果| 1 | # Pagination: follow(), everything(), Generators |
| 2 | |
| 3 | Pyzotero returns 100 items by default. Use these methods to retrieve more. |
| 4 | |
| 5 | ## everything() — Retrieve All Results |
| 6 | |
| 7 | The simplest way to get all items: |
| 8 | |
| 9 | ```python |
| 10 | # All items in the library |
| 11 | all_items = zot.everything(zot.items()) |
| 12 | |
| 13 | # All top-level items |
| 14 | all_top = zot.everything(zot.top()) |
| 15 | |
| 16 | # All items in a collection |
| 17 | all_col = zot.everything(zot.collection_items('COLKEY')) |
| 18 | |
| 19 | # All items matching a search |
| 20 | all_results = zot.everything(zot.items(q='machine learning', itemType='journalArticle')) |
| 21 | ``` |
| 22 | |
| 23 | Use `everything()` with list-returning endpoints (items, collections, tags, searches) and BibTeX databases. Do not pass dict responses (`format=versions`, `deleted`, `settings`) or bytes (`format=keys`): their iteration semantics differ. |
| 24 | |
| 25 | ## follow() — Sequential Pagination |
| 26 | |
| 27 | ```python |
| 28 | # Retrieve items in batches, manually advancing the page |
| 29 | first_batch = zot.top(limit=25) |
| 30 | second_batch = zot.follow() # next 25 items |
| 31 | third_batch = zot.follow() # next 25 items |
| 32 | ``` |
| 33 | |
| 34 | `follow()` returns `None` when no next-page link exists. `iterfollow()` and `makeiter()` end by raising `StopIteration` through normal generator exhaustion. Do not interleave other reads on the same client while paging: they replace its pagination links. |
| 35 | |
| 36 | ## iterfollow() — Generator |
| 37 | |
| 38 | ```python |
| 39 | # Create a generator over follow() |
| 40 | first = zot.top(limit=10) |
| 41 | lazy = zot.iterfollow() |
| 42 | |
| 43 | # Retrieve subsequent pages |
| 44 | second = next(lazy) |
| 45 | third = next(lazy) |
| 46 | ``` |
| 47 | |
| 48 | ## makeiter() — Generator over Any Method |
| 49 | |
| 50 | ```python |
| 51 | # Create a generator directly from a method call |
| 52 | gen = zot.makeiter(zot.top(limit=25)) |
| 53 | |
| 54 | page1 = next(gen) # first 25 items, fetched AGAIN by makeiter() |
| 55 | page2 = next(gen) # next 25 items |
| 56 | # Raises StopIteration when exhausted |
| 57 | ``` |
| 58 | |
| 59 | ## Manual start/limit Pagination |
| 60 | |
| 61 | ```python |
| 62 | page_size = 50 |
| 63 | offset = 0 |
| 64 | |
| 65 | while True: |
| 66 | batch = zot.items(limit=page_size, start=offset) |
| 67 | if not batch: |
| 68 | break |
| 69 | # process batch |
| 70 | for item in batch: |
| 71 | process(item) |
| 72 | offset += page_size |
| 73 | ``` |
| 74 | |
| 75 | ## Performance Notes |
| 76 | |
| 77 | - `everything()` makes multiple API calls sequentially; large libraries may take time. |
| 78 | - For incremental synchronization, combine `since=version` with `deleted(since=version)` and account for changes during paging; see [read-api.md](read-api.md). |
| 79 | - The raw local API has no default limit, but Pyzotero still injects 100 unless you pass `limit=None`. Local reads can use `zot.items(limit=None)`; remote JSON reads remain paginated. |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

