nodes: 0, module; 1, function_definition; 2, function_name:_sort_records_map; 3, parameters; 4, block; 5, identifier:records; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, for_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, expression_statement; 17, expression_statement; 18, expression_statement; 19, expression_statement; 20, expression_statement; 21, with_statement; 22, expression_statement; 23, expression_statement; 24, expression_statement; 25, expression_statement; 26, comment:"""Map function sorting records.
  
    Converts records to KeyValue protos, sorts them by key and writes them
    into new GCS file. Creates _OutputFile entity to record resulting
    file name.
  
    Args:
      records: list of records which are serialized KeyValue protos.
    """; 27, assignment; 28, assignment; 29, assignment; 30, call; 31, identifier:i; 32, call; 33, block; 34, call; 35, call; 36, call; 37, assignment; 38, assignment; 39, assignment; 40, assignment; 41, assignment; 42, assignment; 43, with_clause; 44, block; 45, call; 46, call; 47, assignment; 48, call; 49, identifier:ctx; 50, call; 51, identifier:l; 52, call; 53, identifier:key_records; 54, binary_operator:[None] * l; 55, attribute; 56, argument_list; 57, identifier:range; 58, argument_list; 59, expression_statement; 60, expression_statement; 61, expression_statement; 62, attribute; 63, argument_list; 64, attribute; 65, argument_list; 66, attribute; 67, argument_list; 68, identifier:mapper_spec; 69, attribute; 70, identifier:params; 71, call; 72, identifier:bucket_name; 73, call; 74, identifier:filename; 75, parenthesized_expression; 76, identifier:full_filename; 77, binary_operator:"/%s/%s" % (bucket_name, filename); 78, identifier:filehandle; 79, call; 80, with_item; 81, for_statement; 82, attribute; 83, argument_list; 84, attribute; 85, argument_list; 86, identifier:entity; 87, call; 88, attribute; 89, argument_list; 90, attribute; 91, argument_list; 92, identifier:len; 93, argument_list; 94, list; 95, identifier:l; 96, identifier:logging; 97, identifier:debug; 98, string:"Parsing"; 99, identifier:l; 100, assignment; 101, call; 102, assignment; 103, identifier:logging; 104, identifier:debug; 105, string:"Sorting"; 106, identifier:key_records; 107, identifier:sort; 108, keyword_argument; 109, identifier:logging; 110, identifier:debug; 111, string:"Writing"; 112, attribute; 113, identifier:mapper; 114, attribute; 115, argument_list; 116, attribute; 117, argument_list; 118, binary_operator:ctx.mapreduce_spec.name + "/" + ctx.mapreduce_id + "/output-" +
                ctx.shard_id + "-" + str(int(time.time())); 119, string:"/%s/%s"; 120, tuple; 121, attribute; 122, argument_list; 123, as_pattern; 124, identifier:key_record; 125, identifier:key_records; 126, block; 127, identifier:logging; 128, identifier:debug; 129, string:"Finalizing"; 130, identifier:filehandle; 131, identifier:close; 132, identifier:_OutputFile; 133, argument_list; 134, identifier:entity; 135, identifier:put; 136, identifier:context; 137, identifier:get; 138, identifier:records; 139, None; 140, identifier:proto; 141, call; 142, attribute; 143, argument_list; 144, subscript; 145, tuple; 146, identifier:cmp; 147, identifier:_compare_keys; 148, identifier:ctx; 149, identifier:mapreduce_spec; 150, identifier:input_readers; 151, identifier:_get_params; 152, identifier:mapper_spec; 153, identifier:params; 154, identifier:get; 155, string:"bucket_name"; 156, binary_operator:ctx.mapreduce_spec.name + "/" + ctx.mapreduce_id + "/output-" +
                ctx.shard_id + "-"; 157, call; 158, identifier:bucket_name; 159, identifier:filename; 160, identifier:cloudstorage; 161, identifier:open; 162, identifier:full_filename; 163, keyword_argument; 164, call; 165, as_pattern_target; 166, expression_statement; 167, keyword_argument; 168, keyword_argument; 169, attribute; 170, argument_list; 171, identifier:proto; 172, identifier:ParseFromString; 173, subscript; 174, identifier:key_records; 175, identifier:i; 176, call; 177, subscript; 178, binary_operator:ctx.mapreduce_spec.name + "/" + ctx.mapreduce_id + "/output-" +
                ctx.shard_id; 179, string:"-"; 180, identifier:str; 181, argument_list; 182, identifier:mode; 183, string:"w"; 184, attribute; 185, argument_list; 186, identifier:pool; 187, call; 188, identifier:key_name; 189, identifier:full_filename; 190, identifier:parent; 191, call; 192, identifier:kv_pb; 193, identifier:KeyValue; 194, identifier:records; 195, identifier:i; 196, attribute; 197, argument_list; 198, identifier:records; 199, identifier:i; 200, binary_operator:ctx.mapreduce_spec.name + "/" + ctx.mapreduce_id + "/output-"; 201, attribute; 202, call; 203, identifier:output_writers; 204, identifier:GCSRecordsPool; 205, identifier:filehandle; 206, keyword_argument; 207, attribute; 208, argument_list; 209, attribute; 210, argument_list; 211, identifier:proto; 212, identifier:key; 213, binary_operator:ctx.mapreduce_spec.name + "/" + ctx.mapreduce_id; 214, string:"/output-"; 215, identifier:ctx; 216, identifier:shard_id; 217, identifier:int; 218, argument_list; 219, identifier:ctx; 220, identifier:ctx; 221, identifier:pool; 222, identifier:append; 223, subscript; 224, identifier:_OutputFile; 225, identifier:get_root_key; 226, attribute; 227, binary_operator:ctx.mapreduce_spec.name + "/"; 228, attribute; 229, call; 230, identifier:key_record; 231, integer:1; 232, identifier:ctx; 233, identifier:mapreduce_id; 234, attribute; 235, string:"/"; 236, identifier:ctx; 237, identifier:mapreduce_id; 238, attribute; 239, argument_list; 240, attribute; 241, identifier:name; 242, identifier:time; 243, identifier:time; 244, identifier:ctx; 245, identifier:mapreduce_spec
code: def _sort_records_map(records):
    """Map function sorting records.
  
    Converts records to KeyValue protos, sorts them by key and writes them
    into new GCS file. Creates _OutputFile entity to record resulting
    file name.
  
    Args:
      records: list of records which are serialized KeyValue protos.
    """
    ctx = context.get()
    l = len(records)
    key_records = [None] * l
  
    logging.debug("Parsing")
    for i in range(l):
      proto = kv_pb.KeyValue()
      proto.ParseFromString(records[i])
      key_records[i] = (proto.key(), records[i])
  
    logging.debug("Sorting")
    key_records.sort(cmp=_compare_keys)
  
    logging.debug("Writing")
    mapper_spec = ctx.mapreduce_spec.mapper
    params = input_readers._get_params(mapper_spec)
    bucket_name = params.get("bucket_name")
    filename = (ctx.mapreduce_spec.name + "/" + ctx.mapreduce_id + "/output-" +
                ctx.shard_id + "-" + str(int(time.time())))
    full_filename = "/%s/%s" % (bucket_name, filename)
    filehandle = cloudstorage.open(full_filename, mode="w")
    with output_writers.GCSRecordsPool(filehandle, ctx=ctx) as pool:
      for key_record in key_records:
        pool.append(key_record[1])
  
    logging.debug("Finalizing")
    filehandle.close()
  
    entity = _OutputFile(key_name=full_filename,
                         parent=_OutputFile.get_root_key(ctx.mapreduce_id))
    entity.put()