Python ast 模块,Add() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用ast.Add()

项目:opyum    作者:Amper    | 项目源码 | 文件源码
def visit_BinOp(self, node):
        node  = self.generic_visit(node)
        left  = node.left
        right = node.right
        if all(isinstance(value, ast.Num) for value in (left, right)):
            if isinstance(node.op, tuple(self._operators.keys())):
                val  = self._operators[type(node.op)](left.n, right.n)
                node = ast.copy_location(ast.Num(n = val), node)
                return node
        elif all(isinstance(value, ast.Str) for value in (left, right)):
           if isinstance(node.op, ast.Add):
                val  = left.s + right.s
                node = ast.copy_location(ast.Str(s = val), node)
                return node
        return self.fold(node)

    #def visit_GeneratorExp(self, node):
    #    return self.comprehension(node)
项目:ITAP-django    作者:krivers    | 项目源码 | 文件源码
def num_negate(op):
    top = type(op)
    neg = not op.num_negated if hasattr(op, "num_negated") else True
    if top == ast.Add:
        newOp = ast.Sub()
    elif top == ast.Sub:
        newOp = ast.Add()
    elif top in [ast.Mult, ast.Div, ast.Mod, ast.Pow, ast.LShift, 
                 ast.RShift, ast.BitOr, ast.BitXor, ast.BitAnd, ast.FloorDiv]:
        return None # can't negate this
    elif top in [ast.Num, ast.Name]:
        # this is a normal value, so put a - in front of it
        newOp = ast.UnaryOp(ast.USub(addedNeg=True), op)
    else:
        log("astTools\tnum_negate\tUnusual type: " + str(top), "bug")
    transferMetaData(op, newOp)
    newOp.num_negated = neg
    return newOp
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:sherlock.py    作者:Luavis    | 项目源码 | 文件源码
def _generate_binop(self, node, ext_info):
    left_type = self.get_type(node.left)
    right_type = self.get_type(node.right)
    extra_code = ext_info.get('extra_code', '')

    if left_type.is_void or right_type.is_void:
        raise CompileError('Void type is not able to operate.')

    if left_type.is_number and right_type.is_number:
        op = self.generate_numeric_op(node.op, ext_info)
        if len(op) is 0:
            raise SyntaxNotSupportError("%s operation is not support yet." % node.op.__class__.__name__)

        left_name, extra_code = get_node_name_with_extra_code(self, node.left, extra_code)
        right_name, extra_code = get_node_name_with_extra_code(self, node.right, extra_code)

        return '$(( %s %s %s ))' % (left_name, op, right_name), extra_code
    elif (left_type.is_string or right_type.is_string) and isinstance(node.op, ast.Add):
        _ext_info = {'extra_code': extra_code}
        left = self.dispatch(node.left, _ext_info)
        right = self.dispatch(node.right, _ext_info)

        return left + right, _ext_info['extra_code']
    else:
        raise SyntaxNotSupportError("%s operation is not support yet." % node.op.__class__.__name__)
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:vizgen    作者:uva-graphics    | 项目源码 | 文件源码
def add_line_info(root_node):
    """
    Add lineno attribute for all nodes at and under root_node, recursively.
    """
    class AddLineNumbers(BottomUpVisitor):
        def __init__(self):
            BottomUpVisitor.__init__(self, strict_line_order=True, make_unique=True)
        def visit_one_node(self, node, lineno=None):
#            print(node, lineno, getattr(node, 'lineno', None))
            if not hasattr(node, 'lineno'):
                node.lineno = lineno
            else:
                if node.lineno != lineno:
                    print(node, lineno, node.lineno)
                    print(astor.dump(root_node))
                    assert False
            BottomUpVisitor.visit_one_node(self, node, lineno)
    AddLineNumbers().visit(root_node)
