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")
LOCS_COUNT = 3
DISTS_COUNT = 100
DISTS_COUNT = 10
FORMAT_FACTOR = 1e6 # μm
FIRST_SEP = ':'
OTHER_SEP = ','
LOC_SEP = ';'
EncodedLocation = list[tuple[float, list[float]]]
@ -171,32 +174,36 @@ def parse_dist(dist: str) -> float:
def format_location(location: EncodedLocation) -> str:
"""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:
"""Parse a location string into an EncodedLocation."""
# Stub
return [
(5., [1., 2., 3.]),
(6., [4., 5., 6.]),
]
raw_locations = location.strip().split(LOC_SEP)
first_split: list[tuple[str, str]] = [tuple(l.strip().split(FIRST_SEP)) for l in raw_locations]
second_split: list[tuple[str, list[str]]] = [(d, l.strip().split(OTHER_SEP)) for d, l in first_split]
return [(parse_dist(d1), [parse_dist(d3) for d3 in d2]) for d1, d2 in second_split]
def main():
"""Testing."""
coords = (52.210796, 0.091659)
print("Original:", coords)
print("Original:", coords, end="\n\n")
outcome = encode(coords)
print("Encoded:", outcome)
encoded = encode(coords)
print("Encoded:", encoded, end="\n\n")
decoded = decode(outcome)
print("Decoded:", decoded)
formatted = format_location(encoded)
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))
print(f"Error: {error:.10f}m")
print(f"Error: {error:.3f}m")
if __name__ == "__main__":