From 9f5847088c2c2af52ca89cc43f1e5c12a9e1dc8a Mon Sep 17 00:00:00 2001 From: Midgard Date: Tue, 16 Jan 2024 02:28:34 +0100 Subject: [PATCH] Remove fancy JsonRoundingFloat and JsonLatLon because the Python JSON lib can't override the precise serialization anyway. Ugh. --- tests.py | 22 +--------------------- trainmap_backup.py | 45 +++++---------------------------------------- 2 files changed, 6 insertions(+), 61 deletions(-) diff --git a/tests.py b/tests.py index 7fd5085..bd500eb 100644 --- a/tests.py +++ b/tests.py @@ -1,6 +1,5 @@ from trainmap_backup import \ - camel_to_snake, transform_dict, RETAIN_KEY, RETAIN_VALUE, DISCARD, \ - JsonRoundingFloat, JsonLonLatCoord, json_encode + camel_to_snake, transform_dict, RETAIN_KEY, RETAIN_VALUE, DISCARD def test_camel_to_snake(): @@ -33,22 +32,3 @@ def test_transform_dict(): "uppervalue": "VALUE", "retainboth": "value", } - - -def test_JsonRoundingFloat(): - assert str(JsonRoundingFloat(1.1234567890123456789012345678901234567890, 2)) == "1.12" - assert str(JsonRoundingFloat(1.1234567890123456789012345678901234567890, 3)) == "1.123" - assert str(JsonRoundingFloat(1.1, 3)) == "1.1" - - -def test_JsonLonLatCoord(): - assert str(JsonLonLatCoord(1.1234567890123456789012345678901234567890, 50.1234)) == \ - "[1.123457, 50.1234]" - - -def test_json_encode(): - assert json_encode(JsonLonLatCoord(1, 2)) == "[1, 2]" - assert json_encode([ - JsonLonLatCoord(1, 2), - JsonLonLatCoord(3, 4), - ]) == "[[1, 2], [3, 4]]" diff --git a/trainmap_backup.py b/trainmap_backup.py index 1e22f8f..89e2f02 100755 --- a/trainmap_backup.py +++ b/trainmap_backup.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 -from dataclasses import dataclass import re import json import socketio @@ -8,28 +7,6 @@ import socketio WS_URL = "wss://trainmap.belgiantrain.be/socket.io/" -@dataclass -class JsonLonLatCoord: - lon: float - lat: float - def __repr__(self): - return str(self.__to_json__()) - def __to_json__(self): - # return f"[{self.lon:.6f}, {self.lat:.6f}]" - return [round(self.lon, 6), round(self.lat, 6)] - - -@dataclass -class JsonRoundingFloat: - value: float - decimals: int - def __repr__(self): - return str(self.__to_json__()) - def __to_json__(self): - # return f"{{0:.{self.decimals}f}}".format(self.value) - return round(self.value, self.decimals) - - class RETAIN_KEY: pass class RETAIN_VALUE: pass class DISCARD: pass @@ -84,12 +61,12 @@ def filtered_data(data): trip["trip_short_name"]: transform_dict({ "trip_id": DISCARD, "trip_short_name": DISCARD, - "position": (RETAIN_KEY, lambda l: ( - JsonLonLatCoord(*l) - if isinstance(l, list) else l + "position": (RETAIN_KEY, lambda p: ( + [round(p[0], 6), round(p[1], 6)] + if isinstance(p, list) else p )), "current_traveled_distance": (RETAIN_KEY, lambda x: ( - JsonRoundingFloat(x, 1) + round(x, 1) if isinstance(x, float) else x )), "fromstationdistance": ("from_station_distance", RETAIN_VALUE), @@ -105,20 +82,8 @@ def filtered_data(data): } -class JsonEncoder(json.JSONEncoder): - def default(self, o): - if hasattr(o, "__to_json__"): - return o.__to_json__() - else: - return super().default(o) - - -def json_encode(data, *args, **kwargs): - return json.dumps(data, *args, cls=JsonEncoder, **kwargs) - - def emit(data): - print(json_encode(filtered_data(data), ensure_ascii=False)) + print(json.dumps(filtered_data(data), ensure_ascii=False)) def main():