项目:PYSL    作者:sparkon    | 项目源码 | 文件源码
def eval_numeric_constexpr(node: ast.AST) -> int:
    if isinstance(node, ast.Num):
        return node.n
    if isinstance(node, ast.UnaryOp):
        if isinstance(node.op, ast.UAdd):
            return +eval_numeric_constexpr(node.operand)
        elif isinstance(node.op, ast.USub):
            return -eval_numeric_constexpr(node.operand)
        else:
            return None
    if isinstance(node, ast.BinOp):
        if isinstance(node.op, ast.Add):
            return eval_numeric_constexpr(node.left) + eval_numeric_constexpr(node.right)
        if isinstance(node.op, ast.Sub):
            return eval_numeric_constexpr(node.left) - eval_numeric_constexpr(node.right)
        if isinstance(node.op, ast.Mult):
            return eval_numeric_constexpr(node.left) * eval_numeric_constexpr(node.right)
        if isinstance(node.op, ast.Div):
            return eval_numeric_constexpr(node.left) / eval_numeric_constexpr(node.right)
        return None
项目:PYSL    作者:sparkon    | 项目源码 | 文件源码
def binop_str(op: ast.AST) -> str:
    if isinstance(op, ast.Add):
        return '+'
    if isinstance(op, ast.Sub):
        return '-'
    if isinstance(op, ast.Mult):
        return '*'
    if isinstance(op, ast.Div):
        return '/ '
    if isinstance(op, ast.Mod):
        return '%'
    if isinstance(op, ast.LShift):
        return '<<'
    if isinstance(op, ast.RShift):
        return '>>'
    if isinstance(op, ast.BitOr):
        return '|'
    if isinstance(op, ast.BitXor):
        return '^'
    if isinstance(op, ast.BitAnd):
        return '&'
    if isinstance(op, ast.MatMult):
        return '@'
    error(loc(op), "Invalid binary operator encountered: {0}:{1}. Check supported intrinsics.".format(op.lineno, op.col_offset))
    return 'INVALID_BINOP'
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:bubblesub    作者:rr-    | 项目源码 | 文件源码
def eval_expr(expr):
    import ast
    import operator as op

    op = {
        ast.Add: op.add,
        ast.Sub: op.sub,
        ast.Mult: op.mul,
        ast.Div: op.truediv,
        ast.Pow: op.pow,
        ast.BitXor: op.xor,
        ast.USub: op.neg,
    }

    def eval_(node):
        if isinstance(node, ast.Num):
            return fractions.Fraction(node.n)
        elif isinstance(node, ast.BinOp):
            return op[type(node.op)](eval_(node.left), eval_(node.right))
        elif isinstance(node, ast.UnaryOp):
            return op[type(node.op)](eval_(node.operand))
        raise TypeError(node)

    return eval_(ast.parse(str(expr), mode='eval').body)
项目:monkeys    作者:hchasestevens    | 项目源码 | 文件源码
def visit_Assert(self, node):
        """Replace assertions with augmented assignments."""
        self.max_score += 1
        return ast.AugAssign(
            op=ast.Add(),
            target=ast.Name(
                id=self.score_var_name,
                ctx=ast.Store()
            ),
            value=ast.Call(
                args=[node.test],
                func=ast.Name(
                    id='bool',
                    ctx=ast.Load()
                ),
                keywords=[],
                kwargs=None,
                starargs=None
            )
        )
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_increment_lineno(self):
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src, n=3), src)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
        # issue10869: do not increment lineno of root twice
        src = ast.parse('1 + 1', mode='eval')
        self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
            'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
            'col_offset=0))'
        )
项目:viper    作者:ethereum    | 项目源码 | 文件源码
def aug_assign(self):
        target = self.get_target(self.stmt.target)
        sub = Expr.parse_value_expr(self.stmt.value, self.context)
        if not isinstance(self.stmt.op, (ast.Add, ast.Sub, ast.Mult, ast.Div, ast.Mod)):
            raise Exception("Unsupported operator for augassign")
        if not isinstance(target.typ, BaseType):
            raise TypeMismatchException("Can only use aug-assign operators with simple types!", self.stmt.target)
        if target.location == 'storage':
            o = Expr.parse_value_expr(ast.BinOp(left=LLLnode.from_list(['sload', '_stloc'], typ=target.typ, pos=target.pos),
                                    right=sub, op=self.stmt.op, lineno=self.stmt.lineno, col_offset=self.stmt.col_offset), self.context)
            return LLLnode.from_list(['with', '_stloc', target, ['sstore', '_stloc', base_type_conversion(o, o.typ, target.typ)]], typ=None, pos=getpos(self.stmt))
        elif target.location == 'memory':
            o = Expr.parse_value_expr(ast.BinOp(left=LLLnode.from_list(['mload', '_mloc'], typ=target.typ, pos=target.pos),
                                    right=sub, op=self.stmt.op, lineno=self.stmt.lineno, col_offset=self.stmt.col_offset), self.context)
            return LLLnode.from_list(['with', '_mloc', target, ['mstore', '_mloc', base_type_conversion(o, o.typ, target.typ)]], typ=None, pos=getpos(self.stmt))
