#!/usr/bin/env python3 import re import json import socketio WS_URL = "wss://trainmap.belgiantrain.be/socket.io/" class VERBATIM_KEY: pass class VERBATIM_VALUE: pass class DISCARD: pass def transform_dict(specification: dict, d: dict): """ specification is a dict where the key is used to determine which action to take with the same key in the data. The values to use in specification are: - 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 VERBATIM_KEY or VERBATIM_VALUE respectively { """ return { ( specification[k][0] 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 VERBATIM_VALUE else v ) for k, v in d.items() if specification.get(k) is not DISCARD } def camel_to_snake(s: str): if s.islower(): return s return re.sub( r"(?