<s> import io <EOL> class Streamer ( object ) : <EOL> def __init__ ( self ) : <EOL> self . parse_lookup = { } <EOL> self . stream_lookup = { } <EOL> def register_functions ( self , lookup ) : <EOL> for c , v in lookup : <EOL> parse_f , stream_f = v <EOL> self . parse_lookup [ c ] = parse_f <EOL> self . stream_lookup [ c ] = stream_f <EOL> def register_array_count_parse ( self , array_count_parse_f ) : <EOL> self . array_count_parse_f = array_count_parse_f <EOL> def parse_struct ( self , fmt , f ) : <EOL> l = [ ] <EOL> i = <NUM_LIT:0> <EOL> while i < len ( fmt ) : <EOL> c = fmt [ i ] <EOL> if c == "<STR_LIT:[>" : <EOL> end = fmt . find ( "<STR_LIT:]>" , i ) <EOL> if end < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> subfmt = fmt [ i + <NUM_LIT:1> : end ] <EOL> count = self . array_count_parse_f ( f ) <EOL> array = [ ] <EOL> for j in range ( count ) : <EOL> if len ( subfmt ) == <NUM_LIT:1> : <EOL> array . append ( self . parse_struct ( subfmt , f ) [ <NUM_LIT:0> ] ) <EOL> else : <EOL> array . append ( self . parse_struct ( subfmt , f ) ) <EOL> l . append ( tuple ( array ) ) <EOL> i = end <EOL> else : <EOL> l . append ( self . parse_lookup [ c ] ( f ) ) <EOL> i += <NUM_LIT:1> <EOL> return tuple ( l ) <EOL> def parse_as_dict ( self , attribute_list , pack_list , f ) : <EOL> return dict ( list ( zip ( attribute_list , self . parse_struct ( pack_list , f ) ) ) ) <EOL> def stream_struct ( self , fmt , f , * args ) : <EOL> for c , v in zip ( fmt , args ) : <EOL> self . stream_lookup [ c ] ( f , v ) <EOL> def unpack_struct ( self , fmt , b ) : <EOL> return self . parse_struct ( fmt , io . BytesIO ( b ) ) <EOL> def pack_struct ( self , fmt , * args ) : <EOL> b = io . BytesIO ( ) <EOL> self . stream_struct ( fmt , b , * args ) <EOL> return b . getvalue ( ) </s>
