Example #1
0
    def test_rewrite_nested_if_copy_subpart_following_to_nested_body_structural_check(self):
        def update(self, dt):
            y = 1
            x = 22
            if x < 30:
                if y < 50:
                    y = 50
                else:
                    y -= 15

                x += 22
                return x
            else:
                if y < 50:
                    y = 44
                    return y
                else:
                    y -= 15
                    return y + 100.5

        up_ast = SH.get_ast_from_function_definition(update)
        SH.RewriteIfElse().walk(up_ast)
        SH.add_parent_info(up_ast)

        ifnode = up_ast.body[2]
        then, orelse = ifnode.body, ifnode.orelse
        assert len(then) == 1, "then-branch has correct number of statements"
        assert len(orelse) == 1, "else-branch has correct number of statements"

        assert len(then[0].body) == 3, "then-then-branch has correct number of statements"
        assert len(then[0].orelse) == 3, "then-then-branch has correct number of statements"
        assert len(orelse[0].body) == 2, "else-then-branch has correct number of statements"
        assert len(orelse[0].orelse) == 2, "then-then-branch has correct number of statements"
Example #2
0
    def test_rewrite_single_if_copy_following_to_body_structural_check(self):
        def update(self, dt):
            y = 1
            x = 22
            if x < 30:
                y = 50
            else:
                return 100.5
            y += 3
            return y * 4
        up_ast = SH.get_ast_from_function_definition(update)
        SH.RewriteIfElse().walk(up_ast)
        SH.add_parent_info(up_ast)

        ifnode = up_ast.body[2]
        then, orelse = ifnode.body, ifnode.orelse
        assert len(then) == 3, "then-branch has correct number of statements"
        assert len(orelse) == 1, "else-branch has correct number of statements"
        assert isinstance(then[1], ast.AugAssign), "AugAssign has not been copied to then-branch"
        assert isinstance(then[2], ast.Return), "Return has not been copied to then-branch"
        assert isinstance(orelse[0], ast.Return), "else-branch statement is a return (just as the original)"
Example #3
0
def gen_Update(obj, name="", parent=None, **kwargs):
    returnlist = []
    color = get_color(id(obj)) if kwargs["color_updates"] else "black"
    label = ""
    if kwargs["update_labels"]:
        # print("There's an issue with the display of update-labels. Waiting for astor 0.6 to be available...")
        # """ deactivate until astor 0.6 is available"""
        func_ast = SH.get_ast_from_function_definition(obj.function)
        label = astor.to_source(func_ast)
    returnlist.append(
        f"{id(obj.state)} -> {id(obj.target)} [style=\"dashed\" color=\"{color}\" label=\"{label}\"]"
    )

    accessed = SH.get_accessed_ports(obj.function, obj)
    for acc in accessed:
        style = "dashed" if kwargs["show_update_ports"] else "invis"
        returnlist.append(
            f"{id(acc)} -> {id(obj.target)} [style=\"{style}\" color=\"{color}\" ]"
        )

    return returnlist