init
This commit is contained in:
34
gismeteo/location.py
Normal file
34
gismeteo/location.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from typing import NamedTuple
|
||||
|
||||
|
||||
class LocationValue(NamedTuple):
|
||||
location_id: int
|
||||
name: str
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.name}-{self. location_id}"
|
||||
|
||||
@classmethod
|
||||
def parse(cls, source: str) -> "LocationValue":
|
||||
location, name = source.split("-")
|
||||
return cls(int(location), name)
|
||||
|
||||
|
||||
class LocationBundle:
|
||||
def __init__(self, *values: LocationValue) -> None:
|
||||
self._values = values
|
||||
self._values_by_name = {value.name: value for value in self._values}
|
||||
self._values_by_id = {value.location_id: value for value in self._values}
|
||||
|
||||
def parse(self, value: str) -> LocationValue:
|
||||
if str.isdigit(value):
|
||||
return self._values_by_id[int(value)]
|
||||
if value in self._values_by_name:
|
||||
return self._values_by_name[value]
|
||||
return LocationValue.parse(value)
|
||||
|
||||
|
||||
LOCATION_BUNDLE = LocationBundle(
|
||||
LocationValue(4432, "orel"),
|
||||
LocationValue(184640, "zmiyevka"),
|
||||
)
|
||||
Reference in New Issue
Block a user