项目:meteos    作者:openstack    | 项目源码 | 文件源码
def add_error(self, node, message=None):
        """Add an error caused by a node to the list of errors for pep8."""
        message = message or self.CHECK_DESC
        error = (node.lineno, node.col_offset, message, self.__class__)
        self._errors.append(error)
项目:meteos    作者:openstack    | 项目源码 | 文件源码
def visit_BinOp(self, node):
        if isinstance(node.op, ast.Add):
            if self._check_call_names(node.left, self.TRANS_FUNC):
                self.add_error(node.left)
            elif self._check_call_names(node.right, self.TRANS_FUNC):
                self.add_error(node.right)
        super(CheckForTransAdd, self).generic_visit(node)
项目:ITAP-django    作者:krivers    | 项目源码 | 文件源码
def listNotEmpty(a):
    """Determines that the iterable is NOT empty, if we can know that"""
    """Used for For objects"""
    if not isinstance(a, ast.AST):
        return False
    if type(a) == ast.Call:
        if type(a.func) == ast.Name and a.func.id in ["range"]:
            if len(a.args) == 1: # range(x)
                return type(a.args[0]) == ast.Num and type(a.args[0].n) != complex and a.args[0].n > 0
            elif len(a.args) == 2: # range(start, x)
                if type(a.args[0]) == ast.Num and type(a.args[1]) == ast.Num and \
                    type(a.args[0].n) != complex and type(a.args[1].n) != complex and \
                    a.args[0].n < a.args[1].n:
                    return True
                elif type(a.args[1]) == ast.BinOp and type(a.args[1].op) == ast.Add:
                    if type(a.args[1].right) == ast.Num and type(a.args[1].right) != complex and a.args[1].right.n > 0 and \
                        compareASTs(a.args[0], a.args[1].left, checkEquality=True) == 0:
                        return True
                    elif type(a.args[1].left) == ast.Num and type(a.args[1].left) != complex and a.args[1].left.n > 0 and \
                        compareASTs(a.args[0], a.args[1].right, checkEquality=True) == 0:
                        return True
    elif type(a) in [ast.List, ast.Tuple]:
        return len(a.elts) > 0
    elif type(a) == ast.Str:
        return len(a.s) > 0
    return False
项目:ITAP-django    作者:krivers    | 项目源码 | 文件源码
def doBinaryOp(op, l, r):
    """Perform the given AST binary operation on the values"""
    top = type(op)
    if top == ast.Add:
        return l + r
    elif top == ast.Sub:
        return l - r
    elif top == ast.Mult:
        return l * r
    elif top == ast.Div:
        # Don't bother if this will be a really long float- it won't work properly!
        # Also, in Python 3 this is floating division, so perform it accordingly.
        val = 1.0 * l / r
        if (val * 1e10 % 1.0) != 0:
            raise Exception("Repeating Float")
        return val
    elif top == ast.Mod:
        return l % r
    elif top == ast.Pow:
        return l ** r
    elif top == ast.LShift:
        return l << r
    elif top == ast.RShift:
        return l >> r
    elif top == ast.BitOr:
        return l | r
    elif top == ast.BitXor:
        return l ^ r
    elif top == ast.BitAnd:
        return l & r
    elif top == ast.FloorDiv:
        return l // r
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def visit_BinOp(self, node):
        if node.op.__class__ in self.operators:
            sympy_class = self.operators[node.op.__class__]
            right = self.visit(node.right)

            if isinstance(node.op, ast.Sub):
                right = ast.UnaryOp(op=ast.USub(), operand=right)
            elif isinstance(node.op, ast.Div):
                right = ast.Call(
                    func=ast.Name(id='Pow', ctx=ast.Load()),
                    args=[right, ast.UnaryOp(op=ast.USub(), operand=ast.Num(1))],
                    keywords=[ast.keyword(arg='evaluate', value=ast.Name(id='False', ctx=ast.Load()))],
                    starargs=None,
                    kwargs=None
                )

            new_node = ast.Call(
                func=ast.Name(id=sympy_class, ctx=ast.Load()),
                args=[self.visit(node.left), right],
                keywords=[ast.keyword(arg='evaluate', value=ast.Name(id='False', ctx=ast.Load()))],
                starargs=None,
                kwargs=None
            )

            if sympy_class in ('Add', 'Mul'):
                # Denest Add or Mul as appropriate
                new_node.args = self.flatten(new_node.args, sympy_class)

            return new_node
        return node
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_copy_location(self):
        src = ast.parse('1 + 1', mode='eval')
        src.body.right = ast.copy_location(ast.Num(2), src.body.right)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
            'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
            'col_offset=0))'
        )
