Skip to content

Commit 3161c9d

Browse files
committed
Review
Pre-commit checks: All checks passed ✅
1 parent b248ebe commit 3161c9d

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

causal_automl/TutorTask401_EIA_metadata_downloader_pipeline/eia_utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,17 @@ def _get_api_request(self, route: str) -> Dict[str, Any]:
118118
# Build the full API request URL.
119119
url = f"{self._base_url}/{route}?api_key={self._api_key}"
120120
# Send HTTP GET request to the EIA API.
121+
# TODO(alvino): Add error handling for the HTTP request to handle
122+
# potential exceptions such as connection errors or timeouts.
121123
response = requests.get(url, timeout=20)
122124
# Parse JSON content.
125+
# TODO(alvino): Check if the response is successful (e.g.,
126+
# `response.status_code == 200`) before attempting to parse the JSON
127+
# content.
123128
json_data = response.json()
124129
# Get response from parsed payload.
125130
data: Dict[str, Any] = {}
131+
# TODO(alvino): Add error handling for JSON parsing to manage potential parsing errors.
126132
data = json_data.get("response", {})
127133
return data
128134

@@ -238,6 +244,8 @@ def _extract_metadata(
238244
# Determine parameter CSV path for associated facet values.
239245
param_file_path = f"eia_parameters_v{self._version_num}/{dataset_id_clean}_parameters.csv"
240246
# Flattened metadata row for one frequency and metric combination.
247+
# TODO(gp): `.get()` will use `None` if there is a missing
248+
# value in the dictionary. Is this the intended behavior?
241249
metadata = {
242250
"url": url,
243251
"id": f"{route_clean}.{frequency_id}.{metric_id_clean}",
@@ -270,6 +278,7 @@ def _get_facet_values(
270278
:param route: dataset route under the EIA v2 API
271279
:return: data containing all facet values
272280
"""
281+
hdbg.dassert_in("facets", metadata)
273282
facets = metadata["facets"]
274283
rows = []
275284
for facet in facets:
@@ -340,8 +349,7 @@ def plot_distribution(df_metadata: pd.DataFrame, column: str, title: str) -> Non
340349
'frequency_id', 'data_units')
341350
:param title: title for the plot
342351
"""
343-
if column not in df_metadata.columns:
344-
raise ValueError(f"Column '{column}' not found in metadata index.")
352+
hdbg.dassert_in(column, df_metadata.columns)
345353
counts = df_metadata[column].value_counts()
346354
ax = counts.plot(kind="bar", figsize=(8, 4), title=title)
347355
ax.set_xlabel(column.replace("_", " ").title())

helpers_root

0 commit comments

Comments
 (0)