2
0

paging_and_iteration.rst 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. Paging and iteration
  2. ====================
  3. Overview
  4. --------
  5. Most provider services have list() methods, and all list methods accept a limit
  6. parameter which specifies the maximum number of results to return. If a limit
  7. is not specified, CloudBridge will default to the global configuration variable
  8. `default_result_limit`, which can be modified through the provider config.
  9. Since the returned result list may have more records available, CloudBridge
  10. will always return a :py:class:`ResultList` object to assist with paging through
  11. additional results. A ResultList extends the standard :py:class:`list` and
  12. the following example illustrates how to fetch additional records.
  13. Example:
  14. .. code-block:: python
  15. # get first page of results
  16. rl = provider.compute.instances.list(limit=50)
  17. for result in rl:
  18. print("Instance Data: {0}", result)
  19. if rl.supports_total:
  20. print("Total results: {0}".format(rl.total_results))
  21. else:
  22. print("Total records unknown,"
  23. "but has more data?: {0}."format(rl.is_truncated))
  24. # Page to next set of results
  25. if (rl.is_truncated)
  26. rl = provider.compute.instances.list(limit=100,
  27. marker=rl.marker)
  28. To ease development, CloudBridge also provides standard Python iterators that
  29. will page the results in for you automatically. Therefore, when you need to
  30. iterate through all available objects, the following shorthand is recommended:
  31. Example:
  32. .. code-block:: python
  33. # Iterate through all results
  34. for instance in provider.compute.instances:
  35. print("Instance Data: {0}", instance)