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

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

项目: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
项目: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'
项目:Packages    作者:Keypirinha    | 项目源码 | 文件源码
def __init__(self):
        super().__init__()

        # add support for bitwise operators
        if ast.LShift not in self.MATH_OPERATORS: # '<<'
            self.MATH_OPERATORS[ast.LShift] = simpleeval.op.lshift
        if ast.RShift not in self.MATH_OPERATORS: # '>>'
            self.MATH_OPERATORS[ast.RShift] = simpleeval.op.rshift
        if ast.BitOr not in self.MATH_OPERATORS: # '|'
            self.MATH_OPERATORS[ast.BitOr] = simpleeval.op.or_
        if ast.BitXor not in self.MATH_OPERATORS: # '^'
            self.MATH_OPERATORS[ast.BitXor] = simpleeval.op.xor
        if ast.BitAnd not in self.MATH_OPERATORS: # '&'
            self.MATH_OPERATORS[ast.BitAnd] = simpleeval.op.and_

        # add support for extra operators
        #if ast.Not not in self.MATH_OPERATORS: # not ('not')
        #    self.MATH_OPERATORS[ast.Not] = simpleeval.op.not_
        if ast.FloorDiv not in self.MATH_OPERATORS: # floordiv ('//')
            self.MATH_OPERATORS[ast.FloorDiv] = simpleeval.op.floordiv
项目: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
项目:ml-utils    作者:LinxiFan    | 项目源码 | 文件源码
def __init__(self, range_op=ast.BitXor):
        assert range_op in [ast.BitXor, ast.BitAnd, ast.BitOr, ast.MatMult]
        self.range_op = range_op
项目:redbiom    作者:biocore    | 项目源码 | 文件源码
def BitOr():
    return operator.or_
项目: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
项目:Typpete    作者:caterinaurban    | 项目源码 | 文件源码
def _infer_bitwise(left_type, right_type, op, lineno, solver):
    """Infer the type of a bitwise operation, and add the corresponding axioms"""
    result_type = solver.new_z3_const("bitwise_result")

    magic_method = ""
    if isinstance(op, ast.BitOr):
        magic_method = "__or__"
    elif isinstance(op, ast.BitXor):
        magic_method = "__xor__"
    elif isinstance(op, ast.BitAnd):
        magic_method = "__and__"

    solver.add(axioms.bitwise(left_type, right_type, result_type, magic_method, solver.z3_types),
               fail_message="Bitwise operation in line {}".format(lineno))
    return result_type
项目: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_BitAnd(self, node):
        return ast.BitOr()
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def syn_BinOp(self, ctx, e):
        if isinstance(e.op, (ast.LShift, ast.RShift, ast.BitOr, ast.BitXor, ast.BitAnd)):
            raise _errors.TyError("Cannot use bitwise operators on ieee values.", e)
        ctx.ana(e.right, self)
        return self
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def syn_BinOp(self, ctx, e):
        if isinstance(e.op, (ast.LShift, ast.RShift, ast.BitOr, ast.BitXor, ast.BitAnd)):
            raise _errors.TyError("Cannot use bitwise operators on cplx values.", e)
        if isinstance(e.op, ast.Mod):
            raise _errors.TyError("Cannot take the modulus of a complex number.", e)

        right = e.right
        ctx.ana(right, self)

        return self
项目:TerpreT    作者:51alg    | 项目源码 | 文件源码
def visit_BinOp(self, node):
        left_term = self.visit(node.left)
        right_term = self.visit(node.right)

        if self.__is_bool(left_term) and self.__is_bool(right_term):
            if isinstance(node.op, ast.BitAnd):
                return And(left_term, right_term)
            elif isinstance(node.op, ast.BitOr):
                return Or(left_term, right_term)
            elif isinstance(node.op, ast.BitXor):
                return Xor(left_term, right_term)
            else:
                raise Exception("Unsupported bool binary operation %s" % unparse(node))

        if DATA_TYPE == "int":
            if isinstance(node.op, ast.Mod):
                return left_term % right_term
            elif isinstance(node.op, ast.Add):
                return left_term + right_term
            elif isinstance(node.op, ast.Sub):
                return left_term - right_term
            elif isinstance(node.op, ast.Mult):
                return left_term * right_term
            elif isinstance(node.op, ast.BitXor):
                # Special-case for bool circuit-examples:
                if is_is_int(left_term):
                    left_term = left_term == IntVal(1)
                if is_is_int(right_term):
                    right_term = right_term == IntVal(1)
                return left_term != right_term
            else:
                raise Exception("Unsupported integer binary operation %s" % unparse(node))
        elif DATA_TYPE.startswith("bit_"):
            if isinstance(node.op, ast.BitAnd):
                return left_term & right_term
            elif isinstance(node.op, ast.BitOr):
                return left_term | right_term
            elif isinstance(node.op, ast.BitXor):
                return left_term ^ right_term
            else:
                raise Exception("Unsupported bitvector operation %s" % unparse(node))
        else:
            raise Exception("Unsupported data type %s" % DATA_TYPE)
项目:redbiom    作者:biocore    | 项目源码 | 文件源码
def seteval(str_, get=None, stemmer=None, target=None):
    """Evaluate a set operation string, where each Name is fetched

    Parameters
    ----------
    str_ : str
        The query to evaluate
    get : function, optional
        A getting method, defaults to instatiating one from _requests
    stemmer : function, optional
        A method to stem a query Name. If None, defaults to passthrough.
    target : str, optional
        A subcontext to query against. If None, defaults to text-search.
    """
    if get is None:
        import redbiom
        config = redbiom.get_config()
        get = redbiom._requests.make_get(config)

    if stemmer is None:
        stemmer = passthrough

    if target is None:
        target = 'text-search'

    # Load is subject to indirection to simplify testing
    globals()['Load'] = make_Load(get)

    # this seems right now to be the easiest way to inject parameters
    # into Name
    globals()['stemmer'] = stemmer
    globals()['target'] = target

    formed = ast.parse(str_, mode='eval')

    node_types = (ast.BitAnd, ast.BitOr, ast.BitXor, ast.Name, ast.Sub,
                  ast.Expression, ast.BinOp, ast.Load)

    for node in ast.walk(formed):
        if not isinstance(node, node_types):
            raise TypeError("Unsupported node type: %s" % ast.dump(node))

    result = eval(ast.dump(formed))

    # clean up
    global Load
    del Load
    del stemmer
    del target

    return result