|
1 | 1 | from __future__ import absolute_import
|
2 | 2 |
|
| 3 | +import sys |
| 4 | + |
3 | 5 | from .proxy import _ItemsResourceProxy, _DownloadableProxyMixin
|
4 | 6 |
|
5 | 7 |
|
@@ -51,6 +53,20 @@ class Items(_DownloadableProxyMixin, _ItemsResourceProxy):
|
51 | 53 | File "<stdin>", line 1, in <module>
|
52 | 54 | StopIteration
|
53 | 55 |
|
| 56 | + - retrieving via meth::`list_iter` also supports the `start` and `count`. |
| 57 | + params. This is useful when you want to only retrieve a subset of items in |
| 58 | + a job. The example below belongs to a job with 10 items:: |
| 59 | +
|
| 60 | + >>> gen = job.items.list_iter(chunksize=2, start=5, size=3) |
| 61 | + >>> next(gen) |
| 62 | + [{'name': 'Item #5'}, {'name': 'Item #6'}] |
| 63 | + >>> next(gen) |
| 64 | + [{'name': 'Item #7'}] |
| 65 | + >>> next(gen) |
| 66 | + Traceback (most recent call last): |
| 67 | + File "<stdin>", line 1, in <module> |
| 68 | + StopIteration |
| 69 | +
|
54 | 70 | - retrieve 1 item with multiple filters::
|
55 | 71 |
|
56 | 72 | >>> filters = [("size", ">", [30000]), ("size", "<", [40000])]
|
@@ -85,18 +101,29 @@ def list_iter(self, chunksize=1000, *args, **kwargs):
|
85 | 101 | You can improve I/O overheads by increasing the chunk value but that
|
86 | 102 | would also increase the memory consumption.
|
87 | 103 |
|
| 104 | + :param chunksize: size of list to be returned per iteration |
| 105 | + :param start: offset to specify the start of the item iteration |
| 106 | + :param count: overall number of items to be returned, which is broken |
| 107 | + down by `chunksize`. |
| 108 | +
|
88 | 109 | :return: an iterator over items, yielding lists of items.
|
89 | 110 | :rtype: :class:`collections.Iterable`
|
90 | 111 | """
|
91 | 112 |
|
| 113 | + start = kwargs.pop("start", 0) |
| 114 | + count = kwargs.pop("count", sys.maxsize) |
92 | 115 | processed = 0
|
| 116 | + |
93 | 117 | while True:
|
94 |
| - next_key = self.key + '/' + str(processed) |
| 118 | + next_key = self.key + "/" + str(start) |
95 | 119 | items = [
|
96 | 120 | item for item in self.iter(
|
97 | 121 | count=chunksize, start=next_key, *args, **kwargs)
|
98 | 122 | ]
|
99 | 123 | yield items
|
100 | 124 | processed += len(items)
|
| 125 | + start += len(items) |
| 126 | + if processed >= count: |
| 127 | + break |
101 | 128 | if len(items) < chunksize:
|
102 | 129 | break
|
0 commit comments