55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
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)
|