35 lines
976 B
Python
35 lines
976 B
Python
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":
|
|
name, location = 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"),
|
|
)
|