Example #1
0
def __get_code(solutions: pd.DataFrame, index: int, task_index: int,
               canon_tree: ast.AST, anon_tree: ast.AST) -> Code:
    tests_results = __get_column_value(
        solutions, index, consts.CODE_TRACKER_COLUMN.TESTS_RESULTS)
    rate = get_rate(tests_results, task_index)
    log.info(f'Task index is :{task_index}, rate is: {rate}')
    return Code(anon_tree, canon_tree, rate)
Example #2
0
def get_two_vertices(sg: SolutionGraph) -> List[Vertex]:
    source_0 = 'print(\'Hi\')'
    source_1 = 'x = 6\nif x > 5:\n    x = 5\nprint(x)'
    sources = [source_0, source_1]
    rates = [TEST_RESULT.CORRECT_CODE.value] * len(sources)
    return [
        Vertex(sg, code=Code.from_source(s, rates[i]))
        for i, s in enumerate(sources)
    ]
Example #3
0
 def create_user_trees(
         self,
         source_code: str,
         profile: Profile,
         rate: Optional[float] = None) -> Tuple[AnonTree, ast.AST]:
     code_info = CodeInfo(User(profile))
     code = Code.from_source(source_code, rate=rate, task=self._graph.task)
     anon_tree = AnonTree(code.anon_tree, code.rate,
                          self.get_file_path(f'{TREE_TYPE.ANON.value}'),
                          code_info)
     anon_tree.find_medians()
     return anon_tree, code.canon_tree
Example #4
0
def __remove_loops(code_info_chain: List[Tuple[Code, CodeInfo]],
                   user: User) -> List[Tuple[Code, CodeInfo]]:
    # Add empty code
    # We want to remove loops, but we can have the situation: Tree1 -> Tree2 -> Tree3 -> Empty Tree -> Tree5
    # In the case we will get after loops removing: Tree1 -> Tree2 -> Tree3 -> Empty Tree -> Tree5,
    # but it is incorrect, because we expect Empty Tree -> Tree5
    # If we add empty code, we will have: Empty Tree -> Tree1 -> Tree2 -> Tree3 -> Empty Tree -> Tree5
    # and we will get after loops removing: Empty Tree -> Tree5
    code_info_chain = [(Code.from_source(
        '', TEST_RESULT.CORRECT_CODE.value), CodeInfo(user))] + code_info_chain

    current_tree_index = 0
    while current_tree_index < len(code_info_chain):
        current_anon_tree = code_info_chain[current_tree_index][0].anon_tree
        same_tree_indexes = []
        for next_tree_index in range(current_tree_index + 1,
                                     len(code_info_chain)):
            next_anon_tree = code_info_chain[next_tree_index][0].anon_tree
            if are_asts_equal(current_anon_tree, next_anon_tree):
                same_tree_indexes.append(next_tree_index)
        if len(same_tree_indexes) > 0:
            del code_info_chain[current_tree_index:max(same_tree_indexes)]
        current_tree_index += 1
    return code_info_chain
Example #5
0
def get_code_chain_from_sources(start_index: int = 0, end_index: int = len(sources)) -> List[Tuple[Code, CodeInfo]]:
    IdCounter.reset_all()
    return [(Code.from_source(source, TEST_RESULT.CORRECT_CODE.value), code_info)
            for source in sources[start_index: end_index]]
Example #6
0
def get_code_info_chain(sources: List[str]) -> List[Tuple[Code, CodeInfo]]:
    user = User()
    return [(Code.from_source(s,
                              TEST_RESULT.CORRECT_CODE.value), CodeInfo(user))
            for s in sources]
Example #7
0
def __get_code_by_source(source: str, is_goal: bool = False) -> Code:
    anon_tree, canon_tree = get_trees(source, {TREE_TYPE.ANON, TREE_TYPE.CANON})
    rate = 0 if not is_goal else TEST_RESULT.FULL_SOLUTION.value
    return Code(anon_tree=anon_tree, canon_tree=canon_tree, rate=rate)
Example #8
0
def create_code_from_source(source: str,
                            rate: float = TEST_RESULT.CORRECT_CODE.value
                            ) -> Code:
    anon_tree, canon_tree = get_trees(source,
                                      {TREE_TYPE.ANON, TREE_TYPE.CANON})
    return Code(anon_tree, canon_tree, rate)