Skip to content

Commit 4673350

Browse files
committed
use Tasklist instead of just a list
1 parent 60cf1dd commit 4673350

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ Check `this`__ for further information.
170170
__ https://docs.scaleapi.com/#list-all-tasks
171171

172172
Retrieve a list of tasks, with optional filter by date/type. Paginated with limit/offset.
173+
The return value is a ``scaleapi.Tasklist``, which acts as a list, but also has fields
174+
for the total number of tasks, the limit and offset, and whether or not there's more.
173175

174176
.. code-block :: python
175177
@@ -180,6 +182,11 @@ Retrieve a list of tasks, with optional filter by date/type. Paginated with limi
180182
limit=100,
181183
offset=200)
182184
185+
print(tasks.total) # 1000
186+
print(tasks.limit) # 100
187+
print(tasks.offset) # 200
188+
print(tasks.has_more) # True
189+
183190
Error handling
184191
==============
185192

scaleapi/__init__.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import namedtuple
12
import requests
23

34
from .tasks import Task
@@ -15,6 +16,8 @@
1516
'objects_to_annotate', 'with_labels'},
1617
'datacollection': {'attachment', 'attachment_type', 'fields'}}
1718
SCALE_ENDPOINT = 'https://api.scaleapi.com/v1/'
19+
DEFAULT_LIMIT = 100
20+
DEFAULT_OFFSET = 0
1821

1922

2023
def validate_payload(task_type, kwargs):
@@ -35,6 +38,16 @@ class ScaleInvalidRequest(ScaleException, ValueError):
3538
pass
3639

3740

41+
class Tasklist(list):
42+
def __init__(self, docs, total, limit, offset, has_more):
43+
super(Tasklist, self).__init__(docs)
44+
self.docs = docs
45+
self.total = total
46+
self.limit = limit
47+
self.offset = offset
48+
self.has_more = has_more
49+
50+
3851
class ScaleClient(object):
3952
def __init__(self, api_key):
4053
self.api_key = api_key
@@ -102,8 +115,10 @@ def tasks(self, **kwargs):
102115
if key not in allowed_kwargs:
103116
raise ScaleInvalidRequest('Illegal parameter %s for ScaleClient.tasks()'
104117
% key, None)
105-
return [Task(json, self) for json in
106-
self._getrequest('tasks', params=kwargs)['docs']]
118+
response = self._getrequest('tasks', params=kwargs)
119+
docs = [Task(json, self) for json in response['docs']]
120+
return Tasklist(docs, response['total'], response['limit'],
121+
response['offset'], response['has_more'])
107122

108123
def create_categorization_task(self, **kwargs):
109124
validate_payload('categorization', kwargs)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
setup(
33
name = 'scaleapi',
44
packages = ['scaleapi'],
5-
version = '0.1.3',
5+
version = '0.1.4',
66
description = 'The official Python client library for the Scale API, the API for human labor.',
77
author = 'Calvin Huang',
88
author_email = 'c@lvin.me',

tests/test_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,12 @@ def test_task_retrieval():
159159

160160
def test_task_retrieval_time():
161161
task = make_a_task()
162+
time.sleep(0.5)
162163
start_time = datetime.utcnow().isoformat()
163-
time.sleep(1)
164+
time.sleep(0.5)
164165
end_time = datetime.utcnow().isoformat()
165166
tasks = client.tasks(start_time=start_time, end_time=end_time)
166-
assert tasks == []
167+
assert tasks.docs == []
167168

168169

169170
def test_task_retrieval_fail():

0 commit comments

Comments
 (0)