11import six
22
3+ from openml .exceptions import OpenMLServerException
4+
35
46def extract_xml_tags (xml_tag_name , node , allow_none = True ):
57 """Helper to extract xml tags from xmltodict.
@@ -37,4 +39,50 @@ def extract_xml_tags(xml_tag_name, node, allow_none=True):
3739 return None
3840 else :
3941 raise ValueError ("Could not find tag '%s' in node '%s'" %
40- (xml_tag_name , str (node )))
42+ (xml_tag_name , str (node )))
43+
44+ def list_all (listing_call , batch_size = 10000 , * args , ** filters ):
45+ """Helper to handle paged listing requests.
46+
47+ Example usage:
48+
49+ ``evaluations = list_all(list_evaluations, "predictive_accuracy", task=mytask)``
50+
51+ Note: I wanted to make this a generator, but this is not possible since all
52+ listing calls return dicts
53+
54+ Parameters
55+ ----------
56+ listing_call : callable
57+ Call listing, e.g. list_evaluations.
58+ batch_size : int (default: 10000)
59+ Batch size for paging.
60+ *args : Variable length argument list
61+ Any required arguments for the listing call.
62+ **filters : Arbitrary keyword arguments
63+ Any filters that can be applied to the listing function.
64+
65+ Returns
66+ -------
67+ dict
68+ """
69+ page = 0
70+ result = {}
71+
72+ while True :
73+ try :
74+ new_batch = listing_call (
75+ * args ,
76+ size = batch_size ,
77+ offset = batch_size * page ,
78+ ** filters
79+ )
80+ except OpenMLServerException as e :
81+ if page == 0 and e .args [0 ] == 'No results' :
82+ raise e
83+ else :
84+ break
85+ result .update (new_batch )
86+ page += 1
87+
88+ return result
0 commit comments