Created
March 6, 2014 11:20
-
-
Save mmerce/9387590 to your computer and use it in GitHub Desktop.
While loop to list the BigML sources created from a date on using the python bindings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bigml.api import BigML, FINISHED | |
api = BigML() | |
DATE = '2013-10-05T00:25:36.973000' | |
PAGE_LENGTH = 20 # number of resources to be retrieved in one list call | |
# q_w: query_string with the query parameters. In this case, creation data and | |
# maximum number of resources to be returned. | |
QUERY_STRING = "created__gte=%s" % DATE | |
# api_function: function that should be used to retrieve the resources. In this | |
# case we use the list_sources call to retrieve sources. We could use any | |
# other list call like api.list_datasets, api.list_models... | |
api_function = api.list_sources; | |
# complete query string, with status.code to ensure only finished resources are | |
# retrieved, and limit parameters to control pagination | |
q_s = 'status.code=%s;limit=%s;%s' % ( | |
FINISHED, PAGE_LENGTH, QUERY_STRING) | |
resources = api_function(q_s) | |
ids = [obj['resource'] for obj in (resources['objects'] or [])] | |
while (resources['objects'] and | |
(resources['meta']['total_count'] > (resources['meta']['offset'] + | |
resources['meta']['limit']))): | |
offset = resources['meta']['offset'] + PAGE_LENGTH | |
# adding offset parameter to query string | |
q_s = 'status.code=%s;offset=%s;limit=%s;%s' % ( | |
FINISHED, offset, PAGE_LENGTH, QUERY_STRING) | |
resources = api_function(q_s) | |
if resources['objects']: | |
ids.extend([obj['resource'] for obj in resources['objects']]) | |
print ids |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment