Skip to content

Commit b3bb115

Browse files
Added a function to automatically loop over paged calls
1 parent c5c1dc3 commit b3bb115

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

openml/utils.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,33 @@ def extract_xml_tags(xml_tag_name, node, allow_none=True):
3737
return None
3838
else:
3939
raise ValueError("Could not find tag '%s' in node '%s'" %
40-
(xml_tag_name, str(node)))
40+
(xml_tag_name, str(node)))
41+
42+
def list_all(listing_call, *args, **filters):
43+
"""Helper to handle paged listing requests.
44+
Example usage: evaluations = list_all(list_evaluations, "predictive_accuracy", task=mytask)
45+
Note: I wanted to make this a generator, but this is not possible since all listing calls return dicts
46+
47+
Parameters
48+
----------
49+
listing_call : object
50+
Name of the listing call, e.g. list_evaluations
51+
*args : Variable length argument list
52+
Any required arguments for the listing call
53+
**filters : Arbitrary keyword arguments
54+
Any filters that need to be applied
55+
56+
Returns
57+
-------
58+
object
59+
"""
60+
batch_size = 10000
61+
page = 0
62+
has_more = 1
63+
result = {}
64+
while has_more:
65+
new_batch = listing_call(*args, size=batch_size, offset=batch_size*page, **filters)
66+
result.update(new_batch)
67+
page += 1
68+
has_more = (len(new_batch) == batch_size)
69+
return result

0 commit comments

Comments
 (0)