A telemetry feed delivers readings as raw strings of key=value pairs separated by semicolons (;), and some of those strings are malformed. Write a parser that converts one telemetry string into a dictionary (a JSON object) with typed values.
The parser trims whitespace around keys and values, keeps the last occurrence when a key repeats, maps an empty value such as key= to None, skips segments that contain no =, and splits each segment only at its first =, so a value may contain more = characters. Integer values become int, float values become float, true and false in any letter case become booleans, and every other value stays a trimmed string. An empty or whitespace-only input returns an empty dictionary.
Function Signature
def parse_telemetry(telemetry: str) -> dict:
...
Rules
-
Split the input on
;
. Each piece is a segment. A segment with no
=
is skipped, and that includes empty and whitespace-only segments.
-
Split a segment at its first
=
. The text before it is the key and the text after it is the raw value. Trim leading and trailing whitespace from both, as Python's
str.strip()
does.
-
A segment whose key is empty after trimming (for example
=5
) is skipped.
-
Type the trimmed value by checking these cases in order:
-
An empty string becomes
None
.
-
true
or
false
in any letter case becomes
True
or
False
.
-
An optional
+
or
-
followed by one or more digits
0-9
, and nothing else, becomes an
int
. Leading zeros are allowed, so
007
becomes
7
.
-
An optional sign, then either digits with an optional fractional part (
36.6
,
3.
) or a fractional part alone (
.5
), then an optional exponent (
e
or
E
, an optional sign, one or more digits) becomes a
float
. For example
1e3
becomes
1000.0
.
-
Anything else stays a string. That includes
inf
,
nan
,
1_000
,
0x1F
and
1.2.3
.
-
When a key appears more than once, the value from its last occurrence is kept. Key order in the returned dictionary does not matter.
Constraints
-
0 <= len(telemetry) <= 10^4
-
Keys and values may have leading or trailing whitespace.
Examples
Input: telemetry = "sensor_id=42; temperature=36.6; active=true; label=main unit"
Output: {"sensor_id": 42, "temperature": 36.6, "active": True, "label": "main unit"}
There are four valid pairs. 42 is an integer, 36.6 is a float, true is a boolean, and main unit matches no other type, so it stays a string with its inner space.
Input: telemetry = "key1=hello; key1=world; empty=; novalue; key2=3.14; bool=FALSE"
Output: {"key1": "world", "empty": None, "key2": 3.14, "bool": False}
key1 appears twice and keeps its last value, world. empty= has an empty value, so it maps to None, and novalue has no =, so it is skipped. FALSE is a boolean because the match ignores case.
Input: telemetry = " query = mode=fast ; =orphan ;count= -7;ratio=1e3;; flag = True "
Output: {"query": "mode=fast", "count": -7, "ratio": 1000.0, "flag": True}
query is split only at its first =, so its value is mode=fast. =orphan has an empty key and the empty segment between ;; has no =, so both are skipped. -7 is an integer, 1e3 is a float in exponent form, and True is a boolean.