Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Created March 3, 2021 20:24
Show Gist options
  • Save MartinThoma/37d0c0f26b6eeb7230c977d6a19d0f67 to your computer and use it in GitHub Desktop.
Save MartinThoma/37d0c0f26b6eeb7230c977d6a19d0f67 to your computer and use it in GitHub Desktop.
from typing import Optional
from pydantic import validator
from pydantic.dataclasses import dataclass
@dataclass(frozen=True)
class Position:
longitude: float
latitude: float
address: Optional[str] = None
@validator("longitude")
def longitude_value_range(cls, v):
if not (-180 <= v <= 180):
raise ValueError(f"Longitude was {v}, but must be in [-180, +180]")
return v
@validator("latitude")
def latitude_value_range(cls, v):
if not (-90 <= v <= 90):
raise ValueError(f"Latitude was {v}, but must be in [-90, +90]")
return v
pos1 = Position(49.0127913, 8.4231381, "Parkstraße 17")
pos2 = Position(longitude=42.1238762, latitude=9.1649964)
def get_distance(p1: Position, p2: Position) -> float:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment