MCPcopy Create free account
hub / github.com/Materials-Consortia/optimade-python-tools / count

Method count

optimade/client/client.py:358–424  ·  view source on GitHub ↗

Counts the number of results for the filter, requiring only 1 request per provider by making use of the `meta->data_returned` key. If missing, attempts will be made to perform an exponential/binary search over pagination to count the results. Raises: Runt

(
        self, filter: str | None = None, endpoint: str | None = None
    )

Source from the content-addressed store, hash-verified

356 return {endpoint: {filter: {k: results[k].asdict() for k in results}}}
357
358 def count(
359 self, filter: str | None = None, endpoint: str | None = None
360 ) -> dict[str, dict[str, dict[str, int | None]]]:
361 """Counts the number of results for the filter, requiring
362 only 1 request per provider by making use of the `meta->data_returned`
363 key. If missing, attempts will be made to perform an exponential/binary
364 search over pagination to count the results.
365
366 Raises:
367 RuntimeError: If the query could not be executed.
368
369 Returns:
370 A nested mapping from endpoint, filter and base URL to the number of query results.
371
372 """
373
374 if endpoint is None:
375 if self.__current_endpoint is not None:
376 endpoint = self.__current_endpoint
377 self.__current_endpoint = None
378 else:
379 endpoint = "structures"
380
381 if filter is None:
382 filter = ""
383
384 self._progress = OptimadeClientProgress()
385 if self.silent:
386 self._progress.disable = True
387
388 self._check_filter(filter, endpoint)
389
390 with self._progress:
391 if not self.silent:
392 self._progress.print(
393 Panel(
394 f"Counting results for [bold yellow]{endpoint}[/bold yellow]/?filter=[bold magenta][i]{filter}[/i][/bold magenta]",
395 expand=False,
396 )
397 )
398 results = self._execute_queries(
399 filter,
400 endpoint,
401 page_limit=1,
402 paginate=False,
403 response_fields=[],
404 sort=None,
405 )
406 count_results = {}
407
408 for base_url in results:
409 count_results[base_url] = results[base_url].meta.get(
410 "data_returned", None
411 )
412
413 if count_results[base_url] is None or self._force_binary_search:
414 if self.count_binary_search:
415 count_results[base_url] = self.binary_search_count(

Callers 6

_getFunction · 0.95
test_client_endpointsFunction · 0.95
test_multiple_base_urlsFunction · 0.95
check_for_entry_typeMethod · 0.45

Calls 6

_check_filterMethod · 0.95
_execute_queriesMethod · 0.95
binary_search_countMethod · 0.95
printMethod · 0.80
getMethod · 0.45

Tested by 2

test_client_endpointsFunction · 0.76
test_multiple_base_urlsFunction · 0.76