Skip to content

Commit

Permalink
Revert "Issues/37 Add function for returning an iterator instead of s…
Browse files Browse the repository at this point in the history
…equence" (#90)
  • Loading branch information
frankinspace authored Sep 24, 2024
1 parent bda1cff commit 9936bfe
Show file tree
Hide file tree
Showing 14 changed files with 10,291 additions and 32,332 deletions.
6 changes: 0 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added
- New function `get_iter` for returning results as an iterator instead of sequence ([#37](https://github.com/nasa/python_cmr/issues/37))

### Deprecated
- Function `get` has been marked as deprecated in favor of the new `get_iter` function. `get` will likely be removed for the 1.0.0 release. ([#37](https://github.com/nasa/python_cmr/issues/37))

## [0.13.0]

### Added
Expand Down
55 changes: 5 additions & 50 deletions cmr/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from datetime import date, datetime, timezone
from inspect import getmembers, ismethod
from re import search
from typing import Iterator

from typing_extensions import (
Any,
List,
Expand All @@ -22,7 +20,7 @@
Tuple,
TypeAlias,
Union,
override, deprecated,
override,
)
from urllib.parse import quote

Expand Down Expand Up @@ -60,12 +58,11 @@ def __init__(self, route: str, mode: str = CMR_OPS):
self.concept_id_chars: Set[str] = set()
self.headers: MutableMapping[str, str] = {}

@deprecated("Use get_iter() instead")
def get(self, limit: int = 2000) -> Sequence[Any]:
"""
Get all results up to some limit, even if spanning multiple pages.
:param limit: The number of results to return
:limit: The number of results to return
:returns: query results as a list
"""

Expand Down Expand Up @@ -120,56 +117,14 @@ def hits(self) -> int:

def get_all(self) -> Sequence[Any]:
"""
Returns all of the results for the query. This method could take quite
Returns all of the results for the query. This will call hits() first to determine how many
results their are, and then calls get() with that number. This method could take quite
awhile if many requests have to be made.
:returns: query results as a list
"""

return list(self.get_iter())

def get_iter(self, limit: int = -1, page_size: int = 2000) -> Iterator[Any]:
"""
Returns all results for the query as an iterator (generator)
:param limit: The maximum number of results to return. Negative value means no limit.
:param page_size: The page size (min 0, max 2000) of results retrieved from CMR. Smaller page size means
fewer items in memory and more cmr queries. Larger page size means more items in memory and fewer cmr queries.
:returns: query results as an iterator (generator)
"""

url = self._build_url()

headers = dict(self.headers or {})
more_results = True
page_size = min(max(0, page_size), 2000)
n_results = 0
if limit < 0:
limit = self.hits()

while more_results:
# Only get what we need on the last page.
page_size = min(limit - n_results, page_size)
response = requests.get(
url, headers=headers, params={"page_size": page_size}
)
response.raise_for_status()

# Explicitly track the number of results we have because the length
# of the results list will only match the number of entries fetched
# when the format is JSON. Otherwise, the length of the results
# list is the number of *pages* fetched, not the number of *items*.
n_results += page_size

if self._format == "json":
yield from response.json()["feed"]["entry"]
else:
yield response.text

if cmr_search_after := response.headers.get("cmr-search-after"):
headers["cmr-search-after"] = cmr_search_after

more_results = n_results < limit and cmr_search_after is not None
return self.get(self.hits())

def parameters(self, **kwargs: Any) -> Self:
"""
Expand Down
201 changes: 99 additions & 102 deletions poetry.lock

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ interactions:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- python-requests/2.31.0
method: GET
uri: https://cmr.earthdata.nasa.gov/search/granules.json?short_name=MOD09GA&version=061&temporal%5B%5D=1990-01-01T00:00:00Z,1990-01-02T00:00:00Z&page_size=2000
response:
body:
string: '{"feed":{"updated":"2024-09-24T00:25:07.533Z","id":"https://cmr.earthdata.nasa.gov:443/search/granules.json?short_name=MOD09GA&version=061&temporal%5B%5D=1990-01-01T00:00:00Z,1990-01-02T00:00:00Z&page_size=2000","title":"ECHO
string: '{"feed":{"updated":"2024-03-19T01:45:43.818Z","id":"https://cmr.earthdata.nasa.gov:443/search/granules.json?short_name=MOD09GA&version=061&temporal%5B%5D=1990-01-01T00:00:00Z,1990-01-02T00:00:00Z&page_size=2000","title":"ECHO
granule metadata","entry":[]}}'
headers:
Access-Control-Allow-Origin:
Expand All @@ -23,19 +25,15 @@ interactions:
CMR-Hits:
- '0'
CMR-Request-Id:
- a39aed96-a5ea-4980-adb7-1f497fc33822
- 211cef0c-67e7-4233-86c4-716bb79ae77d
CMR-Took:
- '26'
- '17'
Connection:
- keep-alive
Content-MD5:
- 376262287202a49779a917bf4035ee6a
Content-SHA1:
- 425b36da5d29c89e98dea565e9d6b5db2dade8f8
Content-Type:
- application/json;charset=utf-8
Date:
- Tue, 24 Sep 2024 00:25:07 GMT
- Tue, 19 Mar 2024 01:45:43 GMT
Server:
- ServerTokens ProductOnly
Strict-Transport-Security:
Expand All @@ -45,19 +43,19 @@ interactions:
Vary:
- Accept-Encoding, User-Agent
Via:
- 1.1 d440a00f2a23058d6ebb4241e0a5ff0a.cloudfront.net (CloudFront)
- 1.1 c8c9787916110356915bbdbddd0a32d6.cloudfront.net (CloudFront)
X-Amz-Cf-Id:
- g_WThtG6gn12mG2BvbbeKTgfhGJWN9tyOYipZedFOwPaplG1yg16JQ==
- B1Ro_QV_1jxT6j_JD9v_vH0uJaxzpwS1-wYiLga59S08cpmZuuNNXQ==
X-Amz-Cf-Pop:
- LAX50-C1
- IAD89-C2
X-Cache:
- Miss from cloudfront
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- SAMEORIGIN
X-Request-Id:
- g_WThtG6gn12mG2BvbbeKTgfhGJWN9tyOYipZedFOwPaplG1yg16JQ==
- B1Ro_QV_1jxT6j_JD9v_vH0uJaxzpwS1-wYiLga59S08cpmZuuNNXQ==
X-XSS-Protection:
- 1; mode=block
content-length:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ interactions:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- python-requests/2.31.0
method: GET
uri: https://cmr.earthdata.nasa.gov/search/granules.json?short_name=TELLUS_GRAC_L3_JPL_RL06_LND_v04&page_size=0
response:
body:
string: '{"feed":{"updated":"2024-09-24T00:24:58.509Z","id":"https://cmr.earthdata.nasa.gov:443/search/granules.json?short_name=TELLUS_GRAC_L3_JPL_RL06_LND_v04&page_size=0","title":"ECHO
string: '{"feed":{"updated":"2023-08-14T17:02:36.801Z","id":"https://cmr.earthdata.nasa.gov:443/search/granules.json?short_name=TELLUS_GRAC_L3_JPL_RL06_LND_v04&page_size=0","title":"ECHO
granule metadata","entry":[]}}'
headers:
Access-Control-Allow-Origin:
Expand All @@ -23,41 +25,37 @@ interactions:
CMR-Hits:
- '163'
CMR-Request-Id:
- 64ce09ea-2037-48a1-b5e3-1e9706811229
- 5855d714-8aff-4d0f-b4cc-e556f02ef96a
CMR-Took:
- '131'
- '52'
Connection:
- keep-alive
Content-MD5:
- 3c1bb7d108b84325434e60a36dda1159
Content-SHA1:
- 3d871ed3d2791fefc0fb58701b67811f121caa63
Content-Type:
- application/json;charset=utf-8
Date:
- Tue, 24 Sep 2024 00:24:58 GMT
- Mon, 14 Aug 2023 17:02:36 GMT
Server:
- ServerTokens ProductOnly
Strict-Transport-Security:
- max-age=31536000; includeSubDomains; preload
- max-age=31536000
Transfer-Encoding:
- chunked
Vary:
- Accept-Encoding, User-Agent
Via:
- 1.1 b837267595110a1135bf4fb036d71e1e.cloudfront.net (CloudFront)
- 1.1 cc58556a6e846289f4d3105969536e4c.cloudfront.net (CloudFront)
X-Amz-Cf-Id:
- nCF7mfer1omvbZi5CTMRTv9-9uPozEm7zBM8NhFZ8nJ_sXVz-tBAgw==
- qj9VuAc1JQu-rnMVDg3mGwstR-jGQA4rd7MKVRAEpXeTDbKZT5p5jg==
X-Amz-Cf-Pop:
- LAX50-C1
- SFO53-C1
X-Cache:
- Miss from cloudfront
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- SAMEORIGIN
X-Request-Id:
- nCF7mfer1omvbZi5CTMRTv9-9uPozEm7zBM8NhFZ8nJ_sXVz-tBAgw==
- qj9VuAc1JQu-rnMVDg3mGwstR-jGQA4rd7MKVRAEpXeTDbKZT5p5jg==
X-XSS-Protection:
- 1; mode=block
content-length:
Expand All @@ -74,11 +72,13 @@ interactions:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- python-requests/2.31.0
method: GET
uri: https://cmr.earthdata.nasa.gov/search/granules.json?short_name=TELLUS_GRAC_L3_JPL_RL06_LND_v04&page_size=163
response:
body:
string: '{"feed":{"updated":"2024-09-24T00:24:58.790Z","id":"https://cmr.earthdata.nasa.gov:443/search/granules.json?short_name=TELLUS_GRAC_L3_JPL_RL06_LND_v04&page_size=163","title":"ECHO
string: '{"feed":{"updated":"2023-08-14T17:02:40.416Z","id":"https://cmr.earthdata.nasa.gov:443/search/granules.json?short_name=TELLUS_GRAC_L3_JPL_RL06_LND_v04&page_size=163","title":"ECHO
granule metadata","entry":[{"boxes":["-89.5 0.5 89.5 180","-89.5 -180 89.5
-0.5"],"time_start":"2002-04-04T00:00:00.000Z","updated":"2023-04-17T15:27:21.022Z","dataset_id":"JPL
TELLUS GRACE Level-3 Monthly Land Water-Equivalent-Thickness Surface Mass
Expand Down Expand Up @@ -2045,43 +2045,39 @@ interactions:
CMR-Hits:
- '163'
CMR-Request-Id:
- b82d5198-a729-42bd-b597-a1132b2652c3
- 60eb29b2-95e1-453c-8efe-6e59cf649eb5
CMR-Search-After:
- '["pocloud",1495497600000,2658328520]'
CMR-Took:
- '125'
- '4959'
Connection:
- keep-alive
Content-MD5:
- 2f2981275f193e1579bea1c3e9f1acf5
Content-SHA1:
- 4fa7e296cc7b77f83dcf2bcda4237d29dc885fd1
Content-Type:
- application/json;charset=utf-8
Date:
- Tue, 24 Sep 2024 00:24:58 GMT
- Mon, 14 Aug 2023 17:02:42 GMT
Server:
- ServerTokens ProductOnly
Strict-Transport-Security:
- max-age=31536000; includeSubDomains; preload
- max-age=31536000
Transfer-Encoding:
- chunked
Vary:
- Accept-Encoding, User-Agent
Via:
- 1.1 be66acbcc5d85e825abf1047b034d722.cloudfront.net (CloudFront)
- 1.1 44933b72098305e9c31fc50b2e6554a0.cloudfront.net (CloudFront)
X-Amz-Cf-Id:
- JIeDUJvd8TodeetWYvcK5xBnxBTh8jvsNt8if-ZsMjTUWW4sbZ9P2A==
- 9TJ3JRMGc6mUxKegR4f2HSLC_1Cfwei5QHZuicg_aLsWEJS3T6XCNg==
X-Amz-Cf-Pop:
- LAX50-C1
- SFO53-C1
X-Cache:
- Miss from cloudfront
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- SAMEORIGIN
X-Request-Id:
- JIeDUJvd8TodeetWYvcK5xBnxBTh8jvsNt8if-ZsMjTUWW4sbZ9P2A==
- 9TJ3JRMGc6mUxKegR4f2HSLC_1Cfwei5QHZuicg_aLsWEJS3T6XCNg==
X-XSS-Protection:
- 1; mode=block
content-length:
Expand Down
Loading

0 comments on commit 9936bfe

Please sign in to comment.