项目:sherlock.py    作者:Luavis    | 项目源码 | 文件源码
def generate_aug_assign(self, node, ext_info):
    tmp_name = self.temp_variable.get_new_name()
    tmp_assign_code = '%s=%s' % (
        tmp_name,
        self.dispatch(node.value)
    )
    self.code_buffer.append(tmp_assign_code)
    target = self.dispatch(node.target)
    target_type = self.get_type(node.target)
    if target_type.is_number:
        op = self.generate_numeric_op(node.op, ext_info)
        return '%s=$(( $%s %s $%s ))'% (
            target,
            target,
            op,
            tmp_name
        )
    elif target_type.is_string:
        if isinstance(node.op, ast.Add):
            return '%s=%s$%s'% (
                target,
                target,
                tmp_name
            )
        else:
            raise SyntaxNotSupportError(
                "%s operation is not support yet."
                % node.op.__class__.__name__
            )
项目:sherlock.py    作者:Luavis    | 项目源码 | 文件源码
def get_type(self, node):
        if isinstance(node, ast.BinOp):
            left_type = self.get_type(node.left)
            right_type = self.get_type(node.right)

            if isinstance(node.op, ast.Add):
                if left_type.is_number and right_type.is_number:
                    return Type.NUMBER
                else:
                    return Type.STRING
            elif left_type.is_number and right_type.is_number:
                return Type.NUMBER
            else:
                raise CompileError("Can not '%s' operator with string." % node.op.__class__.__name__)
        elif isinstance(node, ast.UnaryOp):
            if isinstance(operand, ast.Num):
                return Type.NUMBER
            else:
                raise SyntaxNotSupportError("Not support unary operator except number.")

        elif isinstance(node, ast.Num):
            return Type.NUMBER

        elif isinstance(node, ast.Str):
            return Type.STRING
        elif isinstance(node, ast.List):
            return Type.LIST
        elif isinstance(node, ast.Call):
            args_type = [self.get_type(arg) for arg in node.args]
            return self.get_function_return_type(node.func.id, args_type)
        elif isinstance(node, ast.Name):
            return self.variables[node.id].var_type
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_copy_location(self):
        src = ast.parse('1 + 1', mode='eval')
        src.body.right = ast.copy_location(ast.Num(2), src.body.right)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
            'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
            'col_offset=0))'
        )
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_copy_location(self):
        src = ast.parse('1 + 1', mode='eval')
        src.body.right = ast.copy_location(ast.Num(2), src.body.right)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
            'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
            'col_offset=0))'
        )
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_copy_location(self):
        src = ast.parse('1 + 1', mode='eval')
        src.body.right = ast.copy_location(ast.Num(2), src.body.right)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
            'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
            'col_offset=0))'
        )
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_augassign(self):
        aug = ast.AugAssign(ast.Name("x", ast.Load()), ast.Add(),
                            ast.Name("y", ast.Load()))
        self.stmt(aug, "must have Store context")
        aug = ast.AugAssign(ast.Name("x", ast.Store()), ast.Add(),
                            ast.Name("y", ast.Store()))
        self.stmt(aug, "must have Load context")
