diff --git a/tests.py b/tests.py index c7b6851..4d2564d 100644 --- a/tests.py +++ b/tests.py @@ -1,5 +1,5 @@ from trainmap_backup import \ - camel_to_snake, transform_dict, RETAIN_KEY, RETAIN_VALUE, DISCARD, \ + camel_to_snake, transform_dict, VERBATIM_KEY, VERBATIM_VALUE, DISCARD, \ JsonRoundingFloat, JsonLonLatCoord, json_encode @@ -16,9 +16,9 @@ def test_transform_dict(): assert transform_dict( { "discard": DISCARD, - "otherkey": ("newkey", RETAIN_VALUE), - "uppervalue": (RETAIN_KEY, str.upper), - "retainboth": (RETAIN_KEY, RETAIN_VALUE) + "otherkey": ("newkey", VERBATIM_VALUE), + "uppervalue": (VERBATIM_KEY, str.upper), + "retainboth": (VERBATIM_KEY, VERBATIM_VALUE) }, { "discard": True, diff --git a/trainmap_backup.py b/trainmap_backup.py index 89e2f02..b88f31b 100755 --- a/trainmap_backup.py +++ b/trainmap_backup.py @@ -7,8 +7,8 @@ import socketio WS_URL = "wss://trainmap.belgiantrain.be/socket.io/" -class RETAIN_KEY: pass -class RETAIN_VALUE: pass +class VERBATIM_KEY: pass +class VERBATIM_VALUE: pass class DISCARD: pass def transform_dict(specification: dict, d: dict): """ @@ -17,18 +17,18 @@ def transform_dict(specification: dict, d: dict): - keys that should be discarded: DISCARD - keys whose key and value should be retained verbatim: not present - keys whose key and/or value should be transformed, with None denoting no change: tuple of - (str, function), or any of those two RETAIN_KEY or RETAIN_VALUE respectively + (str, function), or any of those two VERBATIM_KEY or VERBATIM_VALUE respectively { """ return { ( specification[k][0] - if specification.get(k) not in (None, DISCARD) and specification[k][0] is not RETAIN_KEY + if specification.get(k) not in (None, DISCARD) and specification[k][0] is not VERBATIM_KEY else k ): ( specification[k][1](v) - if specification.get(k) not in (None, DISCARD) and specification[k][1] is not RETAIN_VALUE + if specification.get(k) not in (None, DISCARD) and specification[k][1] is not VERBATIM_VALUE else v ) for k, v in d.items() @@ -61,17 +61,17 @@ def filtered_data(data): trip["trip_short_name"]: transform_dict({ "trip_id": DISCARD, "trip_short_name": DISCARD, - "position": (RETAIN_KEY, lambda p: ( + "position": (VERBATIM_KEY, lambda p: ( [round(p[0], 6), round(p[1], 6)] if isinstance(p, list) else p )), - "current_traveled_distance": (RETAIN_KEY, lambda x: ( + "current_traveled_distance": (VERBATIM_KEY, lambda x: ( round(x, 1) if isinstance(x, float) else x )), - "fromstationdistance": ("from_station_distance", RETAIN_VALUE), - "nextstationdistance": ("next_station_distance", RETAIN_VALUE), - "trip_headsign": (RETAIN_KEY, lambda x: transform_dict({ + "fromstationdistance": ("from_station_distance", VERBATIM_VALUE), + "nextstationdistance": ("next_station_distance", VERBATIM_VALUE), + "trip_headsign": (VERBATIM_KEY, lambda x: transform_dict({ "default": DISCARD, "en": DISCARD, "de": DISCARD,