Skip to content

Commit be0f072

Browse files
thodson-usgsclaude
andauthored
refactor(wqp): collapse the 8 what_* bodies into a shared _what helper (#320)
The what_sites/what_organizations/what_projects/what_activities/ what_detection_limits/what_habitat_metrics/what_project_weights/ what_activity_metrics functions had byte-identical bodies differing only by the service name and which URL builder they used. Factor the shared body (kwarg check -> URL resolution -> query -> CSV parse -> metadata) into a private _what() helper; each public function becomes a one-line delegator. The WQX3.0-capable services are derived from the existing services_wqx3 constant (single source of truth) rather than hardcoded per function. Public signatures, docstrings, and observable behavior are unchanged. _warn_wqx3_unavailable's stacklevel is bumped 3 -> 4 to account for the extra _what frame so the warning is still attributed to the public what_* call site. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent ff8f535 commit be0f072

1 file changed

Lines changed: 48 additions & 74 deletions

File tree

dataretrieval/wqp.py

Lines changed: 48 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,33 @@ def get_results(
186186
return df, WQP_Metadata(response, **kwargs)
187187

188188

189+
def _what(
190+
service: str,
191+
*,
192+
ssl_check: bool,
193+
legacy: bool,
194+
**kwargs: Any,
195+
) -> tuple[DataFrame, WQP_Metadata]:
196+
"""Shared implementation for the ``what_*`` metadata search functions.
197+
198+
``service`` is the WQP service name (e.g. ``"Station"``). Services with a
199+
WQX3.0 equivalent (those in :data:`services_wqx3`) use :func:`wqx3_url`
200+
when ``legacy=False`` and :func:`wqp_url` otherwise; legacy-only services
201+
route through :func:`_legacy_only_url`, which warns and falls back to the
202+
legacy profile. The CSV response is parsed via :func:`_read_wqp_csv`.
203+
"""
204+
kwargs = _check_kwargs(kwargs)
205+
206+
if service in services_wqx3:
207+
url = wqp_url(service) if legacy else wqx3_url(service)
208+
else:
209+
url = _legacy_only_url(service, legacy=legacy)
210+
211+
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
212+
df = _read_wqp_csv(response.text)
213+
return df, WQP_Metadata(response, **kwargs)
214+
215+
189216
def what_sites(
190217
ssl_check: bool = True,
191218
legacy: bool = True,
@@ -230,15 +257,7 @@ def what_sites(
230257
231258
"""
232259

233-
kwargs = _check_kwargs(kwargs)
234-
235-
url = wqp_url("Station") if legacy is True else wqx3_url("Station")
236-
237-
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
238-
239-
df = _read_wqp_csv(response.text)
240-
241-
return df, WQP_Metadata(response, **kwargs)
260+
return _what("Station", ssl_check=ssl_check, legacy=legacy, **kwargs)
242261

243262

244263
def what_organizations(
@@ -281,15 +300,7 @@ def what_organizations(
281300
282301
"""
283302

284-
kwargs = _check_kwargs(kwargs)
285-
286-
url = _legacy_only_url("Organization", legacy=legacy)
287-
288-
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
289-
290-
df = _read_wqp_csv(response.text)
291-
292-
return df, WQP_Metadata(response, **kwargs)
303+
return _what("Organization", ssl_check=ssl_check, legacy=legacy, **kwargs)
293304

294305

295306
def what_projects(
@@ -332,15 +343,7 @@ def what_projects(
332343
333344
"""
334345

335-
kwargs = _check_kwargs(kwargs)
336-
337-
url = _legacy_only_url("Project", legacy=legacy)
338-
339-
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
340-
341-
df = _read_wqp_csv(response.text)
342-
343-
return df, WQP_Metadata(response, **kwargs)
346+
return _what("Project", ssl_check=ssl_check, legacy=legacy, **kwargs)
344347

345348

346349
def what_activities(
@@ -396,15 +399,7 @@ def what_activities(
396399
... )
397400
"""
398401

399-
kwargs = _check_kwargs(kwargs)
400-
401-
url = wqp_url("Activity") if legacy is True else wqx3_url("Activity")
402-
403-
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
404-
405-
df = _read_wqp_csv(response.text)
406-
407-
return df, WQP_Metadata(response, **kwargs)
402+
return _what("Activity", ssl_check=ssl_check, legacy=legacy, **kwargs)
408403

409404

410405
def what_detection_limits(
@@ -454,15 +449,12 @@ def what_detection_limits(
454449
455450
"""
456451

457-
kwargs = _check_kwargs(kwargs)
458-
459-
url = _legacy_only_url("ResultDetectionQuantitationLimit", legacy=legacy)
460-
461-
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
462-
463-
df = _read_wqp_csv(response.text)
464-
465-
return df, WQP_Metadata(response, **kwargs)
452+
return _what(
453+
"ResultDetectionQuantitationLimit",
454+
ssl_check=ssl_check,
455+
legacy=legacy,
456+
**kwargs,
457+
)
466458

467459

468460
def what_habitat_metrics(
@@ -505,15 +497,7 @@ def what_habitat_metrics(
505497
506498
"""
507499

508-
kwargs = _check_kwargs(kwargs)
509-
510-
url = _legacy_only_url("BiologicalMetric", legacy=legacy)
511-
512-
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
513-
514-
df = _read_wqp_csv(response.text)
515-
516-
return df, WQP_Metadata(response, **kwargs)
500+
return _what("BiologicalMetric", ssl_check=ssl_check, legacy=legacy, **kwargs)
517501

518502

519503
def what_project_weights(
@@ -561,15 +545,12 @@ def what_project_weights(
561545
562546
"""
563547

564-
kwargs = _check_kwargs(kwargs)
565-
566-
url = _legacy_only_url("ProjectMonitoringLocationWeighting", legacy=legacy)
567-
568-
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
569-
570-
df = _read_wqp_csv(response.text)
571-
572-
return df, WQP_Metadata(response, **kwargs)
548+
return _what(
549+
"ProjectMonitoringLocationWeighting",
550+
ssl_check=ssl_check,
551+
legacy=legacy,
552+
**kwargs,
553+
)
573554

574555

575556
def what_activity_metrics(
@@ -617,15 +598,7 @@ def what_activity_metrics(
617598
618599
"""
619600

620-
kwargs = _check_kwargs(kwargs)
621-
622-
url = _legacy_only_url("ActivityMetric", legacy=legacy)
623-
624-
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
625-
626-
df = _read_wqp_csv(response.text)
627-
628-
return df, WQP_Metadata(response, **kwargs)
601+
return _what("ActivityMetric", ssl_check=ssl_check, legacy=legacy, **kwargs)
629602

630603

631604
def wqp_url(service: str) -> str:
@@ -745,11 +718,12 @@ def _warn_legacy_use() -> None:
745718

746719

747720
def _warn_wqx3_unavailable() -> None:
748-
# stacklevel=3: warn -> _warn_wqx3_unavailable -> _legacy_only_url -> what_*
721+
# stacklevel=4: warn -> _warn_wqx3_unavailable -> _legacy_only_url -> _what
722+
# -> what_*, so the warning is attributed to the public ``what_*`` call.
749723
warnings.warn(
750724
"WQX3.0 profile not available, returning legacy profile.",
751725
UserWarning,
752-
stacklevel=3,
726+
stacklevel=4,
753727
)
754728

755729

0 commit comments

Comments
 (0)