项目:vizgen    作者:uva-graphics    | 项目源码 | 文件源码
def chosen_typespec_dumps(typeconfig):
    """
    Add a comment about the chosen type specialization that was used for a given function.
    """
    ans = chosen_typespec + repr(typeconfig) + '\n'
    if typeconfig is not None and verbose:
        print('chosen_typespec_dumps:', ans)
        print('chosen_typespec_dumps, checking')
        for value in typeconfig.values():
            print('chosen_typespec_dumps, check', type(value), repr(value))
    #        value.check()
    return ans
项目:vizgen    作者:uva-graphics    | 项目源码 | 文件源码
def add_parent_info(root_node):
    """
    Add parent attribute for all nodes at and under root_node, recursively.
    """
    class AddParents(TopDownVisitor):
        def generic_visit(self, node):
            if not hasattr(node, 'parent'):
                node.parent = None
            for child in ast.iter_child_nodes(node):
                child.parent = node
    AddParents().visit(root_node)
项目:vizgen    作者:uva-graphics    | 项目源码 | 文件源码
def get_binary_op_str(bin_op_node):
    """Returns the string representation of the binary operator node (e.g. +, -,
    etc.). For some reason astor doesn't implement this???
    """

    if isinstance(bin_op_node, ast.Add):
        return "+"

    elif isinstance(bin_op_node, ast.Sub):
        return "-"

    elif isinstance(bin_op_node, ast.Mult):
        return "*"

    elif isinstance(bin_op_node, ast.Div):
        return "/"

    elif isinstance(bin_op_node, ast.Mod):
        return "%"

    elif isinstance(bin_op_node, ast.Pow):
        return "**"

    elif isinstance(bin_op_node, ast.LShift):
        return "<<"

    elif isinstance(bin_op_node, ast.RShift):
        return ">>"

    else:
        raise ValueError("No string defined for binary operator node %s" % \
            bin_op_node.__class__.__name__)
项目:python2c    作者:Kcrong    | 项目源码 | 文件源码
def get_op_string(op_class):
        return {
            ast.Add: '+',
            ast.Sub: '-',
            ast.Div: '/',
            ast.Mult: '*'
        }[op_class.__class__]

    # For expr code
项目:TerpreT    作者:51alg    | 项目源码 | 文件源码
def is_add_function(node):
    return isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add)
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
def test_copy_location(self):
        src = ast.parse('1 + 1', mode='eval')
        src.body.right = ast.copy_location(ast.Num(2), src.body.right)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
            'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
            'col_offset=0))'
        )
项目:pydiatra    作者:jwilk    | 项目源码 | 文件源码
def visit_BinOp(self, node):
        if isinstance(node.op, ast.BitOr):
            op = operator.or_
        elif isinstance(node.op, ast.Add):
            op = operator.add
        else:
            raise BadConst
        values = [
            self.visit(node.left),
            self.visit(node.right),
        ]
        if all(isinstance(v, RegexpFlag) for v in values):
            return RegexpFlag(op(*values))
        else:
            raise BadConst
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_copy_location(self):
        src = ast.parse('1 + 1', mode='eval')
        src.body.right = ast.copy_location(ast.Num(2), src.body.right)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
            'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
            'col_offset=0))'
        )
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_augassign(self):
        aug = ast.AugAssign(ast.Name("x", ast.Load()), ast.Add(),
                            ast.Name("y", ast.Load()))
        self.stmt(aug, "must have Store context")
        aug = ast.AugAssign(ast.Name("x", ast.Store()), ast.Add(),
                            ast.Name("y", ast.Store()))
        self.stmt(aug, "must have Load context")
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
def test_copy_location(self):
        src = ast.parse('1 + 1', mode='eval')
        src.body.right = ast.copy_location(ast.Num(2), src.body.right)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
            'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
            'col_offset=0))'
        )
