From fe97038a1d55c3888a5977ad0877b86e6c935e80 Mon Sep 17 00:00:00 2001 From: Arno Schmerer Date: Sat, 1 Jun 2024 15:26:48 +0200 Subject: [PATCH] Refactor get_forecast_for_future_hour to return dictionary instead of JSON string - Refactored the get_forecast_for_future_hour method in class Location to return a dictionary directly instead of a JSON string. - Bumped the version in setup.py to 1.1.0rc6 to reflect these improvements and changes. This change improves the design of the Python package, making it easier to work with in other parts of the application and reducing potential errors related to JSON string handling. --- dwd_global_radiation/global_radiation.py | 22 ++++++++++------------ setup.py | 2 +- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/dwd_global_radiation/global_radiation.py b/dwd_global_radiation/global_radiation.py index 8e7ad66..eee3a24 100644 --- a/dwd_global_radiation/global_radiation.py +++ b/dwd_global_radiation/global_radiation.py @@ -125,8 +125,8 @@ def get_forecast_for_future_hour(self, datetime_input, number_of_hours): retrieve the forecast. Returns: - str: A JSON string containing the forecast timestamp in UTC and sis (Global Radiation) - value, or an error message if no forecast is found for the specified time. + dict: A dictionary containing the forecast timestamp in UTC and sis (Global Radiation) + value, or an error message if no forecast is found for the specified time. """ # Ensure datetime_input is in UTC datetime_input = datetime_input.astimezone(timezone.utc) @@ -145,16 +145,14 @@ def get_forecast_for_future_hour(self, datetime_input, number_of_hours): if target_index < len(self.forecasts[0].entries): forecast_entry = self.forecasts[0].entries[target_index] - return json.dumps( - { - "timestamp": datetime.fromtimestamp( - forecast_entry.timestamp, tz=timezone.utc - ).strftime("%Y-%m-%dT%H:%M:%S%z"), - "sis": round(forecast_entry.sis), - } - ) - - return json.dumps({"error": "No forecast found for the specified time"}) + return { + "timestamp": datetime.fromtimestamp( + forecast_entry.timestamp, tz=timezone.utc + ).strftime("%Y-%m-%dT%H:%M:%S%z"), + "sis": round(forecast_entry.sis), + } + + return {"error": "No forecast found for the specified time"} @dataclass diff --git a/setup.py b/setup.py index 7408dbe..7b7a8b1 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ setup( name='dwd_global_radiation', - version='1.1.0rc5', + version='1.1.0rc6', packages=find_packages(), description='Access and analyze DWD global radiation data and forecasts', long_description=long_description,