def write_compacted(g):
d_nodes = {}<EOL>d_edges = {}<EOL>def conv(value):<EOL><INDENT>if isinstance(value, str):<EOL><INDENT>return value.strip('<STR_LIT:">')<EOL><DEDENT>else:<EOL><INDENT>return value<EOL><DEDENT><DEDENT>for node in g.nodes():<EOL><INDENT>label = None<EOL>attrs = []<EOL>for k, v in sorted(g.node_attributes(node)):<EOL><INDENT>v_ = conv(v)<EOL>if k == "<STR_LIT:label>":<EOL><INDENT>label = v_<EOL><DEDENT>else:<EOL><INDENT>attrs.append((k, v_))<EOL><DEDENT><DEDENT>value = (node, label) if label else node<EOL>d_nodes.setdefault(tuple(attrs), []).append(value)<EOL><DEDENT>for edge in g.edges():<EOL><INDENT>attrs = [(k, conv(v)) for k, v in sorted(g.edge_attributes(edge))]<EOL>label = str(g.edge_label(edge))<EOL>value = tuple(list(edge) + [label]) if label else edge<EOL>d_edges.setdefault(tuple(attrs), []).append(tuple(value))<EOL><DEDENT>doc = dict(nodes=list(d_nodes.items()), edges=list(d_edges.items()))<EOL>contents = str(doc)<EOL>return contents<EOL>
Write a graph in our own compacted format.

    Returns:
        str.