Remove fancy JsonRoundingFloat and JsonLatLon
because the Python JSON lib can't override the precise serialization anyway. Ugh.
This commit is contained in:
parent
3f7a499f7a
commit
accea00639
|
@ -1,6 +1,5 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from dataclasses import dataclass
|
|
||||||
import re
|
import re
|
||||||
import json
|
import json
|
||||||
import socketio
|
import socketio
|
||||||
|
@ -8,28 +7,6 @@ import socketio
|
||||||
WS_URL = "wss://trainmap.belgiantrain.be/socket.io/"
|
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_KEY: pass
|
||||||
class RETAIN_VALUE: pass
|
class RETAIN_VALUE: pass
|
||||||
class DISCARD: pass
|
class DISCARD: pass
|
||||||
|
@ -84,12 +61,12 @@ def filtered_data(data):
|
||||||
trip["trip_short_name"]: transform_dict({
|
trip["trip_short_name"]: transform_dict({
|
||||||
"trip_id": DISCARD,
|
"trip_id": DISCARD,
|
||||||
"trip_short_name": DISCARD,
|
"trip_short_name": DISCARD,
|
||||||
"position": (RETAIN_KEY, lambda l: (
|
"position": (RETAIN_KEY, lambda p: (
|
||||||
JsonLonLatCoord(*l)
|
[round(p[0], 6), round(p[1], 6)]
|
||||||
if isinstance(l, list) else l
|
if isinstance(p, list) else p
|
||||||
)),
|
)),
|
||||||
"current_traveled_distance": (RETAIN_KEY, lambda x: (
|
"current_traveled_distance": (RETAIN_KEY, lambda x: (
|
||||||
JsonRoundingFloat(x, 1)
|
round(x, 1)
|
||||||
if isinstance(x, float) else x
|
if isinstance(x, float) else x
|
||||||
)),
|
)),
|
||||||
"fromstationdistance": ("from_station_distance", RETAIN_VALUE),
|
"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):
|
def emit(data):
|
||||||
print(json_encode(filtered_data(data), ensure_ascii=False))
|
print(json.dumps(filtered_data(data), ensure_ascii=False))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
Loading…
Reference in a new issue