From 36126583234e506e2d1ebcbc0ca2a8551995f2ec Mon Sep 17 00:00:00 2001 From: shmyga Date: Tue, 22 Sep 2026 20:58:18 +0300 Subject: [PATCH] refactor(weather): rework wind parser --- gallery/painting/gismeteo/parser.py | 4 +- gallery/painting/openweather/parser.py | 4 +- gallery/painting/rp5/parser.py | 4 +- gallery/painting/yandexweather/parser.py | 4 +- gallery/sketch/weather/parse/__init__.py | 0 gallery/sketch/weather/parse/wind.py | 49 +++++++++++++++++++++ gallery/sketch/weather/parser.py | 54 ------------------------ 7 files changed, 57 insertions(+), 62 deletions(-) create mode 100644 gallery/sketch/weather/parse/__init__.py create mode 100644 gallery/sketch/weather/parse/wind.py delete mode 100644 gallery/sketch/weather/parser.py diff --git a/gallery/painting/gismeteo/parser.py b/gallery/painting/gismeteo/parser.py index dd2971b..d03c530 100644 --- a/gallery/painting/gismeteo/parser.py +++ b/gallery/painting/gismeteo/parser.py @@ -19,7 +19,7 @@ from gallery.sketch.weather.model import ( WeatherValue, WindDirection, ) -from gallery.sketch.weather.parser import parse_wind_direction +from gallery.sketch.weather.parse.wind import WIND_DIRECTION_PARSER from gallery.sketch.weather.util import fix_dates_sequence, merge_weather_values from . import PROVIDER @@ -152,7 +152,7 @@ class WindDirectionParser(TableValueParser[WindDirection | None]): model_key = "wind.direction" def extract(self, data: Tag) -> WindDirection | None: - return parse_wind_direction(data.text.strip()) + return WIND_DIRECTION_PARSER.parse(data.text.strip()) class PrecipitationParser(TableValueParser[float]): diff --git a/gallery/painting/openweather/parser.py b/gallery/painting/openweather/parser.py index c5c9077..8df0d9a 100644 --- a/gallery/painting/openweather/parser.py +++ b/gallery/painting/openweather/parser.py @@ -7,7 +7,7 @@ from gallery.sketch.weather.model import ( Precipitation, WeatherValue, ) -from gallery.sketch.weather.parser import parse_wind_direction +from gallery.sketch.weather.parse.wind import WIND_DIRECTION_PARSER from gallery.sketch.weather.util import convert_hPa_to_mmHg from .openweather import Forecast, ForecastItem @@ -47,7 +47,7 @@ class ForecastItemParser(Parser[ForecastItem, list[WeatherValue]]): "wind": { "speed": round(data.wind.speed), "gust": round(data.wind.gust), - "direction": parse_wind_direction(data.wind.deg), + "direction": WIND_DIRECTION_PARSER.parse(data.wind.deg), }, "temperature": [round(data.main.temp)], # [round(data.main.temp_max), round(data.main.temp_min)] "precipitation": round(data.rain.interval_3h, 1) if data.rain else 0, diff --git a/gallery/painting/rp5/parser.py b/gallery/painting/rp5/parser.py index 319eb4c..63b14ec 100644 --- a/gallery/painting/rp5/parser.py +++ b/gallery/painting/rp5/parser.py @@ -19,7 +19,7 @@ from gallery.sketch.weather.model import ( WeatherValue, WindDirection, ) -from gallery.sketch.weather.parser import parse_wind_direction +from gallery.sketch.weather.parse.wind import WIND_DIRECTION_PARSER from gallery.sketch.weather.util import fix_dates_sequence from . import PROVIDER @@ -135,7 +135,7 @@ class WindDirectionParser(TableValueParser[WindDirection | None]): model_key = "wind.direction" def extract(self, data: Tag) -> WindDirection | None: - return parse_wind_direction(data.text.strip()) + return WIND_DIRECTION_PARSER.parse(data.text.strip()) class HumidityParser(TableValueParser[int]): diff --git a/gallery/painting/yandexweather/parser.py b/gallery/painting/yandexweather/parser.py index 25ab2cd..947b251 100644 --- a/gallery/painting/yandexweather/parser.py +++ b/gallery/painting/yandexweather/parser.py @@ -21,7 +21,7 @@ from gallery.sketch.weather.model import ( WeatherValue, WindDirection, ) -from gallery.sketch.weather.parser import parse_wind_direction +from gallery.sketch.weather.parse.wind import WIND_DIRECTION_PARSER from . import PROVIDER @@ -113,7 +113,7 @@ class WindDirectionParser(TableValueParser[WindDirection | None]): model_key = "wind.direction" def extract(self, data: Tag) -> WindDirection | None: - return parse_wind_direction(data.text.strip()) + return WIND_DIRECTION_PARSER.parse(data.text.strip()) class HumidityParser(TableValueParser[int]): diff --git a/gallery/sketch/weather/parse/__init__.py b/gallery/sketch/weather/parse/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gallery/sketch/weather/parse/wind.py b/gallery/sketch/weather/parse/wind.py new file mode 100644 index 0000000..72f5128 --- /dev/null +++ b/gallery/sketch/weather/parse/wind.py @@ -0,0 +1,49 @@ +from gallery.sketch.parse.core import Parser + +from ..model import WindDirection + + +class WindDirectionParser(Parser[str | int | float, WindDirection | None]): + WIND_DIRECTION_MAP: dict[str, WindDirection | None] = { + "с": WindDirection.N, + "св": WindDirection.NE, + "в": WindDirection.E, + "юв": WindDirection.SE, + "ю": WindDirection.S, + "юз": WindDirection.SW, + "з": WindDirection.W, + "сз": WindDirection.NW, + } + + # pylint:disable=too-many-return-statements + def _parse_number(self, value: int | float) -> WindDirection | None: + if value > 337.5 or value <= 22.25: + return WindDirection.N + elif value <= 67.5: + return WindDirection.NE + elif value <= 112.5: + return WindDirection.E + elif value <= 157.5: + return WindDirection.SE + elif value <= 202.5: + return WindDirection.S + elif value <= 247.5: + return WindDirection.SW + elif value <= 292.5: + return WindDirection.W + elif value <= 337.5: + return WindDirection.NW + else: + return None + + def _parse_str(self, value: str) -> WindDirection | None: + return self.WIND_DIRECTION_MAP.get(value.replace("-", "").lower()) + + def parse(self, data: str | int | float) -> WindDirection | None: + if isinstance(data, str): + return self._parse_str(data) + else: + return self._parse_number(data) + + +WIND_DIRECTION_PARSER = WindDirectionParser() diff --git a/gallery/sketch/weather/parser.py b/gallery/sketch/weather/parser.py deleted file mode 100644 index 6cf80b7..0000000 --- a/gallery/sketch/weather/parser.py +++ /dev/null @@ -1,54 +0,0 @@ -from typing import Self - -from gallery.sketch.weather.model import WindDirection - - -class ParseError(Exception): - @classmethod - def invalid_date(cls, date_str: str) -> Self: - return cls("Invalid date string", date_str) - - -WIND_DIRECTION_MAP: dict[str, WindDirection | None] = { - "с": WindDirection.N, - "св": WindDirection.NE, - "в": WindDirection.E, - "юв": WindDirection.SE, - "ю": WindDirection.S, - "юз": WindDirection.SW, - "з": WindDirection.W, - "сз": WindDirection.NW, -} - - -# pylint:disable=too-many-return-statements -def _parse_wind_direction_number(value: int | float) -> WindDirection | None: - if value > 337.5 or value <= 22.25: - return WindDirection.N - elif value <= 67.5: - return WindDirection.NE - elif value <= 112.5: - return WindDirection.E - elif value <= 157.5: - return WindDirection.SE - elif value <= 202.5: - return WindDirection.S - elif value <= 247.5: - return WindDirection.SW - elif value <= 292.5: - return WindDirection.W - elif value <= 337.5: - return WindDirection.NW - else: - return None - - -def _parse_wind_direction_str(value: str) -> WindDirection | None: - return WIND_DIRECTION_MAP.get(value.replace("-", "").lower()) - - -def parse_wind_direction(value: str | int | float) -> WindDirection | None: - if isinstance(value, str): - return _parse_wind_direction_str(value) - else: - return _parse_wind_direction_number(value)