From 610e8027f4ee2734f40b11408ff583f014fa6ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregor=20Jer=C5=A1e?= Date: Wed, 16 Apr 2025 14:45:50 +0200 Subject: [PATCH] Add support for values to query --- src/resdk/query.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/resdk/query.py b/src/resdk/query.py index df7e0777..7dce2e40 100644 --- a/src/resdk/query.py +++ b/src/resdk/query.py @@ -230,8 +230,8 @@ def _populate_resource(self, data: dict) -> BaseResource: resolwe=self.resolwe, **data, initial_data_source=DataSource.SERVER ) - def _fetch(self): - """Make request to the server and populate cache.""" + def _fetch_data(self): + """Fetch data from the server.""" if self._cache is not None: # Already fetched. return @@ -248,12 +248,17 @@ def _fetch(self): # Extract data from paginated response if isinstance(items, dict) and "results" in items: - self._count = items["count"] + count = items["count"] items = items["results"] # Store count when list of objects is received without limit. if isinstance(items, list) and self._limit is None: - self._count = len(items) + count = len(items) + return items, count + def _fetch(self): + """Make request to the server and populate cache.""" + items, count = self._fetch_data() + self._count = count self._cache = [self._populate_resource(data) for data in items] def clear_cache(self): @@ -276,6 +281,17 @@ def count(self) -> int: remaining = self._count - self._offset return max(0, min(self._limit, remaining)) + def values(self, fields: list[str]) -> list[dict]: + """Return only values for the given fields. + + Used to reduce the data transferred from the server. + + TODO: Should we support pagination? How do we support it? + """ + field_query = self.filter(fields=fields) + items, _count = field_query._fetch_data() + return items + def get(self, *args, **kwargs): """Get object that matches given parameters.