<s> """<STR_LIT>""" <EOL> import re <EOL> token_pattern = '<STR_LIT>' <EOL> key_pattern = '<STR_LIT>' <EOL> def check_key ( s ) : <EOL> assert re . match ( key_pattern , s ) , '<STR_LIT>' % s <EOL> return s <EOL> def parse_value ( s , ts ) : <EOL> if s == '<STR_LIT:[>' : <EOL> return parse ( ts ) <EOL> elif ( s [ <NUM_LIT:0> ] + s [ - <NUM_LIT:1> ] ) == '<STR_LIT>' : <EOL> return ts , s [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> elif '<STR_LIT:.>' in s : <EOL> return ts , float ( s ) <EOL> else : <EOL> return ts , int ( s ) <EOL> def parse ( ts ) : <EOL> d = [ ] <EOL> while len ( ts ) > <NUM_LIT:1> and ts [ <NUM_LIT:0> ] not in '<STR_LIT:]>' : <EOL> key = check_key ( ts [ <NUM_LIT:0> ] ) <EOL> ts , val = parse_value ( ts [ <NUM_LIT:1> ] , ts [ <NUM_LIT:2> : ] ) <EOL> d . append ( ( key , val ) ) <EOL> return ts [ <NUM_LIT:1> : ] , tuple ( d ) <EOL> def build_vert ( vl ) : <EOL> v_id , v_name = None , None <EOL> for key , value in vl : <EOL> if key == '<STR_LIT:id>' : <EOL> v_id = str ( value ) <EOL> elif key == '<STR_LIT:label>' : <EOL> v_name = value <EOL> assert v_id is not None <EOL> v_name = v_id if v_name is None else v_name <EOL> return ( v_id , v_name ) <EOL> def build_edge ( el , serial ) : <EOL> e_id , e_name , e_src , e_tgt = '<STR_LIT>' % serial , None , None , None <EOL> for key , value in el : <EOL> if key == '<STR_LIT:label>' : <EOL> e_name = value <EOL> elif key == '<STR_LIT:source>' : <EOL> e_src = str ( value ) <EOL> elif key == '<STR_LIT:target>' : <EOL> e_tgt = str ( value ) <EOL> return ( e_id , e_name , e_src , e_tgt ) <EOL> ID , NAME , EDGES , SRC , TGT = <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:3> <EOL> def deserialize ( d , ** kw ) : <EOL> commented = re . sub ( "<STR_LIT>" , "<STR_LIT:\n>" , d ) <EOL> tokenized = [ mo [ <NUM_LIT:0> ] or mo [ <NUM_LIT:1> ] for mo in re . findall ( token_pattern , commented ) ] <EOL> empty , tree = parse ( tokenized ) <EOL> serial = iter ( xrange ( <NUM_LIT:0> , <NUM_LIT:2> ** <NUM_LIT> ) ) . next <EOL> verts , edges = [ ] , [ ] <EOL> for key , node in tree : <EOL> if key . lower ( ) == '<STR_LIT>' : <EOL> for key , val in node : <EOL> if key . lower ( ) == '<STR_LIT>' : <EOL> verts . append ( build_vert ( val ) ) <EOL> elif key . lower ( ) == '<STR_LIT>' : <EOL> edges . append ( build_edge ( val , serial ( ) ) ) <EOL> return verts , edges <EOL> else : <EOL> assert False , "<STR_LIT>" </s>
