Пример #1
0
    def valid_arguments(function, arguments):
        """
        Returns boolean indicating if all arguments are supplied.
        :param function: object.
        :param arguments: dict with arguments.
        :return: Boolean.
        """
        # TODO: deal with optional arguments.
        for var in helpers.get_function_inputs(function):
            if var not in arguments:
                return False

        return True
    def get_function_code(self, signature, expression_expected, table, fun):
        """
        Tests that a right function definition is generated.
        :param signature: of the function eg: sum(a,b).
        :param table: truth table.
        """
        expected_code = ["def " + signature + ":", "    return " + expression_expected]

        inputs = get_function_inputs(fun)
        expression = s.get_function_expression(table, inputs)
        definition = 'def ' + signature
        code = s.add_code_to_implementation(current_implementation=s.get_initial_implementation(definition),
                                            bool_expression=expression,
                                            definition=definition,
                                            the_output=True)
        self.assertListEqual(code, expected_code)
Пример #3
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)
Пример #4
0
def print_invoked_function(output):
    """
    Prints a function that is invoked with arguments: eg: fun(a, b)
    :param output: a c.Output object
    :return: string
    """
    if isinstance(output, c.Output):
        args_dict = output.arguments
        args_str = ''
        for var in helpers.get_function_inputs(output.function):
            if args_str == '':
                args_str += print_object(args_dict[var])
            else:
                args_str += ', ' + print_object(args_dict[var])

        return output.function.__name__ + '(' + args_str + ')'
    else:
        warnings.warn('method is not receiving correct data type', UserWarning)