项目:Typpete    作者:caterinaurban    | 项目源码 | 文件源码
def binary_operation_type(left_type, op, right_type, lineno, solver):
    """Infer the type of a binary operation result"""
    if isinstance(op, ast.Add):
        inference_func = _infer_add
    elif isinstance(op, ast.Mult):
        inference_func = _infer_mult
    elif isinstance(op, ast.Div):
        inference_func = _infer_div
    elif isinstance(op, (ast.BitOr, ast.BitXor, ast.BitAnd)):
        return _infer_bitwise(left_type, right_type, op, lineno, solver)
    else:
        return _infer_arithmetic(left_type, right_type, op, lineno, solver)

    return inference_func(left_type, right_type, lineno, solver)
项目:mutpy    作者:mutpy    | 项目源码 | 文件源码
def mutate_Sub(self, node):
        if self.should_mutate(node):
            return ast.Add()
        raise MutationResign()
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_copy_location(self):
        src = ast.parse('1 + 1', mode='eval')
        src.body.right = ast.copy_location(ast.Num(2), src.body.right)
        self.assertEqual(ast.dump(src, include_attributes=True),
            'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
            'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
            'col_offset=0))'
        )
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_augassign(self):
        aug = ast.AugAssign(ast.Name("x", ast.Load()), ast.Add(),
                            ast.Name("y", ast.Load()))
        self.stmt(aug, "must have Store context")
        aug = ast.AugAssign(ast.Name("x", ast.Store()), ast.Add(),
                            ast.Name("y", ast.Store()))
        self.stmt(aug, "must have Load context")
项目:femtocode    作者:diana-hep    | 项目源码 | 文件源码
def pythonast(self, args, tonative=False):
        return reduce(lambda x, y: ast.BinOp(x, ast.Add(), y), args)
项目:Trusted-Platform-Module-nova    作者:BU-NU-CLOUD-SP16    | 项目源码 | 文件源码
def add_error(self, node, message=None):
        """Add an error caused by a node to the list of errors for pep8."""
        message = message or self.CHECK_DESC
        error = (node.lineno, node.col_offset, message, self.__class__)
        self._errors.append(error)
项目:Trusted-Platform-Module-nova    作者:BU-NU-CLOUD-SP16    | 项目源码 | 文件源码
def visit_BinOp(self, node):
        if isinstance(node.op, ast.Add):
            if self._check_call_names(node.left, self.TRANS_FUNC):
                self.add_error(node.left)
            elif self._check_call_names(node.right, self.TRANS_FUNC):
                self.add_error(node.right)
        super(CheckForTransAdd, self).generic_visit(node)
项目:fastpy    作者:tartavull    | 项目源码 | 文件源码
def visit_AugAssign(self, node):
        if isinstance(node.op, ast.Add):
            ref = node.target.id
            value = self.visit(node.value)
            return Assign(ref, Prim("add#", [Var(ref), value]))
        if isinstance(node.op, ast.Mul):
            ref = node.target.id
            value = self.visit(node.value)
            return Assign(ref, Prim("mult#", [Var(ref), value]))
        else:
            raise NotImplementedError
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def syn_BinOp(self, ctx, e):
        op = e.op
        if isinstance(op, ast.Add):
            ctx.ana(e.right, self)
            return self
        else:
            raise _errors.TyError("Invalid binary operator on strings.", e)
