X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=cached%2Fweather_forecast.py;h=7973bbb4c8b38b185042446efb82cfc2c72e130b;hb=HEAD;hp=807f36d2bcd9c5770d6fa5e5bc897dcc791f8ca1;hpb=713a609bd19d491de03debf8a4a6ddf2540b13dc;p=python_utils.git diff --git a/cached/weather_forecast.py b/cached/weather_forecast.py index 807f36d..7973bbb 100644 --- a/cached/weather_forecast.py +++ b/cached/weather_forecast.py @@ -1,5 +1,9 @@ #!/usr/bin/env python3 +# © Copyright 2021-2022, Scott Gasch + +"""How's the weather going to be tomorrow?""" + import datetime import logging import os @@ -52,7 +56,7 @@ class WeatherForecast: @persistent.persistent_autoloaded_singleton() # type: ignore -class CachedDetailedWeatherForecast(persistent.Persistent): +class CachedDetailedWeatherForecast(persistent.PicklingFileBasedPersistent): def __init__(self, forecasts=None): if forecasts is not None: self.forecasts = forecasts @@ -115,28 +119,24 @@ class CachedDetailedWeatherForecast(persistent.Persistent): description=blurb, ) - @classmethod + @staticmethod @overrides - def load(cls) -> Any: - if persistent.was_file_written_within_n_seconds( - config.config['weather_forecast_cachefile'], - config.config['weather_forecast_stalest_acceptable'].total_seconds(), - ): - import pickle - - with open(config.config['weather_forecast_cachefile'], 'rb') as rf: - weather_data = pickle.load(rf) - return cls(weather_data) - return None + def get_filename() -> str: + return config.config['weather_forecast_cachefile'] + @staticmethod @overrides - def save(self) -> bool: - import pickle - - with open(config.config['weather_forecast_cachefile'], 'wb') as wf: - pickle.dump( - self.forecasts, - wf, - pickle.HIGHEST_PROTOCOL, - ) + def should_we_save_data(filename: str) -> bool: return True + + @staticmethod + @overrides + def should_we_load_data(filename: str) -> bool: + return persistent.was_file_written_within_n_seconds( + filename, + config.config['weather_forecast_stalest_acceptable'].total_seconds(), + ) + + @overrides + def get_persistent_data(self) -> Any: + return self.forecasts