Rename RETAIN_ to VERBATIM_

This commit is contained in:
Midgard 2024-01-16 02:31:03 +01:00
parent 9f5847088c
commit 5706443217
Signed by: midgard
GPG key ID: 511C112F1331BBB4
2 changed files with 14 additions and 14 deletions

View file

@ -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
def test_camel_to_snake():
@ -15,9 +15,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,

View file

@ -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,