def _replace_code(code, replacements):
    """
    Replaces code with new code.
    :param str code: code to replace
    :param list replacements: list of tuples containing (start, end, replacement)
    """
    new_code = ''
    index = 0
    for start, end, code_string in sorted(replacements):
        new_code += code[index:start] + code_string
        index = end
    new_code += code[index:]
    return new_code