Parse a Device Record
NetflixPhone screenMedium
A device log record is a single line of text:
machine_code, timestamp, field1, field2, $[free-form]&
It has four comma-separated fixed fields, then a free-form section that starts with $[ and ends with ]&. Spaces around every piece are ignored.
Implement parse_record(line, machines, keys). It returns a dictionary with the keys "machine", "timestamp", "fields", "attrs", in this order:
"machine":machines[machine_code], or"UNKNOWN"when the code is not inmachines."timestamp": the timestamp as an integer."fields": the list[field1, field2]as trimmed strings."attrs": the free-form pairs.
Free-form pairs are written key: value, separated by commas. Only the first colon in a pair separates the key from the value, so a value can contain colons. A key found in keys is renamed to keys[key]; other keys keep their own name. Empty items between commas are skipped, and $[]& gives an empty attrs.
Part 1: Fixed fields and flat pairs
Parse the fixed fields and the free-form pairs. In attrs, each key maps to its value as a trimmed string.
- Example 1
- keys
{"t":"title","v1":"video","v2":"audio"}- line
"123, 1231231231, AAA, BBB, $[v1: SSS, v2: TTT]&"- machines
{"7":"PACKAGER","42":"ORIGIN","123":"ENCODER"}- Output
{"attrs":{"audio":"TTT","video":"SSS"},"fields":["AAA","BBB"],"machine":"ENCODER","timestamp":1231231231}
Why: The code
123maps toENCODER, the timestamp becomes the integer1231231231, andv1andv2are renamed tovideoandaudio.
Constraints
1 <= len(line) <= 10^4- Field values and free-form values contain no
$and no& 0 <= timestamp <= 10^10linealways has the shape described above- Each key appears at most once in a record
- Values contain no
[or]