nodes: 0, module; 1, function_definition; 2, function_name:_encode_penman; 3, parameters; 4, block; 5, identifier:self; 6, identifier:g; 7, default_parameter; 8, expression_statement; 9, if_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, comment:# (preferred, dispreferred); 14, for_statement; 15, expression_statement; 16, expression_statement; 17, function_definition; 18, function_definition; 19, expression_statement; 20, while_statement; 21, return_statement; 22, identifier:top; 23, None; 24, comment:"""
          Walk graph g and find a spanning dag, then serialize the result.
  
          First, depth-first traversal of preferred orientations (whether
          true or inverted) to create graph p.
  
          If any triples remain, select the first remaining triple whose
          source in the dispreferred orientation exists in p, where
          'first' is determined by the order of inserted nodes (i.e. a
          topological sort). Add this triple, then repeat the depth-first
          traversal of preferred orientations from its target. Repeat
          until no triples remain, or raise an error if there are no
          candidates in the dispreferred orientation (which likely means
          the graph is disconnected).
          """; 25, comparison_operator:top is None; 26, block; 27, assignment; 28, assignment; 29, assignment; 30, identifier:t; 31, call; 32, block; 33, assignment; 34, assignment; 35, function_name:_update; 36, parameters; 37, block; 38, function_name:_explore_preferred; 39, parameters; 40, block; 41, call; 42, identifier:remaining; 43, block; 44, call; 45, identifier:top; 46, None; 47, expression_statement; 48, identifier:remaining; 49, call; 50, identifier:variables; 51, call; 52, identifier:store; 53, call; 54, attribute; 55, argument_list; 56, if_statement; 57, identifier:p; 58, call; 59, identifier:topolist; 60, list; 61, identifier:t; 62, expression_statement; 63, expression_statement; 64, expression_statement; 65, if_statement; 66, return_statement; 67, identifier:src; 68, expression_statement; 69, for_statement; 70, expression_statement; 71, comment:# clear explored list; 72, identifier:_explore_preferred; 73, argument_list; 74, expression_statement; 75, for_statement; 76, if_statement; 77, expression_statement; 78, expression_statement; 79, if_statement; 80, attribute; 81, argument_list; 82, assignment; 83, identifier:set; 84, argument_list; 85, attribute; 86, argument_list; 87, identifier:defaultdict; 88, argument_list; 89, identifier:g; 90, identifier:triples; 91, attribute; 92, block; 93, else_clause; 94, identifier:defaultdict; 95, argument_list; 96, identifier:top; 97, assignment; 98, call; 99, call; 100, boolean_operator; 101, block; 102, None; 103, assignment; 104, identifier:t; 105, identifier:ts; 106, block; 107, assignment; 108, identifier:top; 109, assignment; 110, identifier:fc; 111, identifier:flip_candidates; 112, block; 113, not_operator; 114, block; 115, assignment; 116, assignment; 117, comparison_operator:tgt is not None; 118, block; 119, identifier:self; 120, identifier:_layout; 121, identifier:p; 122, identifier:top; 123, integer:0; 124, call; 125, identifier:top; 126, attribute; 127, call; 128, identifier:g; 129, identifier:variables; 130, lambda; 131, identifier:t; 132, identifier:inverted; 133, expression_statement; 134, expression_statement; 135, block; 136, identifier:list; 137, pattern_list; 138, conditional_expression:(t[2], t[0]) if t.inverted else (t[0], t[2]); 139, attribute; 140, argument_list; 141, attribute; 142, argument_list; 143, comparison_operator:tgt in variables; 144, comparison_operator:t.relation != self.TYPE_REL; 145, expression_statement; 146, return_statement; 147, identifier:ts; 148, subscript; 149, if_statement; 150, subscript; 151, list; 152, identifier:flip_candidates; 153, list_comprehension; 154, expression_statement; 155, comment:# clear superfluous; 156, call; 157, raise_statement; 158, identifier:c; 159, call; 160, identifier:tgt; 161, call; 162, identifier:tgt; 163, None; 164, expression_statement; 165, identifier:set; 166, argument_list; 167, identifier:g; 168, identifier:top; 169, attribute; 170, argument_list; 171, tuple; 172, call; 173, call; 174, expression_statement; 175, expression_statement; 176, identifier:src; 177, identifier:tgt; 178, tuple; 179, attribute; 180, tuple; 181, subscript; 182, identifier:append; 183, identifier:t; 184, identifier:remaining; 185, identifier:remove; 186, identifier:t; 187, identifier:tgt; 188, identifier:variables; 189, attribute; 190, attribute; 191, call; 192, identifier:tgt; 193, call; 194, integer:0; 195, comparison_operator:t in remaining; 196, block; 197, identifier:ts; 198, slice; 199, subscript; 200, for_in_clause; 201, assignment; 202, identifier:any; 203, generator_expression; 204, call; 205, identifier:next; 206, generator_expression; 207, identifier:_update; 208, argument_list; 209, call; 210, identifier:g; 211, identifier:triples; 212, list; 213, list; 214, attribute; 215, argument_list; 216, attribute; 217, argument_list; 218, call; 219, call; 220, subscript; 221, subscript; 222, identifier:t; 223, identifier:inverted; 224, subscript; 225, subscript; 226, identifier:p; 227, identifier:src; 228, identifier:t; 229, identifier:relation; 230, identifier:self; 231, identifier:TYPE_REL; 232, attribute; 233, argument_list; 234, attribute; 235, argument_list; 236, identifier:t; 237, identifier:remaining; 238, expression_statement; 239, if_statement; 240, call; 241, integer:1; 242, identifier:v; 243, identifier:topolist; 244, subscript; 245, list_comprehension; 246, comparison_operator:len(fc) > 0; 247, for_in_clause; 248, identifier:EncodeError; 249, argument_list; 250, identifier:c; 251, for_in_clause; 252, for_in_clause; 253, identifier:c; 254, identifier:_explore_preferred; 255, argument_list; 256, subscript; 257, identifier:append; 258, identifier:t; 259, subscript; 260, identifier:append; 261, call; 262, attribute; 263, argument_list; 264, attribute; 265, argument_list; 266, identifier:t; 267, integer:2; 268, identifier:t; 269, integer:0; 270, identifier:t; 271, integer:0; 272, identifier:t; 273, integer:2; 274, identifier:topolist; 275, identifier:append; 276, identifier:tgt; 277, identifier:store; 278, identifier:get; 279, identifier:src; 280, tuple; 281, assignment; 282, comparison_operator:tgt is not None; 283, block; 284, attribute; 285, argument_list; 286, identifier:fc; 287, slice; 288, identifier:c; 289, for_in_clause; 290, if_clause; 291, call; 292, integer:0; 293, identifier:fc; 294, identifier:flip_candidates; 295, string; 296, identifier:fc; 297, identifier:flip_candidates; 298, identifier:c; 299, identifier:fc; 300, identifier:tgt; 301, subscript; 302, integer:0; 303, subscript; 304, integer:1; 305, identifier:Triple; 306, argument_list; 307, subscript; 308, identifier:append; 309, identifier:t; 310, subscript; 311, identifier:append; 312, call; 313, list; 314, list; 315, identifier:tgt; 316, call; 317, identifier:tgt; 318, None; 319, expression_statement; 320, identifier:store; 321, identifier:get; 322, identifier:v; 323, tuple; 324, identifier:c; 325, identifier:fc; 326, comparison_operator:c in remaining; 327, identifier:len; 328, argument_list; 329, string_content:Invalid graph; possibly disconnected.; 330, identifier:store; 331, attribute; 332, identifier:store; 333, attribute; 334, list_splat; 335, keyword_argument; 336, subscript; 337, integer:0; 338, subscript; 339, integer:1; 340, identifier:Triple; 341, argument_list; 342, identifier:_update; 343, argument_list; 344, call; 345, list; 346, list; 347, identifier:c; 348, identifier:remaining; 349, identifier:fc; 350, identifier:t; 351, identifier:target; 352, identifier:t; 353, identifier:source; 354, identifier:t; 355, identifier:inverted; 356, False; 357, identifier:store; 358, attribute; 359, identifier:store; 360, attribute; 361, list_splat; 362, keyword_argument; 363, identifier:t; 364, identifier:_explore_preferred; 365, argument_list; 366, identifier:t; 367, identifier:source; 368, identifier:t; 369, identifier:target; 370, identifier:t; 371, identifier:inverted; 372, True; 373, identifier:tgt
code: def _encode_penman(self, g, top=None):
          """
          Walk graph g and find a spanning dag, then serialize the result.
  
          First, depth-first traversal of preferred orientations (whether
          true or inverted) to create graph p.
  
          If any triples remain, select the first remaining triple whose
          source in the dispreferred orientation exists in p, where
          'first' is determined by the order of inserted nodes (i.e. a
          topological sort). Add this triple, then repeat the depth-first
          traversal of preferred orientations from its target. Repeat
          until no triples remain, or raise an error if there are no
          candidates in the dispreferred orientation (which likely means
          the graph is disconnected).
          """
          if top is None:
              top = g.top
          remaining = set(g.triples())
          variables = g.variables()
          store = defaultdict(lambda: ([], []))  # (preferred, dispreferred)
          for t in g.triples():
              if t.inverted:
                  store[t.target][0].append(t)
                  store[t.source][1].append(Triple(*t, inverted=False))
              else:
                  store[t.source][0].append(t)
                  store[t.target][1].append(Triple(*t, inverted=True))
  
          p = defaultdict(list)
          topolist = [top]
  
          def _update(t):
              src, tgt = (t[2], t[0]) if t.inverted else (t[0], t[2])
              p[src].append(t)
              remaining.remove(t)
              if tgt in variables and t.relation != self.TYPE_REL:
                  topolist.append(tgt)
                  return tgt
              return None
  
          def _explore_preferred(src):
              ts = store.get(src, ([], []))[0]
              for t in ts:
                  if t in remaining:
                      tgt = _update(t)
                      if tgt is not None:
                          _explore_preferred(tgt)
              ts[:] = []  # clear explored list
  
          _explore_preferred(top)
  
          while remaining:
              flip_candidates = [store.get(v, ([],[]))[1] for v in topolist]
              for fc in flip_candidates:
                  fc[:] = [c for c in fc if c in remaining]  # clear superfluous
              if not any(len(fc) > 0 for fc in flip_candidates):
                  raise EncodeError('Invalid graph; possibly disconnected.')
              c = next(c for fc in flip_candidates for c in fc)
              tgt = _update(c)
              if tgt is not None:
                  _explore_preferred(tgt)
  
          return self._layout(p, top, 0, set())