diff options
| author | Scott Gasch <[email protected]> | 2022-09-01 11:42:34 -0700 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2022-09-01 11:42:34 -0700 |
| commit | c4961fb1e600f27e37d9a8bf646e1c76afafd8b6 (patch) | |
| tree | 9571c8ef8d841375e89609965c33a68ff77b7299 /cached | |
| parent | 5771b377afe22318c3587559e7d68fe3d6238cd3 (diff) | |
state via pickle and json.
Diffstat (limited to 'cached')
| -rw-r--r-- | cached/weather_data.py | 50 | ||||
| -rw-r--r-- | cached/weather_forecast.py | 38 |
2 files changed, 33 insertions, 55 deletions
diff --git a/cached/weather_data.py b/cached/weather_data.py index 91d665d..86978c8 100644 --- a/cached/weather_data.py +++ b/cached/weather_data.py @@ -74,7 +74,7 @@ class WeatherData: @persistent.persistent_autoloaded_singleton() # type: ignore -class CachedWeatherData(persistent.Persistent): +class CachedWeatherData(persistent.PicklingFileBasedPersistent): def __init__(self, weather_data: Dict[datetime.date, WeatherData] = None): """C'tor. Do not pass a dict except for testing purposes. @@ -207,42 +207,24 @@ class CachedWeatherData(persistent.Persistent): icon=icon, ) - @classmethod + @staticmethod @overrides - def load(cls) -> Any: + def get_filename() -> str: + return config.config['weather_data_cachefile'] - """Depending on whether we have fresh data persisted either uses that - data to instantiate the class or makes an HTTP request to fetch the - necessary data. - - Note that because this is a subclass of Persistent this is taken - care of automatically. - """ + @staticmethod + @overrides + def should_we_save_data(filename: str) -> bool: + return True - if persistent.was_file_written_within_n_seconds( - config.config['weather_data_cachefile'], + @staticmethod + @overrides + def should_we_load_data(filename: str) -> bool: + return persistent.was_file_written_within_n_seconds( + filename, config.config['weather_data_stalest_acceptable'].total_seconds(), - ): - import pickle - - with open(config.config['weather_data_cachefile'], 'rb') as rf: - weather_data = pickle.load(rf) - return cls(weather_data) - return None + ) @overrides - def save(self) -> bool: - """ - Saves the current data to disk if required. Again, because this is - a subclass of Persistent this is taken care of for you. - """ - - import pickle - - with open(config.config['weather_data_cachefile'], 'wb') as wf: - pickle.dump( - self.weather_data, - wf, - pickle.HIGHEST_PROTOCOL, - ) - return True + def get_persistent_data(self) -> Any: + return self.weather_data diff --git a/cached/weather_forecast.py b/cached/weather_forecast.py index b8a20ed..7973bbb 100644 --- a/cached/weather_forecast.py +++ b/cached/weather_forecast.py @@ -56,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 @@ -119,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 + def get_filename() -> str: + return config.config['weather_forecast_cachefile'] - with open(config.config['weather_forecast_cachefile'], 'rb') as rf: - weather_data = pickle.load(rf) - return cls(weather_data) - return None + @staticmethod + @overrides + def should_we_save_data(filename: str) -> bool: + return True + @staticmethod @overrides - def save(self) -> bool: - import pickle + 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(), + ) - with open(config.config['weather_forecast_cachefile'], 'wb') as wf: - pickle.dump( - self.forecasts, - wf, - pickle.HIGHEST_PROTOCOL, - ) - return True + @overrides + def get_persistent_data(self) -> Any: + return self.forecasts |
