From 9641b4219b8f96c26767f8af7233ac0686a0c2f7 Mon Sep 17 00:00:00 2001 From: Jules Dejaeghere Date: Mon, 5 May 2025 21:42:20 +0200 Subject: [PATCH] Reformat code + add type hints --- irm_kmi_api/data.py | 12 +++++++++++- irm_kmi_api/pollen.py | 10 +++++----- irm_kmi_api/rain_graph.py | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/irm_kmi_api/data.py b/irm_kmi_api/data.py index 0d2f3ac..2724be7 100644 --- a/irm_kmi_api/data.py +++ b/irm_kmi_api/data.py @@ -37,10 +37,13 @@ class Forecast(TypedDict, total=False): class IrmKmiConditionEvol(Enum): + """Possible state for evolution between weather conditions""" + ONE_WAY = 'one_way' TWO_WAYS = 'two_ways' STABLE = 'stable' - + + class IrmKmiRadarStyle(Enum): """Possible style for the rain radar""" @@ -49,6 +52,7 @@ class IrmKmiRadarStyle(Enum): OPTION_STYLE_YELLOW_RED = 'yellow_red_style' OPTION_STYLE_SATELLITE = 'satellite_style' + class IrmKmiPollenNames(Enum): """Pollens names from the API""" @@ -72,6 +76,7 @@ class IrmKmiPollenLevels(Enum): RED = 'red' PURPLE = 'purple' + class IrmKmiForecast(Forecast, total=False): """Forecast class with additional attributes for IRM KMI""" @@ -84,6 +89,7 @@ class IrmKmiForecast(Forecast, total=False): class CurrentWeatherData(TypedDict, total=False): """Class to hold the currently observable weather at a given location""" + condition: str | None temperature: float | None wind_speed: float | None @@ -95,6 +101,7 @@ class CurrentWeatherData(TypedDict, total=False): class WarningData(TypedDict, total=False): """Holds data about a specific warning""" + slug: str id: int level: int @@ -106,6 +113,7 @@ class WarningData(TypedDict, total=False): class IrmKmiRadarForecast(Forecast): """Forecast class to handle rain forecast from the IRM KMI rain radar""" + rain_forecast_max: float rain_forecast_min: float might_rain: bool @@ -114,6 +122,7 @@ class IrmKmiRadarForecast(Forecast): class AnimationFrameData(TypedDict, total=False): """Holds one single frame of the radar camera, along with the timestamp of the frame""" + time: datetime | None image: bytes | str | None value: float | None @@ -124,6 +133,7 @@ class AnimationFrameData(TypedDict, total=False): class RadarAnimationData(TypedDict, total=False): """Holds frames and additional data for the animation to be rendered""" + sequence: List[AnimationFrameData] | None most_recent_image_idx: int | None hint: str | None diff --git a/irm_kmi_api/pollen.py b/irm_kmi_api/pollen.py index 9c5a9a4..db8a0b3 100644 --- a/irm_kmi_api/pollen.py +++ b/irm_kmi_api/pollen.py @@ -39,11 +39,11 @@ class PollenParser: elements: List[ET.Element] = self._extract_elements(root) - pollens = {e.attrib.get('x', None): self._get_elem_text(e).lower() - for e in elements if 'tspan' in e.tag and str(self._get_elem_text(e)).lower() in IrmKmiPollenNames} + pollens = {e.attrib.get('x', None): self._get_txt(e).lower() + for e in elements if 'tspan' in e.tag and str(self._get_txt(e)).lower() in IrmKmiPollenNames} - pollen_levels = {e.attrib.get('x', None): POLLEN_LEVEL_TO_COLOR[self._get_elem_text(e)] - for e in elements if 'tspan' in e.tag and self._get_elem_text(e) in POLLEN_LEVEL_TO_COLOR} + pollen_levels = {e.attrib.get('x', None): POLLEN_LEVEL_TO_COLOR[self._get_txt(e)] + for e in elements if 'tspan' in e.tag and self._get_txt(e) in POLLEN_LEVEL_TO_COLOR} level_dots = {e.attrib.get('cx', None) for e in elements if 'circle' in e.tag} @@ -112,7 +112,7 @@ class PollenParser: return elements @staticmethod - def _get_elem_text(e) -> str | None: + def _get_txt(e) -> str | None: if e.text is not None: return e.text.strip() return None diff --git a/irm_kmi_api/rain_graph.py b/irm_kmi_api/rain_graph.py index 1ec4b79..07a68ab 100644 --- a/irm_kmi_api/rain_graph.py +++ b/irm_kmi_api/rain_graph.py @@ -149,7 +149,7 @@ class RainGraph: """ return self._animation_data.get('hint', '') - async def _download_clouds(self, idx=None): + async def _download_clouds(self, idx: int | None = None): """ Download cloud images and save the result in the internal state.