Rename RETAIN_ to VERBATIM_
This commit is contained in:
parent
9f5847088c
commit
5706443217
2 changed files with 14 additions and 14 deletions
8
tests.py
8
tests.py
|
@ -1,5 +1,5 @@
|
||||||
from trainmap_backup import \
|
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():
|
def test_camel_to_snake():
|
||||||
|
@ -15,9 +15,9 @@ def test_transform_dict():
|
||||||
assert transform_dict(
|
assert transform_dict(
|
||||||
{
|
{
|
||||||
"discard": DISCARD,
|
"discard": DISCARD,
|
||||||
"otherkey": ("newkey", RETAIN_VALUE),
|
"otherkey": ("newkey", VERBATIM_VALUE),
|
||||||
"uppervalue": (RETAIN_KEY, str.upper),
|
"uppervalue": (VERBATIM_KEY, str.upper),
|
||||||
"retainboth": (RETAIN_KEY, RETAIN_VALUE)
|
"retainboth": (VERBATIM_KEY, VERBATIM_VALUE)
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"discard": True,
|
"discard": True,
|
||||||
|
|
|
@ -7,8 +7,8 @@ import socketio
|
||||||
WS_URL = "wss://trainmap.belgiantrain.be/socket.io/"
|
WS_URL = "wss://trainmap.belgiantrain.be/socket.io/"
|
||||||
|
|
||||||
|
|
||||||
class RETAIN_KEY: pass
|
class VERBATIM_KEY: pass
|
||||||
class RETAIN_VALUE: pass
|
class VERBATIM_VALUE: pass
|
||||||
class DISCARD: pass
|
class DISCARD: pass
|
||||||
def transform_dict(specification: dict, d: dict):
|
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 that should be discarded: DISCARD
|
||||||
- keys whose key and value should be retained verbatim: not present
|
- 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
|
- 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 {
|
return {
|
||||||
(
|
(
|
||||||
specification[k][0]
|
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
|
else k
|
||||||
): (
|
): (
|
||||||
specification[k][1](v)
|
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
|
else v
|
||||||
)
|
)
|
||||||
for k, v in d.items()
|
for k, v in d.items()
|
||||||
|
@ -61,17 +61,17 @@ 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 p: (
|
"position": (VERBATIM_KEY, lambda p: (
|
||||||
[round(p[0], 6), round(p[1], 6)]
|
[round(p[0], 6), round(p[1], 6)]
|
||||||
if isinstance(p, list) else p
|
if isinstance(p, list) else p
|
||||||
)),
|
)),
|
||||||
"current_traveled_distance": (RETAIN_KEY, lambda x: (
|
"current_traveled_distance": (VERBATIM_KEY, lambda x: (
|
||||||
round(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", VERBATIM_VALUE),
|
||||||
"nextstationdistance": ("next_station_distance", RETAIN_VALUE),
|
"nextstationdistance": ("next_station_distance", VERBATIM_VALUE),
|
||||||
"trip_headsign": (RETAIN_KEY, lambda x: transform_dict({
|
"trip_headsign": (VERBATIM_KEY, lambda x: transform_dict({
|
||||||
"default": DISCARD,
|
"default": DISCARD,
|
||||||
"en": DISCARD,
|
"en": DISCARD,
|
||||||
"de": DISCARD,
|
"de": DISCARD,
|
||||||
|
|
Loading…
Reference in a new issue