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
9f5847088c
2 changed files with 6 additions and 61 deletions
22
tests.py
22
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]]"
|
||||
|
|
|
@ -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():
|
||||
|
|
Loading…
Reference in a new issue