Skip to content

Commit 22f646f

Browse files
committed
Add count param for iter* methods
1 parent 93582c3 commit 22f646f

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

scrapinghub/client/activity.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import absolute_import
22

33
from .proxy import _Proxy
4-
from .utils import parse_job_key
4+
from .utils import parse_job_key, update_kwargs
55

66

77
class Activity(_Proxy):
@@ -44,13 +44,14 @@ class Activity(_Proxy):
4444
>>> project.activity.add(events)
4545
4646
"""
47-
def iter(self, **params):
47+
def iter(self, count=None, **params):
4848
"""Iterate over activity events.
4949
50+
:param count: limit amount of elements.
5051
:return: a generator object over a list of activity event dicts.
5152
:rtype: :class:`types.GeneratorType[dict]`
5253
"""
53-
# TODO describe allowable params
54+
update_kwargs(params, count=count)
5455
params = self._modify_iter_params(params)
5556
return self._origin.list(**params)
5657

scrapinghub/client/collections.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,22 @@ def create_writer(self, start=0, auth=None, size=1000, interval=15,
284284
maxitemsize=1024 ** 2, callback=None):
285285
"""Create a new writer for a collection.
286286
287+
:param start: (optional) initial offset for writer thread.
288+
:param auth: (optional) set auth credentials for the request.
289+
:param size: (optional) set initial queue size.
290+
:param interval: (optional) set interval for writer thread.
291+
:param qsize: (optional) setup max queue size for the writer.
292+
:param content_encoding: (optional) set different Content-Encoding header.
293+
:param maxitemsize: (optional) max item size in bytes.
294+
:param callback: (optional) some callback function.
287295
:return: a new writer object.
288296
:rtype: :class:`~scrapinghub.hubstorage.batchuploader._BatchWriter`
297+
298+
If provided - calllback shouldn't try to inject more items in the queue,
299+
otherwise it can lead to deadlocks.
289300
"""
290-
# TODO describe allowable params
291301
kwargs = {}
292-
update_kwargs(start=start, auth=auth, size=size, interval=interval,
302+
update_kwargs(kwargs, start=start, auth=auth, size=size, interval=interval,
293303
qsize=qsize, content_encoding=content_encoding,
294304
maxitemsize=maxitemsize, callback=callback)
295305
return self._origin._collections.create_writer(

scrapinghub/client/proxy.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55

66
from ..hubstorage import ValueTooLarge as _ValueTooLarge
7+
from .utils import update_kwargs
78
from .exceptions import ValueTooLarge
89

910

@@ -61,13 +62,14 @@ def write(self, item):
6162
except _ValueTooLarge as exc:
6263
raise ValueTooLarge(str(exc))
6364

64-
def iter(self, _key=None, **params):
65+
def iter(self, _key=None, count=None, **params):
6566
"""Iterate over elements in collection.
6667
68+
:param count: limit amount of elements.
6769
:return: a generator object over a list of element dictionaries.
6870
:rtype: :class:`types.GeneratorType[dict]`
6971
"""
70-
# TODO describe allowable params
72+
update_kwargs(params or {}, count=count)
7173
params = self._modify_iter_params(params)
7274
return self._origin.list(_key, **params)
7375

@@ -90,35 +92,40 @@ def close(self, block=True):
9092

9193
class _DownloadableProxyMixin(object):
9294

93-
def iter(self, _path=None, requests_params=None, **apiparams):
95+
def iter(self, _path=None, count=None, requests_params=None, **apiparams):
9496
"""A general method to iterate through elements.
9597
98+
:param count: limit amount of elements.
9699
:return: an iterator over elements list.
97100
:rtype: :class:`collections.Iterable`
98101
"""
99-
# TODO describe allowable params
102+
update_kwargs(apiparams, count=count)
100103
apiparams = self._modify_iter_params(apiparams)
101104
return self._origin.iter_values(_path, requests_params, **apiparams)
102105

103-
def iter_raw_json(self, _path=None, requests_params=None, **apiparams):
106+
def iter_raw_json(self, _path=None, count=None, requests_params=None,
107+
**apiparams):
104108
"""A method to iterate through raw json-packed elements.
105109
Can be convenient if data is needed in raw json format.
106110
111+
:param count: limit amount of elements.
107112
:return: an iterator over elements list packed with json.
108113
:rtype: :class:`collections.Iterable[str]`
109114
"""
110-
# TODO describe allowable params
115+
update_kwargs(apiparams, count=count)
111116
apiparams = self._modify_iter_params(apiparams)
112117
return self._origin.iter_json(_path, requests_params, **apiparams)
113118

114-
def iter_raw_msgpack(self, _path=None, requests_params=None, **apiparams):
119+
def iter_raw_msgpack(self, _path=None, count=None, requests_params=None,
120+
**apiparams):
115121
"""A method to iterate through raw msgpack-ed elements.
116122
Can be convenient if data is needed in same msgpack format.
117123
124+
:param count: limit amount of elements.
118125
:return: an iterator over elements list packed with msgpack.
119126
:rtype: :class:`collections.Iterable[bytes]`
120127
"""
121-
# TODO describe allowable params
128+
update_kwargs(apiparams, count=count)
122129
apiparams = self._modify_iter_params(apiparams)
123130
return self._origin.iter_msgpack(_path, requests_params, **apiparams)
124131

0 commit comments

Comments
 (0)