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