From eaab80fa33eb34c544a910744ffc3b2661f33e13 Mon Sep 17 00:00:00 2001 From: FL550 Date: Sun, 2 Jul 2023 15:03:45 +0200 Subject: [PATCH] Better distinction of report and measurement --- README.md | 2 ++ setup.py | 2 +- simple_dwd_weatherforecast/dwdforecast.py | 26 ++++++++++++++--------- tests/test_reported_weather.py | 6 +++--- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index cd9b532..76566bb 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,8 @@ class Weather: get_timeframe_condition(datetime, timeframe: hours after datetime as int, optional bool shouldUpdate) # Result is an approximate "feeled" condition at this time frame get_weather_report(optional bool shouldUpdate) # Returns the weather report for the geographical region of the station as HTML + + update(self, optional bool force_hourly (default: False), optional bool with_forecast (default: True), optional bool with_measurements (default: False), optional bool with_report (default: False)) # Updates the weather data ``` #### Advanced Usage diff --git a/setup.py b/setup.py index b98b712..3580fbf 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="simple_dwd_weatherforecast", - version="2.0.6", + version="2.0.7", author="Max Fermor", description="A simple tool to retrieve a weather forecast from DWD OpenData", long_description=long_description, diff --git a/simple_dwd_weatherforecast/dwdforecast.py b/simple_dwd_weatherforecast/dwdforecast.py index f846d69..c607165 100644 --- a/simple_dwd_weatherforecast/dwdforecast.py +++ b/simple_dwd_weatherforecast/dwdforecast.py @@ -226,7 +226,7 @@ def is_valid_timeframe(_, timeframe: int) -> bool: return False return 24 % timeframe == 0 - def has_report(self, station_id): + def has_measurement(self, station_id): if load_station_id(station_id): return stations[station_id]["report_available"] == 1 return False @@ -334,11 +334,11 @@ def get_condition(self, weather_data): return str(condition_text) def get_reported_weather(self, weatherDataType: WeatherDataType, shouldUpdate=True): - if not self.has_report(self.station_id): + if not self.has_measurement(self.station_id): print("no report for this station available") return None - if shouldUpdate: - self.update() + if shouldUpdate or self.report_data is None: + self.update(with_measurements=True) if weatherDataType == WeatherDataType.CONDITION: return self.actual_report_codes[ self.report_data[WeatherDataType.CONDITION.value[0]] @@ -543,14 +543,20 @@ def strip_to_hour(_, timestamp: datetime): def strip_to_day(_, timestamp: datetime): return datetime(timestamp.year, timestamp.month, timestamp.day) - def update(self, force_hourly=False): - if self.has_report(self.station_id): + def update( + self, + force_hourly=False, + with_forecast=True, + with_measurements=False, + with_report=False, + ): + if with_measurements and self.has_measurement(self.station_id): self.download_latest_report() - if self.region is not None: + if with_report and self.region is not None: self.download_weather_report(self.region_codes[self.region]) - if ( + if with_forecast and ( (self.issue_time is None) or (datetime.now(timezone.utc) - self.issue_time >= timedelta(hours=6)) or force_hourly @@ -763,8 +769,8 @@ def parse_csv_row(self, row: dict): } def get_weather_report(self, shouldUpdate=False): - if shouldUpdate: - self.update() + if shouldUpdate or self.weather_report is None: + self.update(with_report=True) return self.weather_report def download_weather_report(self, region_code): diff --git a/tests/test_reported_weather.py b/tests/test_reported_weather.py index 27cf981..4abfafa 100644 --- a/tests/test_reported_weather.py +++ b/tests/test_reported_weather.py @@ -10,12 +10,12 @@ def test_download(self): def test_has_report_true(self): dwdweather = dwdforecast.Weather("01008") - self.assertTrue(dwdweather.has_report("01008")) + self.assertTrue(dwdweather.has_measurement("01008")) def test_has_report_false(self): dwdweather = dwdforecast.Weather("X472") - self.assertFalse(dwdweather.has_report("X472")) + self.assertFalse(dwdweather.has_measurement("X472")) def test_has_report_invalid_station(self): dwdweather = dwdforecast.Weather("01008") - self.assertFalse(dwdweather.has_report("01")) \ No newline at end of file + self.assertFalse(dwdweather.has_measurement("01")) \ No newline at end of file