Skip to content

Instantly share code, notes, and snippets.

@mmerce
Created March 6, 2014 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmerce/9387590 to your computer and use it in GitHub Desktop.
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
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