nodes: 0, module; 1, function_definition; 2, function_name:sorted; 3, parameters; 4, block; 5, identifier:self; 6, identifier:iterator; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, global_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, while_statement; 15, expression_statement; 16, if_statement; 17, if_statement; 18, return_statement; 19, identifier:key; 20, None; 21, identifier:reverse; 22, False; 23, comment:"""
          Sort the elements in iterator, do external sort when the memory
          goes above the limit.
          """; 24, identifier:MemoryBytesSpilled; 25, identifier:DiskBytesSpilled; 26, assignment; 27, assignment; 28, assignment; 29, True; 30, comment:# pick elements in batch; 31, block; 32, call; 33, not_operator; 34, block; 35, identifier:current_chunk; 36, block; 37, call; 38, pattern_list; 39, expression_list; 40, pattern_list; 41, expression_list; 42, identifier:iterator; 43, call; 44, expression_statement; 45, expression_statement; 46, if_statement; 47, expression_statement; 48, if_statement; 49, attribute; 50, argument_list; 51, identifier:chunks; 52, return_statement; 53, expression_statement; 54, attribute; 55, argument_list; 56, identifier:batch; 57, identifier:limit; 58, integer:100; 59, call; 60, identifier:chunks; 61, identifier:current_chunk; 62, list; 63, list; 64, identifier:iter; 65, argument_list; 66, assignment; 67, call; 68, comparison_operator:len(chunk) < batch; 69, block; 70, assignment; 71, comparison_operator:used_memory > limit; 72, comment:# sort them inplace will save memory; 73, block; 74, elif_clause; 75, identifier:current_chunk; 76, identifier:sort; 77, keyword_argument; 78, keyword_argument; 79, identifier:current_chunk; 80, call; 81, identifier:heapq; 82, identifier:merge; 83, identifier:chunks; 84, keyword_argument; 85, keyword_argument; 86, attribute; 87, argument_list; 88, identifier:iterator; 89, identifier:chunk; 90, call; 91, attribute; 92, argument_list; 93, call; 94, identifier:batch; 95, break_statement; 96, identifier:used_memory; 97, call; 98, identifier:used_memory; 99, identifier:limit; 100, expression_statement; 101, expression_statement; 102, with_statement; 103, function_definition; 104, expression_statement; 105, expression_statement; 106, expression_statement; 107, expression_statement; 108, expression_statement; 109, comment:# data will be deleted after close; 110, not_operator; 111, block; 112, identifier:key; 113, identifier:key; 114, identifier:reverse; 115, identifier:reverse; 116, attribute; 117, argument_list; 118, identifier:key; 119, identifier:key; 120, identifier:reverse; 121, identifier:reverse; 122, identifier:self; 123, identifier:_next_limit; 124, identifier:list; 125, argument_list; 126, identifier:current_chunk; 127, identifier:extend; 128, identifier:chunk; 129, identifier:len; 130, argument_list; 131, identifier:get_used_memory; 132, argument_list; 133, call; 134, assignment; 135, with_clause; 136, block; 137, function_name:load; 138, parameters; 139, block; 140, call; 141, assignment; 142, augmented_assignment; 143, augmented_assignment; 144, call; 145, identifier:chunks; 146, expression_statement; 147, identifier:chunks; 148, identifier:append; 149, call; 150, call; 151, identifier:chunk; 152, attribute; 153, argument_list; 154, identifier:path; 155, call; 156, with_item; 157, expression_statement; 158, identifier:f; 159, for_statement; 160, comment:# close the file explicit once we consume all the items; 161, comment:# to avoid ResourceWarning in Python3; 162, expression_statement; 163, attribute; 164, argument_list; 165, identifier:current_chunk; 166, list; 167, identifier:MemoryBytesSpilled; 168, binary_operator:max(used_memory - get_used_memory(), 0) << 20; 169, identifier:DiskBytesSpilled; 170, call; 171, attribute; 172, argument_list; 173, assignment; 174, identifier:iter; 175, argument_list; 176, attribute; 177, argument_list; 178, identifier:current_chunk; 179, identifier:sort; 180, keyword_argument; 181, keyword_argument; 182, attribute; 183, argument_list; 184, as_pattern; 185, call; 186, identifier:v; 187, call; 188, block; 189, call; 190, identifier:chunks; 191, identifier:append; 192, call; 193, call; 194, integer:20; 195, attribute; 196, argument_list; 197, identifier:os; 198, identifier:unlink; 199, identifier:path; 200, identifier:batch; 201, call; 202, identifier:current_chunk; 203, identifier:itertools; 204, identifier:islice; 205, identifier:iterator; 206, identifier:batch; 207, identifier:key; 208, identifier:key; 209, identifier:reverse; 210, identifier:reverse; 211, identifier:self; 212, identifier:_get_path; 213, call; 214, call; 215, as_pattern_target; 216, attribute; 217, argument_list; 218, attribute; 219, argument_list; 220, expression_statement; 221, attribute; 222, argument_list; 223, identifier:load; 224, argument_list; 225, identifier:max; 226, argument_list; 227, attribute; 228, identifier:getsize; 229, identifier:path; 230, identifier:min; 231, argument_list; 232, identifier:len; 233, argument_list; 234, identifier:open; 235, argument_list; 236, identifier:f; 237, attribute; 238, identifier:dump_stream; 239, identifier:current_chunk; 240, identifier:f; 241, attribute; 242, identifier:load_stream; 243, identifier:f; 244, yield; 245, identifier:f; 246, identifier:close; 247, call; 248, binary_operator:used_memory - get_used_memory(); 249, integer:0; 250, identifier:os; 251, identifier:path; 252, call; 253, integer:10000; 254, identifier:chunks; 255, identifier:path; 256, string; 257, identifier:self; 258, identifier:serializer; 259, identifier:self; 260, identifier:serializer; 261, identifier:v; 262, identifier:open; 263, argument_list; 264, identifier:used_memory; 265, call; 266, identifier:int; 267, argument_list; 268, string_content:wb; 269, identifier:path; 270, string; 271, identifier:get_used_memory; 272, argument_list; 273, binary_operator:batch * 1.5; 274, string_content:rb; 275, identifier:batch; 276, float:1.5
