示例#1
0
def return_solution(unittest, f, conditions):
    """
    Solves the riddle, Writes it and tests it.
    :param unittest: the unittest object that is passed to test stuff
    :param f: any function object.
    :param conditions: condition or object or partial truth table (explicit, implicit or mix).
    :return: True for successful operation, False if not.
    """
    f_path = h.get_function_path(f)
    file_code = h.read_file(f_path)
    f_line = h.get_function_line_number(f, file_code)

    # enters only if the function source code was found.
    if f_line > 0 and get_signature(file_code[f_line]):

        definition = file_code[f_line]
        function_inputs = h.get_function_inputs(f)

        # init variables
        implementation = get_initial_implementation(definition)
        processed_conditions = get_processed_conditions(conditions, function_inputs)

        for the_output, table in processed_conditions.tables.iteritems():

            all_inputs = get_input_values(conditions, function_inputs, the_output)
            expression = get_function_expression(table, all_inputs)

            if len(expression) > 0:
                implementation = add_code_to_implementation(current_implementation=implementation,
                                                            bool_expression=expression,
                                                            definition=definition,
                                                            the_output=the_output)

        implementation = add_default_return(definition, processed_conditions, implementation)
        solution = Solution(implementation=implementation,
                            function=f,
                            conditions=conditions,
                            processed_conditions=processed_conditions)
        test_implementation(unittest, solution)

        alter_file(f_line, file_code, implementation, f_path)
        print "Solved and tested " + f.__name__
        return solution

    return get_empty_solution(f, conditions)