Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Force convert into float (np.nan if fails) #126

Merged
merged 3 commits into from
Apr 23, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/ozone/ozone.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
RATE_LIMIT: int = 1


def as_float(x: Any) -> float:
Milind220 marked this conversation as resolved.
Show resolved Hide resolved
"""Convert x into a float. If unable, convert into numpy.nan instead.

Naming and functionality inspired by R function as.numeric()"""
try:
return float(x)
except (TypeError, ValueError):
return numpy.nan


class Ozone:
"""Primary class for Ozone API

Expand Down Expand Up @@ -196,14 +206,14 @@ def _extract_live_data(
try:
if param == "aqi":
# This is in different part of JSON object.
row["aqi"] = float(data_obj["aqi"])
Milind220 marked this conversation as resolved.
Show resolved Hide resolved
row["aqi"] = as_float(data_obj["aqi"])
# This adds AQI_meaning and AQI_health_implications data
(
row["AQI_meaning"],
row["AQI_health_implications"],
) = self._AQI_meaning(float(data_obj["aqi"]))
) = self._AQI_meaning(as_float(data_obj["aqi"]))
else:
row[param] = float(data_obj["iaqi"][param]["v"])
row[param] = as_float(data_obj["iaqi"][param]["v"])
except KeyError:
# Gets triggered if the parameter is not provided by station.
row[param] = numpy.nan
Expand Down Expand Up @@ -331,10 +341,8 @@ def _AQI_meaning(self, aqi: float) -> Tuple[str, str]:
"Health alert: everyone may experience more serious health effects."
)
else:
raise Exception(
f"{aqi} is not valid air quality index value. "
"Should be between 0 to 500."
)
AQI_meaning = "Invalid AQI value"
Milind220 marked this conversation as resolved.
Show resolved Hide resolved
AQI_health_implications = "Invalid AQI value"

return AQI_meaning, AQI_health_implications

Expand Down Expand Up @@ -569,7 +577,7 @@ def get_specific_parameter(
row = self._extract_live_data(data_obj, [air_param])

try:
result = float(row[air_param])
result = as_float(row[air_param])
except KeyError:
raise Exception(
f'Missing air quality parameter "{air_param}"\n'
Expand Down