engine: implement parsing

This commit is contained in:
Oliver Gaskell 2025-11-02 10:45:01 +00:00
parent b03175f9d9
commit 8562e5cba3
No known key found for this signature in database
GPG key ID: F971A08925FCC0AD

View file

@ -18,9 +18,12 @@ BRANDS: dict[str, str] = {
CACHE_FOLDER = Path(".cache") CACHE_FOLDER = Path(".cache")
LOCS_COUNT = 3 LOCS_COUNT = 3
DISTS_COUNT = 100 DISTS_COUNT = 10
FORMAT_FACTOR = 1e6 # μm FORMAT_FACTOR = 1e6 # μm
FIRST_SEP = ':'
OTHER_SEP = ','
LOC_SEP = ';'
EncodedLocation = list[tuple[float, list[float]]] EncodedLocation = list[tuple[float, list[float]]]
@ -171,32 +174,36 @@ def parse_dist(dist: str) -> float:
def format_location(location: EncodedLocation) -> str: def format_location(location: EncodedLocation) -> str:
"""Format an encoded location as a string.""" """Format an encoded location as a string."""
return ";\n".join([f"{format_dist(a)}:{','.join(map(format_dist, b))}" for (a, b) in location]) return LOC_SEP.join([f"{format_dist(a)}{FIRST_SEP}{OTHER_SEP.join(map(format_dist, b))}" for (a, b) in location])
def parse_location(location: str) -> EncodedLocation: def parse_location(location: str) -> EncodedLocation:
"""Parse a location string into an EncodedLocation.""" """Parse a location string into an EncodedLocation."""
raw_locations = location.strip().split(LOC_SEP)
# Stub first_split: list[tuple[str, str]] = [tuple(l.strip().split(FIRST_SEP)) for l in raw_locations]
return [ second_split: list[tuple[str, list[str]]] = [(d, l.strip().split(OTHER_SEP)) for d, l in first_split]
(5., [1., 2., 3.]), return [(parse_dist(d1), [parse_dist(d3) for d3 in d2]) for d1, d2 in second_split]
(6., [4., 5., 6.]),
]
def main(): def main():
"""Testing.""" """Testing."""
coords = (52.210796, 0.091659) coords = (52.210796, 0.091659)
print("Original:", coords) print("Original:", coords, end="\n\n")
outcome = encode(coords) encoded = encode(coords)
print("Encoded:", outcome) print("Encoded:", encoded, end="\n\n")
decoded = decode(outcome) formatted = format_location(encoded)
print("Decoded:", decoded) print("Formatted:", formatted, end="\n\n")
parsed = parse_location(formatted)
print("Parsed:", parsed, end="\n\n")
decoded = decode(encoded)
print("Decoded:", decoded, end="\n\n")
error = spherical_dist(np.array(coords), np.array(decoded)) error = spherical_dist(np.array(coords), np.array(decoded))
print(f"Error: {error:.10f}m") print(f"Error: {error:.3f}m")
if __name__ == "__main__": if __name__ == "__main__":