项目:googletranslate.popclipext    作者:wizyoung    | 项目源码 | 文件源码
def _update(self):
        """update tkk
        """
        # we don't need to update the base TKK value when it is still valid
        now = math.floor(int(time.time() * 1000) / 3600000.0)
        if self.tkk and int(self.tkk.split('.')[0]) == now:
            return

        r = self.session.get(self.host)
        # this will be the same as python code after stripping out a reserved word 'var'
        code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
        # unescape special ascii characters such like a \x3d(=)
        if PY3:  # pragma: no cover
            code = code.encode().decode('unicode-escape')
        else:  # pragma: no cover
            code = code.decode('string_escape')

        if code:
            tree = ast.parse(code)
            visit_return = False
            operator = '+'
            n, keys = 0, dict(a=0, b=0)
            for node in ast.walk(tree):
                if isinstance(node, ast.Assign):
                    name = node.targets[0].id
                    if name in keys:
                        if isinstance(node.value, ast.Num):
                            keys[name] = node.value.n
                        # the value can sometimes be negative
                        elif isinstance(node.value, ast.UnaryOp) and \
                                isinstance(node.value.op, ast.USub):  # pragma: nocover
                            keys[name] = -node.value.operand.n
                elif isinstance(node, ast.Return):
                    # parameters should be set after this point
                    visit_return = True
                elif visit_return and isinstance(node, ast.Num):
                    n = node.n
                elif visit_return and n > 0:
                    # the default operator is '+' but implement some more for
                    # all possible scenarios
                    if isinstance(node, ast.Add):  # pragma: nocover
                        pass
                    elif isinstance(node, ast.Sub):  # pragma: nocover
                        operator = '-'
                    elif isinstance(node, ast.Mult):  # pragma: nocover
                        operator = '*'
                    elif isinstance(node, ast.Pow):  # pragma: nocover
                        operator = '**'
                    elif isinstance(node, ast.BitXor):  # pragma: nocover
                        operator = '^'
            # a safety way to avoid Exceptions
            clause = compile('{1}{0}{2}'.format(
                operator, keys['a'], keys['b']), '', 'eval')
            value = eval(clause, dict(__builtin__={}))
            result = '{}.{}'.format(n, value)

            self.tkk = result
项目:viper    作者:ethereum    | 项目源码 | 文件源码
def parse_for(self):
        from .parser import (
            parse_body,
        )
        # Type 0 for, eg. for i in list(): ...
        if self._is_list_iter():
            return self.parse_for_list()

        if not isinstance(self.stmt.iter, ast.Call) or \
            not isinstance(self.stmt.iter.func, ast.Name) or \
                not isinstance(self.stmt.target, ast.Name) or \
                    self.stmt.iter.func.id != "range" or \
                        len(self.stmt.iter.args) not in (1, 2):
            raise StructureException("For statements must be of the form `for i in range(rounds): ..` or `for i in range(start, start + rounds): ..`", self.stmt.iter)  # noqa

        # Type 1 for, eg. for i in range(10): ...
        if len(self.stmt.iter.args) == 1:
            if not isinstance(self.stmt.iter.args[0], ast.Num):
                raise StructureException("Range only accepts literal values", self.stmt.iter)
            start = LLLnode.from_list(0, typ='num', pos=getpos(self.stmt))
            rounds = self.stmt.iter.args[0].n
        elif isinstance(self.stmt.iter.args[0], ast.Num) and isinstance(self.stmt.iter.args[1], ast.Num):
            # Type 2 for, eg. for i in range(100, 110): ...
            start = LLLnode.from_list(self.stmt.iter.args[0].n, typ='num', pos=getpos(self.stmt))
            rounds = LLLnode.from_list(self.stmt.iter.args[1].n - self.stmt.iter.args[0].n, typ='num', pos=getpos(self.stmt))
        else:
            # Type 3 for, eg. for i in range(x, x + 10): ...
            if not isinstance(self.stmt.iter.args[1], ast.BinOp) or not isinstance(self.stmt.iter.args[1].op, ast.Add):
                raise StructureException("Two-arg for statements must be of the form `for i in range(start, start + rounds): ...`",
                                            self.stmt.iter.args[1])
            if ast.dump(self.stmt.iter.args[0]) != ast.dump(self.stmt.iter.args[1].left):
                raise StructureException("Two-arg for statements of the form `for i in range(x, x + y): ...` must have x identical in both places: %r %r" % (ast.dump(self.stmt.iter.args[0]), ast.dump(self.stmt.iter.args[1].left)), self.stmt.iter)
            if not isinstance(self.stmt.iter.args[1].right, ast.Num):
                raise StructureException("Range only accepts literal values", self.stmt.iter.args[1])
            start = Expr.parse_value_expr(self.stmt.iter.args[0], self.context)
            rounds = self.stmt.iter.args[1].right.n
        varname = self.stmt.target.id
        pos = self.context.new_variable(varname, BaseType('num'))
        self.context.forvars[varname] = True
        o = LLLnode.from_list(['repeat', pos, start, rounds, parse_body(self.stmt.body, self.context)], typ=None, pos=getpos(self.stmt))
        del self.context.vars[varname]
        del self.context.forvars[varname]
        return o