code: def sorted(self, iterator, key=None, reverse=False):
          """
          Sort the elements in iterator, do external sort when the memory
          goes above the limit.
          """
          global MemoryBytesSpilled, DiskBytesSpilled
          batch, limit = 100, self._next_limit()
          chunks, current_chunk = [], []
          iterator = iter(iterator)
          while True:
              # pick elements in batch
              chunk = list(itertools.islice(iterator, batch))
              current_chunk.extend(chunk)
              if len(chunk) < batch:
                  break
  
              used_memory = get_used_memory()
              if used_memory > limit:
                  # sort them inplace will save memory
                  current_chunk.sort(key=key, reverse=reverse)
                  path = self._get_path(len(chunks))
                  with open(path, 'wb') as f:
                      self.serializer.dump_stream(current_chunk, f)
  
                  def load(f):
                      for v in self.serializer.load_stream(f):
                          yield v
                      # close the file explicit once we consume all the items
                      # to avoid ResourceWarning in Python3
                      f.close()
                  chunks.append(load(open(path, 'rb')))
                  current_chunk = []
                  MemoryBytesSpilled += max(used_memory - get_used_memory(), 0) << 20
                  DiskBytesSpilled += os.path.getsize(path)
                  os.unlink(path)  # data will be deleted after close
  
              elif not chunks:
                  batch = min(int(batch * 1.5), 10000)
  
          current_chunk.sort(key=key, reverse=reverse)
          if not chunks:
              return current_chunk
  
          if current_chunk:
              chunks.append(iter(current_chunk))
  
          return heapq.merge(chunks, key=key, reverse=reverse)