From 6869637ffecdbba4df057e37ebd5335f2d913d94 Mon Sep 17 00:00:00 2001 From: n-strong Date: Fri, 6 Dec 2024 23:17:04 -0800 Subject: [PATCH] cleaned up code in helper.py forecast_to_json() --- src/helper.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/helper.py b/src/helper.py index 07a408c..a842aa5 100644 --- a/src/helper.py +++ b/src/helper.py @@ -343,15 +343,10 @@ def set_location(location): def forecast_to_json(forecast_data, decimal): - """ - Takes forecast_data from forecast() as input - and returns it in JSON format - """ - # Formatting into JSON forecasts = [] - for i in range(len(forecast_data["date"])): - forecast = { - "date": str(forecast_data["date"][i].date()), + for i, d in enumerate(forecast_data["date"]): + forecasts.append({ + "date": str(d.date()), "surf height": round( float(forecast_data["wave_height_max"][i]), decimal ), @@ -370,23 +365,29 @@ def forecast_to_json(forecast_data, decimal): "temperature_2m_min": round( float(forecast_data["temperature_2m_min"][i]), decimal ), - "rain_sum": round(float(forecast_data["rain_sum"][i]), decimal), + "rain_sum": round( + float(forecast_data["rain_sum"][i]), decimal + ), "daily_precipitation_probability": round( - float(forecast_data["precipitation_probability_max"][i]), - decimal, + float( + forecast_data["precipitation_probability_max"][i] + ), + decimal ), "wind_speed_max": round( float(forecast_data["wind_speed_10m_max"][i]), decimal ), "wind_direction_10m_dominant": round( - float(forecast_data["wind_direction_10m_dominant"][i]), decimal + float( + forecast_data["wind_direction_10m_dominant"][i] + ), + decimal ), - } - forecasts.append(forecast) - + }) return forecasts + def surf_summary(surf_data): """ Outputs a simple summary of the surf data.