Python parser for compact-array capture objects
-
The parser below is intentionally small and defensive. It rejects trailing bytes instead of guessing a schema.
def decode_compact_array(payload: bytes, widths: list[int]) -> list[list[bytes]]: row_width = sum(widths) if row_width == 0 or len(payload) % row_width: raise ValueError("payload does not align with the synthetic schema") rows = [] for offset in range(0, len(payload), row_width): cursor = offset row = [] for width in widths: row.append(payload[cursor:cursor + width]) cursor += width rows.append(row) return rowsColumn Width Synthetic meaning timestamp 12 octet string active energy 4 unsigned status 1 bit flags -
Keep the structure description beside the payload in test evidence. Without it, a compact array cannot be decoded unambiguously.
-
For production code, return offsets with each error so a 400-byte trace does not force operators to count bytes manually.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login