<s> import dot_parser <EOL> def unquote ( s ) : <EOL> return s [ <NUM_LIT:1> : - <NUM_LIT:1> ] if s and s [ <NUM_LIT:0> ] == s [ - <NUM_LIT:1> ] and s [ <NUM_LIT:0> ] in "<STR_LIT>" else s <EOL> def deserialize ( d , ** kw ) : <EOL> seqno = ( lambda q = iter ( xrange ( <NUM_LIT:0> , <NUM_LIT:2> ** <NUM_LIT> ) ) . next : '<STR_LIT>' % q ( ) ) <EOL> dot = dot_parser . parse_dot_data ( d ) <EOL> verts = dict ( ( unquote ( node . get_name ( ) ) , <EOL> unquote ( node . get_label ( ) or node . get_name ( ) ) ) <EOL> for node in dot . get_nodes ( ) <EOL> if node . get_name ( ) not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> edges = [ ( seqno ( ) , <EOL> unquote ( link . get_label ( ) ) , <EOL> unquote ( link . get_source ( ) ) , <EOL> unquote ( link . get_destination ( ) ) ) <EOL> for link in dot . get_edges ( ) ] <EOL> for i , l , s , t in edges : <EOL> verts . setdefault ( s , s ) <EOL> verts . setdefault ( t , t ) <EOL> verts = verts . items ( ) <EOL> if dot . get_graph_type ( ) == '<STR_LIT>' : <EOL> edges . extend ( [ ( seqno ( ) , l , t , s ) for i , l , s , t in edges ] ) <EOL> return verts , edges <EOL> def serialize ( VE , name = "<STR_LIT>" , ** kw ) : <EOL> highattr = '<STR_LIT>' <EOL> highlight = kw . get ( '<STR_LIT>' ) or [ ] <EOL> V , E = VE [ : <NUM_LIT:2> ] <EOL> s = "<STR_LIT>" % ( name , ) <EOL> for v in sorted ( V . values ( ) ) : <EOL> x = highattr if v [ <NUM_LIT:0> ] in highlight else '<STR_LIT>' <EOL> s += "<STR_LIT>" % ( <EOL> v [ <NUM_LIT:0> ] , ( v [ <NUM_LIT:1> ] or '<STR_LIT>' ) . replace ( '<STR_LIT:\n>' , '<STR_LIT:U+0020>' ) , x ) <EOL> s += "<STR_LIT:\n>" <EOL> for e in sorted ( E . values ( ) ) : <EOL> x = highattr if e [ <NUM_LIT:0> ] in highlight else '<STR_LIT>' <EOL> s += "<STR_LIT>" % ( <EOL> e [ <NUM_LIT:2> ] , e [ <NUM_LIT:3> ] , ( e [ <NUM_LIT:1> ] or '<STR_LIT>' ) . replace ( '<STR_LIT:\n>' , '<STR_LIT:U+0020>' ) , x ) <EOL> s += "<STR_LIT>" <EOL> return s </s>
