|
1 | 1 | import six |
2 | 2 |
|
| 3 | +from openml.exceptions import OpenMLServerException |
| 4 | + |
3 | 5 |
|
4 | 6 | def extract_xml_tags(xml_tag_name, node, allow_none=True): |
5 | 7 | """Helper to extract xml tags from xmltodict. |
@@ -39,31 +41,48 @@ def extract_xml_tags(xml_tag_name, node, allow_none=True): |
39 | 41 | raise ValueError("Could not find tag '%s' in node '%s'" % |
40 | 42 | (xml_tag_name, str(node))) |
41 | 43 |
|
42 | | -def list_all(listing_call, *args, **filters): |
| 44 | +def list_all(listing_call, batch_size=10000, *args, **filters): |
43 | 45 | """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 | + 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 |
46 | 53 | |
47 | 54 | Parameters |
48 | 55 | ---------- |
49 | | - listing_call : object |
50 | | - Name of the listing call, e.g. list_evaluations |
| 56 | + listing_call : callable |
| 57 | + Call listing, e.g. list_evaluations. |
| 58 | + batch_size : int (default: 10000) |
| 59 | + Batch size for paging. |
51 | 60 | *args : Variable length argument list |
52 | | - Any required arguments for the listing call |
| 61 | + Any required arguments for the listing call. |
53 | 62 | **filters : Arbitrary keyword arguments |
54 | | - Any filters that need to be applied |
| 63 | + Any filters that can be applied to the listing function. |
55 | 64 | |
56 | 65 | Returns |
57 | 66 | ------- |
58 | | - object |
| 67 | + dict |
59 | 68 | """ |
60 | | - batch_size = 10000 |
61 | 69 | page = 0 |
62 | | - has_more = 1 |
63 | 70 | result = {} |
64 | | - while has_more: |
65 | | - new_batch = listing_call(*args, size=batch_size, offset=batch_size*page, **filters) |
| 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 |
66 | 85 | result.update(new_batch) |
67 | 86 | page += 1 |
68 | | - has_more = (len(new_batch) == batch_size) |
| 87 | + |
69 | 88 | return result |
0 commit comments