|
| 1 | +""" |
| 2 | +Validated coordinate field types for city data. |
| 3 | +""" |
| 4 | + |
| 5 | +from abc import ABC, abstractmethod |
| 6 | + |
| 7 | +LATITUDE = "latitude" |
| 8 | +LONGITUDE = "longitude" |
| 9 | + |
| 10 | +MIN_LATITUDE = -90 |
| 11 | +MAX_LATITUDE = 90 |
| 12 | +MIN_LONGITUDE = -180 |
| 13 | +MAX_LONGITUDE = 180 |
| 14 | + |
| 15 | + |
| 16 | +class Coordinate(ABC): |
| 17 | + def __init__(self, value: int | float): |
| 18 | + numeric_value = self._validate_type(value) |
| 19 | + lower_bound, upper_bound = self.bounds() |
| 20 | + if numeric_value < lower_bound or numeric_value > upper_bound: |
| 21 | + raise ValueError( |
| 22 | + f"Bad value for {self.field_name()}: {numeric_value}. " |
| 23 | + f"Expected between {lower_bound} and {upper_bound}" |
| 24 | + ) |
| 25 | + self.value = numeric_value |
| 26 | + |
| 27 | + @classmethod |
| 28 | + @abstractmethod |
| 29 | + def bounds(cls) -> tuple[float, float]: |
| 30 | + """Return the inclusive coordinate bounds.""" |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def field_name(cls) -> str: |
| 34 | + return cls.__name__.lower() |
| 35 | + |
| 36 | + def _validate_type(self, value: int | float) -> float: |
| 37 | + if isinstance(value, bool) or not isinstance(value, (int, float)): |
| 38 | + raise TypeError(f"Bad type for value: {type(value)}") |
| 39 | + return float(value) |
| 40 | + |
| 41 | + def __float__(self) -> float: |
| 42 | + return self.value |
| 43 | + |
| 44 | + def __str__(self) -> str: |
| 45 | + return str(self.value) |
| 46 | + |
| 47 | + |
| 48 | +class Latitude(Coordinate): |
| 49 | + @classmethod |
| 50 | + def bounds(cls) -> tuple[float, float]: |
| 51 | + return MIN_LATITUDE, MAX_LATITUDE |
| 52 | + |
| 53 | + |
| 54 | +class Longitude(Coordinate): |
| 55 | + @classmethod |
| 56 | + def bounds(cls) -> tuple[float, float]: |
| 57 | + return MIN_LONGITUDE, MAX_LONGITUDE |
| 58 | + |
| 59 | + |
| 60 | +class Coordinates: |
| 61 | + def __init__( |
| 62 | + self, |
| 63 | + latitude: int | float | Latitude, |
| 64 | + longitude: int | float | Longitude, |
| 65 | + ): |
| 66 | + self.latitude = latitude if isinstance(latitude, Latitude) else Latitude(latitude) |
| 67 | + self.longitude = ( |
| 68 | + longitude if isinstance(longitude, Longitude) else Longitude(longitude) |
| 69 | + ) |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def from_dict(cls, coordinates: dict) -> "Coordinates": |
| 73 | + if not isinstance(coordinates, dict): |
| 74 | + raise TypeError(f"Bad type for coordinates: {type(coordinates)}") |
| 75 | + if LATITUDE not in coordinates: |
| 76 | + raise ValueError("Missing latitude in coordinates") |
| 77 | + if LONGITUDE not in coordinates: |
| 78 | + raise ValueError("Missing longitude in coordinates") |
| 79 | + return cls(coordinates[LATITUDE], coordinates[LONGITUDE]) |
| 80 | + |
| 81 | + def to_dict(self) -> dict: |
| 82 | + return { |
| 83 | + LATITUDE: float(self.latitude), |
| 84 | + LONGITUDE: float(self.longitude), |
| 85 | + } |
0 commit comments