Skip to content

Commit 01c7fc7

Browse files
thodson-usgsclaude
andauthored
Polish wqp.py: silence prints, fix exception shape, fix typos (#256)
- Replace 6 stdout `print(...)` calls in `what_organizations`, `what_projects`, `what_detection_limits`, `what_habitat_metrics`, `what_project_weights`, and `what_activity_metrics` with `warnings.warn(..., UserWarning)` so callers can capture or filter the message instead of getting unconditional stdout pollution. - Convert `wqp_url`/`wqx3_url` from `TypeError(msg, msg)` (which raises with `args=(msg1, msg2)` and is the wrong type for an unrecognized argument) to `ValueError(single_msg)`. - Add `low_memory=False` to `what_activity_metrics` `read_csv` for consistency with the other 8 helpers. - Fix typos: `Retrun` -> `Return`, `intermitttently` -> `intermittently`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 5032b6c commit 01c7fc7

1 file changed

Lines changed: 38 additions & 39 deletions

File tree

dataretrieval/wqp.py

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,7 @@ def what_organizations(
247247

248248
kwargs = _check_kwargs(kwargs)
249249

250-
if legacy is True:
251-
url = wqp_url("Organization")
252-
else:
253-
print("WQX3.0 profile not available, returning legacy profile.")
254-
url = wqp_url("Organization")
250+
url = _legacy_only_url("Organization", legacy=legacy)
255251

256252
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
257253

@@ -298,11 +294,7 @@ def what_projects(ssl_check=True, legacy=True, **kwargs):
298294

299295
kwargs = _check_kwargs(kwargs)
300296

301-
if legacy is True:
302-
url = wqp_url("Project")
303-
else:
304-
print("WQX3.0 profile not available, returning legacy profile.")
305-
url = wqp_url("Project")
297+
url = _legacy_only_url("Project", legacy=legacy)
306298

307299
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
308300

@@ -424,11 +416,7 @@ def what_detection_limits(
424416

425417
kwargs = _check_kwargs(kwargs)
426418

427-
if legacy is True:
428-
url = wqp_url("ResultDetectionQuantitationLimit")
429-
else:
430-
print("WQX3.0 profile not available, returning legacy profile.")
431-
url = wqp_url("ResultDetectionQuantitationLimit")
419+
url = _legacy_only_url("ResultDetectionQuantitationLimit", legacy=legacy)
432420

433421
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
434422

@@ -479,11 +467,7 @@ def what_habitat_metrics(
479467

480468
kwargs = _check_kwargs(kwargs)
481469

482-
if legacy is True:
483-
url = wqp_url("BiologicalMetric")
484-
else:
485-
print("WQX3.0 profile not available, returning legacy profile.")
486-
url = wqp_url("BiologicalMetric")
470+
url = _legacy_only_url("BiologicalMetric", legacy=legacy)
487471

488472
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
489473

@@ -508,7 +492,7 @@ def what_project_weights(ssl_check=True, legacy=True, **kwargs):
508492
ssl_check : bool
509493
Check the SSL certificate. Default is True.
510494
legacy : bool
511-
Retrun the legacy WQX data profile. Default is True.
495+
Return the legacy WQX data profile. Default is True.
512496
**kwargs : optional
513497
Accepts the same parameters as :obj:`dataretrieval.wqp.get_results`
514498
@@ -535,11 +519,7 @@ def what_project_weights(ssl_check=True, legacy=True, **kwargs):
535519

536520
kwargs = _check_kwargs(kwargs)
537521

538-
if legacy is True:
539-
url = wqp_url("ProjectMonitoringLocationWeighting")
540-
else:
541-
print("WQX3.0 profile not available, returning legacy profile.")
542-
url = wqp_url("ProjectMonitoringLocationWeighting")
522+
url = _legacy_only_url("ProjectMonitoringLocationWeighting", legacy=legacy)
543523

544524
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
545525

@@ -591,15 +571,11 @@ def what_activity_metrics(ssl_check=True, legacy=True, **kwargs):
591571

592572
kwargs = _check_kwargs(kwargs)
593573

594-
if legacy is True:
595-
url = wqp_url("ActivityMetric")
596-
else:
597-
print("WQX3.0 profile not available, returning legacy profile.")
598-
url = wqp_url("ActivityMetric")
574+
url = _legacy_only_url("ActivityMetric", legacy=legacy)
599575

600576
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
601577

602-
df = pd.read_csv(StringIO(response.text), delimiter=",")
578+
df = pd.read_csv(StringIO(response.text), delimiter=",", low_memory=False)
603579

604580
return df, WQP_Metadata(response)
605581

@@ -611,9 +587,8 @@ def wqp_url(service):
611587
_warn_legacy_use()
612588

613589
if service not in services_legacy:
614-
raise TypeError(
615-
"Legacy service not recognized. Valid options are",
616-
f"{services_legacy}.",
590+
raise ValueError(
591+
f"Legacy service not recognized. Valid options are {services_legacy}."
617592
)
618593

619594
return f"{base_url}{service}/Search?"
@@ -626,9 +601,8 @@ def wqx3_url(service):
626601
_warn_wqx3_use()
627602

628603
if service not in services_wqx3:
629-
raise TypeError(
630-
"WQX3.0 service not recognized. Valid options are",
631-
f"{services_wqx3}.",
604+
raise ValueError(
605+
f"WQX3.0 service not recognized. Valid options are {services_wqx3}."
632606
)
633607

634608
return f"{base_url}{service}/search?"
@@ -700,7 +674,7 @@ def _check_kwargs(kwargs):
700674
def _warn_wqx3_use():
701675
message = (
702676
"Support for the WQX3.0 profiles is experimental. "
703-
"Queries may be slow or fail intermitttently."
677+
"Queries may be slow or fail intermittently."
704678
)
705679
warnings.warn(message, UserWarning, stacklevel=2)
706680

@@ -714,3 +688,28 @@ def _warn_legacy_use():
714688
"will remove this warning."
715689
)
716690
warnings.warn(message, DeprecationWarning, stacklevel=2)
691+
692+
693+
def _warn_wqx3_unavailable():
694+
# stacklevel=3: warn -> _warn_wqx3_unavailable -> _legacy_only_url -> what_*
695+
warnings.warn(
696+
"WQX3.0 profile not available, returning legacy profile.",
697+
UserWarning,
698+
stacklevel=3,
699+
)
700+
701+
702+
def _legacy_only_url(service: str, legacy: bool) -> str:
703+
"""URL builder for WQP services that have no WQX3.0 equivalent.
704+
705+
When ``legacy=False`` is passed to one of these helpers we emit a
706+
``UserWarning`` explaining the fallback and *also* suppress the legacy
707+
``DeprecationWarning`` that ``wqp_url`` would otherwise raise — its
708+
message claims setting ``legacy=False`` removes the warning, which is
709+
a lie for endpoints that have no WQX3.0 alternative.
710+
"""
711+
with warnings.catch_warnings():
712+
if not legacy:
713+
_warn_wqx3_unavailable()
714+
warnings.simplefilter("ignore", DeprecationWarning)
715+
return wqp_url(service)

0 commit comments

Comments
 (0)