X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=cached%2Fweather_data.py;h=45b6e6efc5d9d3c4cbd07f0996633d835aa4a98a;hb=67e324d8995c18445574415eb44abcfdce436bdc;hp=94a01313375f5a6e6ecc5b485c873e9280c54db5;hpb=fa4298fa508e00759565c246aef423ba28fedf31;p=python_utils.git diff --git a/cached/weather_data.py b/cached/weather_data.py index 94a0131..45b6e6e 100644 --- a/cached/weather_data.py +++ b/cached/weather_data.py @@ -5,9 +5,11 @@ import datetime import json import logging import os -from typing import List +from typing import Any, List import urllib.request +from overrides import overrides + import argparse_utils import config import datetime_utils @@ -23,7 +25,7 @@ cfg = config.add_commandline_args( cfg.add_argument( '--weather_data_cachefile', type=str, - default=f'{os.environ["HOME"]}/.weather_summary_cache', + default=f'{os.environ["HOME"]}/cache/.weather_summary_cache', metavar='FILENAME', help='File in which to cache weather data' ) @@ -41,7 +43,7 @@ class WeatherData: date: datetime.date # The date high: float # The predicted high in F low: float # The predicted low in F - precipitation_inchs: float # Number of inches of precipitation / day + precipitation_inches: float # Number of inches of precipitation / day conditions: List[str] # Conditions per ~3h window most_common_condition: str # The most common condition icon: str # An icon to represent it @@ -108,7 +110,7 @@ class CachedWeatherData(persistent.Persistent): date = dt, high = float(parsed_json["main"]["temp_max"]), low = float(parsed_json["main"]["temp_min"]), - precipitation_inchs = p / 25.4, + precipitation_inches = p / 25.4, conditions = [condition], most_common_condition = condition, icon = icon, @@ -164,7 +166,7 @@ class CachedWeatherData(persistent.Persistent): ): self.weather_data[today].high = high continue - most_common_condition = list_utils.most_common_item(conditions[dt]) + most_common_condition = list_utils.most_common(conditions[dt]) icon = icon_by_condition.get(most_common_condition, '?') if dt == now.date() and now.hour > 18 and condition == 'Clear': icon = '🌙' @@ -172,14 +174,15 @@ class CachedWeatherData(persistent.Persistent): date = dt, high = highs[dt], low = lows[dt], - precipitation_inchs = precip[dt] / 25.4, + precipitation_inches = precip[dt] / 25.4, conditions = conditions[dt], most_common_condition = most_common_condition, icon = icon ) @classmethod - def load(cls): + @overrides + def load(cls) -> Any: if persistent.was_file_written_within_n_seconds( config.config['weather_data_cachefile'], config.config['weather_data_stalest_acceptable'].total_seconds(), @@ -190,7 +193,8 @@ class CachedWeatherData(persistent.Persistent): return cls(weather_data) return None - def save(self): + @overrides + def save(self) -> bool: import pickle with open(config.config['weather_data_cachefile'], 'wb') as wf: pickle.dump( @@ -198,3 +202,4 @@ class CachedWeatherData(persistent.Persistent): wf, pickle.HIGHEST_PROTOCOL, ) + return True