nodes: 0, module; 1, function_definition; 2, function_name:cdx_sort_closest; 3, parameters; 4, block; 5, identifier:closest; 6, identifier:cdx_iter; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, for_statement; 13, for_statement; 14, identifier:limit; 15, integer:10; 16, comment:"""
      sort CDXCaptureResult by closest to timestamp.
      """; 17, assignment; 18, assignment; 19, assignment; 20, identifier:cdx; 21, identifier:cdx_iter; 22, block; 23, identifier:cdx; 24, identifier:closest_cdx; 25, block; 26, identifier:closest_cdx; 27, list; 28, identifier:closest_keys; 29, list; 30, identifier:closest_sec; 31, call; 32, expression_statement; 33, expression_statement; 34, comment:# create tuple to sort by key; 35, comment:#bisect.insort(closest_cdx, (key, cdx)); 36, expression_statement; 37, expression_statement; 38, expression_statement; 39, if_statement; 40, if_statement; 41, expression_statement; 42, identifier:timestamp_to_sec; 43, argument_list; 44, assignment; 45, assignment; 46, assignment; 47, call; 48, call; 49, comparison_operator:len(closest_cdx) == limit; 50, comment:# assuming cdx in ascending order and keys have started increasing; 51, block; 52, comparison_operator:len(closest_cdx) > limit; 53, block; 54, yield; 55, identifier:closest; 56, identifier:sec; 57, call; 58, identifier:key; 59, call; 60, identifier:i; 61, call; 62, attribute; 63, argument_list; 64, attribute; 65, argument_list; 66, call; 67, identifier:limit; 68, if_statement; 69, call; 70, identifier:limit; 71, expression_statement; 72, identifier:cdx; 73, identifier:timestamp_to_sec; 74, argument_list; 75, identifier:abs; 76, argument_list; 77, attribute; 78, argument_list; 79, identifier:closest_keys; 80, identifier:insert; 81, identifier:i; 82, identifier:key; 83, identifier:closest_cdx; 84, identifier:insert; 85, identifier:i; 86, identifier:cdx; 87, identifier:len; 88, argument_list; 89, comparison_operator:key > closest_keys[-1]; 90, block; 91, identifier:len; 92, argument_list; 93, call; 94, subscript; 95, binary_operator:closest_sec - sec; 96, identifier:bisect; 97, identifier:bisect_right; 98, identifier:closest_keys; 99, identifier:key; 100, identifier:closest_cdx; 101, identifier:key; 102, subscript; 103, break_statement; 104, identifier:closest_cdx; 105, attribute; 106, argument_list; 107, identifier:cdx; 108, identifier:TIMESTAMP; 109, identifier:closest_sec; 110, identifier:sec; 111, identifier:closest_keys; 112, unary_operator; 113, identifier:closest_cdx; 114, identifier:pop; 115, integer:1
code: def cdx_sort_closest(closest, cdx_iter, limit=10):
      """
      sort CDXCaptureResult by closest to timestamp.
      """
      closest_cdx = []
      closest_keys = []
      closest_sec = timestamp_to_sec(closest)
  
      for cdx in cdx_iter:
          sec = timestamp_to_sec(cdx[TIMESTAMP])
          key = abs(closest_sec - sec)
  
          # create tuple to sort by key
          #bisect.insort(closest_cdx, (key, cdx))
  
          i = bisect.bisect_right(closest_keys, key)
          closest_keys.insert(i, key)
          closest_cdx.insert(i, cdx)
  
          if len(closest_cdx) == limit:
              # assuming cdx in ascending order and keys have started increasing
              if key > closest_keys[-1]:
                  break
  
          if len(closest_cdx) > limit:
              closest_cdx.pop()
  
      for cdx in closest_cdx:
          yield cdx