Skip to content

Commit 9936bfe

Browse files
authored
Revert "Issues/37 Add function for returning an iterator instead of sequence" (#90)
1 parent bda1cff commit 9936bfe

14 files changed

Lines changed: 10291 additions & 32332 deletions

CHANGELOG.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

99
## [Unreleased]
1010

11-
### Added
12-
- New function `get_iter` for returning results as an iterator instead of sequence ([#37](https://github.com/nasa/python_cmr/issues/37))
13-
14-
### Deprecated
15-
- 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))
16-
1711
## [0.13.0]
1812

1913
### Added

cmr/queries.py

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from datetime import date, datetime, timezone
88
from inspect import getmembers, ismethod
99
from re import search
10-
from typing import Iterator
11-
1210
from typing_extensions import (
1311
Any,
1412
List,
@@ -22,7 +20,7 @@
2220
Tuple,
2321
TypeAlias,
2422
Union,
25-
override, deprecated,
23+
override,
2624
)
2725
from urllib.parse import quote
2826

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

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

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

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

129-
return list(self.get_iter())
130-
131-
def get_iter(self, limit: int = -1, page_size: int = 2000) -> Iterator[Any]:
132-
"""
133-
Returns all results for the query as an iterator (generator)
134-
135-
:param limit: The maximum number of results to return. Negative value means no limit.
136-
:param page_size: The page size (min 0, max 2000) of results retrieved from CMR. Smaller page size means
137-
fewer items in memory and more cmr queries. Larger page size means more items in memory and fewer cmr queries.
138-
:returns: query results as an iterator (generator)
139-
"""
140-
141-
url = self._build_url()
142-
143-
headers = dict(self.headers or {})
144-
more_results = True
145-
page_size = min(max(0, page_size), 2000)
146-
n_results = 0
147-
if limit < 0:
148-
limit = self.hits()
149-
150-
while more_results:
151-
# Only get what we need on the last page.
152-
page_size = min(limit - n_results, page_size)
153-
response = requests.get(
154-
url, headers=headers, params={"page_size": page_size}
155-
)
156-
response.raise_for_status()
157-
158-
# Explicitly track the number of results we have because the length
159-
# of the results list will only match the number of entries fetched
160-
# when the format is JSON. Otherwise, the length of the results
161-
# list is the number of *pages* fetched, not the number of *items*.
162-
n_results += page_size
163-
164-
if self._format == "json":
165-
yield from response.json()["feed"]["entry"]
166-
else:
167-
yield response.text
168-
169-
if cmr_search_after := response.headers.get("cmr-search-after"):
170-
headers["cmr-search-after"] = cmr_search_after
171-
172-
more_results = n_results < limit and cmr_search_after is not None
127+
return self.get(self.hits())
173128

174129
def parameters(self, **kwargs: Any) -> Self:
175130
"""

poetry.lock

Lines changed: 99 additions & 102 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/vcr_cassettes/TestMultipleQueries.test_get_all_more_than_2k.yaml renamed to tests/fixtures/vcr_cassettes/CYGNSS.yaml

Lines changed: 42 additions & 1106 deletions
Large diffs are not rendered by default.

tests/fixtures/vcr_cassettes/TestMultipleQueries.test_get_iter_more_than_2000.yaml renamed to tests/fixtures/vcr_cassettes/MOD02QKM.yaml

Lines changed: 6027 additions & 6101 deletions
Large diffs are not rendered by default.

tests/fixtures/vcr_cassettes/TestMultipleQueries.test_get.yaml renamed to tests/fixtures/vcr_cassettes/MOD02QKM_2000.yaml

Lines changed: 4013 additions & 4015 deletions
Large diffs are not rendered by default.

tests/fixtures/vcr_cassettes/TestMultipleQueries.test_zero_hits_query.yaml renamed to tests/fixtures/vcr_cassettes/MOD09GA061_nohits.yaml

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ interactions:
88
- gzip, deflate
99
Connection:
1010
- keep-alive
11+
User-Agent:
12+
- python-requests/2.31.0
1113
method: GET
1214
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
1315
response:
1416
body:
15-
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
17+
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
1618
granule metadata","entry":[]}}'
1719
headers:
1820
Access-Control-Allow-Origin:
@@ -23,19 +25,15 @@ interactions:
2325
CMR-Hits:
2426
- '0'
2527
CMR-Request-Id:
26-
- a39aed96-a5ea-4980-adb7-1f497fc33822
28+
- 211cef0c-67e7-4233-86c4-716bb79ae77d
2729
CMR-Took:
28-
- '26'
30+
- '17'
2931
Connection:
3032
- keep-alive
31-
Content-MD5:
32-
- 376262287202a49779a917bf4035ee6a
33-
Content-SHA1:
34-
- 425b36da5d29c89e98dea565e9d6b5db2dade8f8
3533
Content-Type:
3634
- application/json;charset=utf-8
3735
Date:
38-
- Tue, 24 Sep 2024 00:25:07 GMT
36+
- Tue, 19 Mar 2024 01:45:43 GMT
3937
Server:
4038
- ServerTokens ProductOnly
4139
Strict-Transport-Security:
@@ -45,19 +43,19 @@ interactions:
4543
Vary:
4644
- Accept-Encoding, User-Agent
4745
Via:
48-
- 1.1 d440a00f2a23058d6ebb4241e0a5ff0a.cloudfront.net (CloudFront)
46+
- 1.1 c8c9787916110356915bbdbddd0a32d6.cloudfront.net (CloudFront)
4947
X-Amz-Cf-Id:
50-
- g_WThtG6gn12mG2BvbbeKTgfhGJWN9tyOYipZedFOwPaplG1yg16JQ==
48+
- B1Ro_QV_1jxT6j_JD9v_vH0uJaxzpwS1-wYiLga59S08cpmZuuNNXQ==
5149
X-Amz-Cf-Pop:
52-
- LAX50-C1
50+
- IAD89-C2
5351
X-Cache:
5452
- Miss from cloudfront
5553
X-Content-Type-Options:
5654
- nosniff
5755
X-Frame-Options:
5856
- SAMEORIGIN
5957
X-Request-Id:
60-
- g_WThtG6gn12mG2BvbbeKTgfhGJWN9tyOYipZedFOwPaplG1yg16JQ==
58+
- B1Ro_QV_1jxT6j_JD9v_vH0uJaxzpwS1-wYiLga59S08cpmZuuNNXQ==
6159
X-XSS-Protection:
6260
- 1; mode=block
6361
content-length:

tests/fixtures/vcr_cassettes/TestMultipleQueries.test_get_all_less_than_2k.yaml renamed to tests/fixtures/vcr_cassettes/TELLUS_GRAC.yaml

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ interactions:
88
- gzip, deflate
99
Connection:
1010
- keep-alive
11+
User-Agent:
12+
- python-requests/2.31.0
1113
method: GET
1214
uri: https://cmr.earthdata.nasa.gov/search/granules.json?short_name=TELLUS_GRAC_L3_JPL_RL06_LND_v04&page_size=0
1315
response:
1416
body:
15-
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
17+
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
1618
granule metadata","entry":[]}}'
1719
headers:
1820
Access-Control-Allow-Origin:
@@ -23,41 +25,37 @@ interactions:
2325
CMR-Hits:
2426
- '163'
2527
CMR-Request-Id:
26-
- 64ce09ea-2037-48a1-b5e3-1e9706811229
28+
- 5855d714-8aff-4d0f-b4cc-e556f02ef96a
2729
CMR-Took:
28-
- '131'
30+
- '52'
2931
Connection:
3032
- keep-alive
31-
Content-MD5:
32-
- 3c1bb7d108b84325434e60a36dda1159
33-
Content-SHA1:
34-
- 3d871ed3d2791fefc0fb58701b67811f121caa63
3533
Content-Type:
3634
- application/json;charset=utf-8
3735
Date:
38-
- Tue, 24 Sep 2024 00:24:58 GMT
36+
- Mon, 14 Aug 2023 17:02:36 GMT
3937
Server:
4038
- ServerTokens ProductOnly
4139
Strict-Transport-Security:
42-
- max-age=31536000; includeSubDomains; preload
40+
- max-age=31536000
4341
Transfer-Encoding:
4442
- chunked
4543
Vary:
4644
- Accept-Encoding, User-Agent
4745
Via:
48-
- 1.1 b837267595110a1135bf4fb036d71e1e.cloudfront.net (CloudFront)
46+
- 1.1 cc58556a6e846289f4d3105969536e4c.cloudfront.net (CloudFront)
4947
X-Amz-Cf-Id:
50-
- nCF7mfer1omvbZi5CTMRTv9-9uPozEm7zBM8NhFZ8nJ_sXVz-tBAgw==
48+
- qj9VuAc1JQu-rnMVDg3mGwstR-jGQA4rd7MKVRAEpXeTDbKZT5p5jg==
5149
X-Amz-Cf-Pop:
52-
- LAX50-C1
50+
- SFO53-C1
5351
X-Cache:
5452
- Miss from cloudfront
5553
X-Content-Type-Options:
5654
- nosniff
5755
X-Frame-Options:
5856
- SAMEORIGIN
5957
X-Request-Id:
60-
- nCF7mfer1omvbZi5CTMRTv9-9uPozEm7zBM8NhFZ8nJ_sXVz-tBAgw==
58+
- qj9VuAc1JQu-rnMVDg3mGwstR-jGQA4rd7MKVRAEpXeTDbKZT5p5jg==
6159
X-XSS-Protection:
6260
- 1; mode=block
6361
content-length:
@@ -74,11 +72,13 @@ interactions:
7472
- gzip, deflate
7573
Connection:
7674
- keep-alive
75+
User-Agent:
76+
- python-requests/2.31.0
7777
method: GET
7878
uri: https://cmr.earthdata.nasa.gov/search/granules.json?short_name=TELLUS_GRAC_L3_JPL_RL06_LND_v04&page_size=163
7979
response:
8080
body:
81-
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
81+
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
8282
granule metadata","entry":[{"boxes":["-89.5 0.5 89.5 180","-89.5 -180 89.5
8383
-0.5"],"time_start":"2002-04-04T00:00:00.000Z","updated":"2023-04-17T15:27:21.022Z","dataset_id":"JPL
8484
TELLUS GRACE Level-3 Monthly Land Water-Equivalent-Thickness Surface Mass
@@ -2045,43 +2045,39 @@ interactions:
20452045
CMR-Hits:
20462046
- '163'
20472047
CMR-Request-Id:
2048-
- b82d5198-a729-42bd-b597-a1132b2652c3
2048+
- 60eb29b2-95e1-453c-8efe-6e59cf649eb5
20492049
CMR-Search-After:
20502050
- '["pocloud",1495497600000,2658328520]'
20512051
CMR-Took:
2052-
- '125'
2052+
- '4959'
20532053
Connection:
20542054
- keep-alive
2055-
Content-MD5:
2056-
- 2f2981275f193e1579bea1c3e9f1acf5
2057-
Content-SHA1:
2058-
- 4fa7e296cc7b77f83dcf2bcda4237d29dc885fd1
20592055
Content-Type:
20602056
- application/json;charset=utf-8
20612057
Date:
2062-
- Tue, 24 Sep 2024 00:24:58 GMT
2058+
- Mon, 14 Aug 2023 17:02:42 GMT
20632059
Server:
20642060
- ServerTokens ProductOnly
20652061
Strict-Transport-Security:
2066-
- max-age=31536000; includeSubDomains; preload
2062+
- max-age=31536000
20672063
Transfer-Encoding:
20682064
- chunked
20692065
Vary:
20702066
- Accept-Encoding, User-Agent
20712067
Via:
2072-
- 1.1 be66acbcc5d85e825abf1047b034d722.cloudfront.net (CloudFront)
2068+
- 1.1 44933b72098305e9c31fc50b2e6554a0.cloudfront.net (CloudFront)
20732069
X-Amz-Cf-Id:
2074-
- JIeDUJvd8TodeetWYvcK5xBnxBTh8jvsNt8if-ZsMjTUWW4sbZ9P2A==
2070+
- 9TJ3JRMGc6mUxKegR4f2HSLC_1Cfwei5QHZuicg_aLsWEJS3T6XCNg==
20752071
X-Amz-Cf-Pop:
2076-
- LAX50-C1
2072+
- SFO53-C1
20772073
X-Cache:
20782074
- Miss from cloudfront
20792075
X-Content-Type-Options:
20802076
- nosniff
20812077
X-Frame-Options:
20822078
- SAMEORIGIN
20832079
X-Request-Id:
2084-
- JIeDUJvd8TodeetWYvcK5xBnxBTh8jvsNt8if-ZsMjTUWW4sbZ9P2A==
2080+
- 9TJ3JRMGc6mUxKegR4f2HSLC_1Cfwei5QHZuicg_aLsWEJS3T6XCNg==
20852081
X-XSS-Protection:
20862082
- 1; mode=block
20872083
content-length:

0 commit comments

Comments
 (0)