예제 #1
0
 def test_fix_missing_locations(self):
     node = gast.Num(n=6)
     tree = gast.UnaryOp(gast.USub(), node)
     tree.lineno = 1
     tree.col_offset = 2
     gast.fix_missing_locations(tree)
     self.assertEqual(node.lineno, tree.lineno)
     self.assertEqual(node.col_offset, tree.col_offset)
예제 #2
0
 def test_fix_missing_locations(self):
     node = gast.Constant(value=6, kind=None)
     tree = gast.UnaryOp(gast.USub(), node)
     tree.lineno = 1
     tree.col_offset = 2
     gast.fix_missing_locations(tree)
     self.assertEqual(node.lineno, tree.lineno)
     self.assertEqual(node.col_offset, tree.col_offset)
예제 #3
0
    def test_NodeTransformer(self):
        node = gast.Constant(value=6, kind=None)
        tree = gast.UnaryOp(gast.USub(), node)

        class Trans(gast.NodeTransformer):
            def visit_Constant(self, node):
                node.value *= 2
                return node

        tree = Trans().visit(tree)

        self.assertEqual(node.value, 12)
예제 #4
0
    def visit_BinOp(self, node):
        self.generic_visit(node)
        left_val = node.left
        right_val = node.right
        left_is_num = isinstance(left_val, gast.Num)
        right_is_num = isinstance(right_val, gast.Num)

        if isinstance(node.op, gast.Mult):
            if left_is_num and right_is_num:
                return gast.Num(left_val.n * right_val.n)
            if left_is_num:
                if left_val.n == 0:
                    return gast.Num(0)
                elif left_val.n == 1:
                    return right_val
            if right_is_num:
                if right_val.n == 0:
                    return gast.Num(0)
                elif right_val.n == 1:
                    return left_val
        elif isinstance(node.op, gast.Add):
            if left_is_num and right_is_num:
                return gast.Num(left_val.n + right_val.n)
            if left_is_num and left_val.n == 0:
                return right_val
            if right_is_num and right_val.n == 0:
                return left_val
        elif isinstance(node.op, gast.Sub):
            if left_is_num and right_is_num:
                return gast.Num(left_val.n - right_val.n)
            if left_is_num and left_val.n == 0:
                return gast.UnaryOp(op=gast.USub(), operand=right_val)
            if right_is_num and right_val.n == 0:
                return left_val
        elif isinstance(node.op, gast.Div):
            if left_is_num and right_is_num:
                return gast.Num(left_val.n / right_val.n)
            if right_is_num and right_val.n == 1:
                return left_val
        elif isinstance(node.op, gast.Pow):
            if left_is_num and right_is_num:
                return gast.Num(left_val.n**right_val.n)
            if left_is_num:
                if left_val.n == 0:
                    return gast.Num(0)
                elif left_val.n == 1:
                    return gast.Num(1)
            if right_is_num:
                if right_val.n == 0:
                    return gast.Num(1)
                elif right_val.n == 1:
                    return left_val
        return node
예제 #5
0
    def test_NodeTransformer(self):
        node = gast.Num(n=6)
        tree = gast.UnaryOp(gast.USub(), node)

        class Trans(gast.NodeTransformer):

            def visit_Num(self, node):
                node.n *= 2
                return node

        tree = Trans().visit(tree)

        self.assertEqual(node.n, 12)
예제 #6
0
    def test_NodeVisitor(self):
        node = gast.Constant(value=6, kind=None)
        tree = gast.UnaryOp(gast.USub(), node)

        class Vis(gast.NodeTransformer):
            def __init__(self):
                self.state = []

            def visit_Constant(self, node):
                self.state.append(node.value)

        vis = Vis()
        vis.visit(tree)

        self.assertEqual(vis.state, [6])
예제 #7
0
 def _build_print_call_node(self, node):
     return gast.Call(
         func=gast.parse('fluid.layers.Print').body[0].value,
         args=[node],
         keywords=[
             gast.keyword(
                 arg='summarize',
                 value=gast.UnaryOp(
                     op=gast.USub(),
                     operand=gast.Constant(
                         value=1, kind=None))), gast.keyword(
                             arg='print_phase',
                             value=gast.Constant(
                                 value='forward', kind=None))
         ])
예제 #8
0
    def test_NodeVisitor(self):
        node = gast.Num(n=6)
        tree = gast.UnaryOp(gast.USub(), node)

        class Vis(gast.NodeTransformer):

            def __init__(self):
                self.state = []

            def visit_Num(self, node):
                self.state.append(node.n)

        vis = Vis()
        vis.visit(tree)

        self.assertEqual(vis.state, [6])
예제 #9
0
 def test_iter_child_nodes(self):
     tree = gast.UnaryOp(gast.USub(), gast.Constant(value=1, kind=None))
     self.assertEqual(len(list(gast.iter_fields(tree))), 2)
예제 #10
0
 def test_iter_child_nodes(self):
     tree = gast.UnaryOp(gast.USub(), gast.Num(n=1))
     self.assertEqual(len(list(gast.iter_fields(tree))),
                      2)