Skip to content

Commit 0aec2c8

Browse files
thodson-usgsclaude
andauthored
Small cleanups: idiomatic Python and a precedence fix (#224)
* Small cleanups: idiomatic Python and a precedence fix - waterdata/utils.py: replace runtime `assert` in `_check_ogc_requests` with an explicit `ValueError` (assertions can be disabled with `-O`). - waterdata/utils.py: fix operator-precedence bug in `_format_datetime` where `len==1 and re.search(...) or "/" in datetime_input[0]` would short-circuit and return `datetime_input[0]` for 2-element inputs whose first element contained "/". Parenthesize so both branches require `len==1`. - nwis.py: `"dec_lat_va" in list(df)` -> `in df.columns` (drop needless list construction). - nwis.py: replace index-based `for i in range(len(index_list)-1)` loop with `zip(index_list[:-1], index_list[1:])`. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Ruff format --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1115167 commit 0aec2c8

2 files changed

Lines changed: 7 additions & 10 deletions

File tree

dataretrieval/nwis.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def format_response(
9696
if service == "peaks":
9797
df = preformat_peaks_response(df)
9898

99-
if gpd is not None and "dec_lat_va" in list(df):
99+
if gpd is not None and "dec_lat_va" in df.columns:
100100
geoms = gpd.points_from_xy(df.dec_long_va.values, df.dec_lat_va.values)
101101
df = gpd.GeoDataFrame(df, geometry=geoms, crs=_CRS)
102102

@@ -993,10 +993,7 @@ def _read_json(json):
993993
)
994994
index_list.append(len(site_list))
995995

996-
for i in range(len(index_list) - 1):
997-
start = index_list[i] # [0]
998-
end = index_list[i + 1] # [21]
999-
996+
for start, end in zip(index_list[:-1], index_list[1:]):
1000997
# grab a block containing timeseries 0:21,
1001998
# which are all from the same site
1002999
site_block = json["value"]["timeSeries"][start:end]

dataretrieval/waterdata/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,8 @@ def _format_api_dates(
184184
if len(datetime_input) <= 2:
185185
# If the list is of length 1, first look for things like "P7D" or dates
186186
# already formatted in ISO08601. Otherwise, try to coerce to datetime
187-
if (
188-
len(datetime_input) == 1
189-
and re.search(r"P", datetime_input[0], re.IGNORECASE)
187+
if len(datetime_input) == 1 and (
188+
re.search(r"P", datetime_input[0], re.IGNORECASE)
190189
or "/" in datetime_input[0]
191190
):
192191
return datetime_input[0]
@@ -291,12 +290,13 @@ def _check_ogc_requests(endpoint: str = "daily", req_type: str = "queryables"):
291290
292291
Raises
293292
------
294-
AssertionError
293+
ValueError
295294
If req_type is not "queryables" or "schema".
296295
requests.HTTPError
297296
If the HTTP request returns an unsuccessful status code.
298297
"""
299-
assert req_type in ["queryables", "schema"]
298+
if req_type not in ("queryables", "schema"):
299+
raise ValueError(f"req_type must be 'queryables' or 'schema', got {req_type!r}")
300300
url = f"{OGC_API_URL}/collections/{endpoint}/{req_type}"
301301
resp = requests.get(url, headers=_default_headers())
302302
resp.raise_for_status()

0 commit comments

Comments
 (0)