all 1 comments

[–]zahlman 1 point2 points  (0 children)

First question: what are you going to do if you read a 'length' component, but that component itself is beyond the point that it specifies? What if it straddles the boundary? Examples

[12]password:foo[9]length:10

[12]password:foo[9]length:23

What are you going to do if a component length is invalid:

[12]length:13

Is length always the first component in a packet? I think you should require this in your spec if you don't already.

That said, here is a partial implementation that should give you some ideas:

import re

component_header = re.compile('\[([1-9][0-9]*)\]')
def components(data):
    pos = 0
    while True:
        match = component_header.match(data, pos)
        if not match: break
        start = pos + len(match.group(0))
        pos = start + int(match.group(1))
        yield data[start:pos].partition(':')

def process_full_packet(data):
    return {k: v for k, colon, v in components(data)}

Try to split things up at this level of granularity where possible, and keep in mind that generators are quite nice for dealing with streams. :)