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

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

项目:fatoptimizer    作者:vstinner    | 项目源码 | 文件源码
def visit_UnaryOp(self, node):
        if not self.config.constant_folding:
            return

        eval_unaryop = EVAL_UNARYOP.get(node.op.__class__)
        if eval_unaryop is None:
            return

        if isinstance(node.op, ast.Invert):
            types = int
        else:
            types =  COMPLEX_TYPES

        value = get_constant(node.operand, types=types)
        if value is not UNSET:
            result = eval_unaryop(value)
            return self.new_constant(node, result)

        if (isinstance(node.op, ast.Not)
        and isinstance(node.operand, ast.Compare)):
            new_node = self.not_compare(node)
            if new_node is not None:
                return new_node
项目:Typpete    作者:caterinaurban    | 项目源码 | 文件源码
def infer_unary_operation(node, context, solver):
    """Infer the type for unary operations

    Examples: -5, not 1, ~2
    """
    unary_type = infer(node.operand, context, solver)
    if isinstance(node.op, ast.Not):  # (not expr) always gives bool type
        return solver.z3_types.bool

    if isinstance(node.op, ast.Invert):
        solver.add(axioms.unary_invert(unary_type, solver.z3_types),
                   fail_message="Invert operation in line {}".format(node.lineno))
        return solver.z3_types.int
    else:
        result_type = solver.new_z3_const("unary_result")
        solver.add(axioms.unary_other(unary_type, result_type, solver.z3_types),
                   fail_message="Unary operation in line {}".format(node.lineno))
        return result_type
项目:ITAP-django    作者:krivers    | 项目源码 | 文件源码
def doUnaryOp(op, val):
    """Perform the given AST unary operation on the value"""
    top = type(op)
    if top == ast.Invert:
        return ~ val
    elif top == ast.Not:
        return not val
    elif top == ast.UAdd:
        return val
    elif top == ast.USub:
        return -val
项目:ml-utils    作者:LinxiFan    | 项目源码 | 文件源码
def visit_UnaryOp(self, node):
        op = node.op
        self.generic_visit(node)
        if not isinstance(op, (ast.Invert, ast.UAdd)):
            return node
        op = '+' if isinstance(op, ast.UAdd) else '~'
        return self.make_Call('_shell_eval_lines',
                              [node.operand,
                               self.make_Call('locals'),
                               ast.Str(op),
                               self.make_Name(self.BASH_REPL_VAR)])
项目:ml-utils    作者:LinxiFan    | 项目源码 | 文件源码
def visit_UnaryOp(self, node):
        op = node.op
        self.generic_visit(node)
        if not isinstance(op, (ast.Invert, ast.UAdd)):
            return node
        op = '+' if isinstance(op, ast.UAdd) else '~'
        return self.make_Call('_shell_eval_lines',
                              [node.operand,
                               self.make_Call('locals'),
                               ast.Str(op),
                               self.make_Name(self.BASH_REPL_VAR)])
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda    作者:SignalMedia    | 项目源码 | 文件源码
def visit_UnaryOp(self, node, **kwargs):
        if isinstance(node.op, (ast.Not, ast.Invert)):
            return UnaryOp('~', self.visit(node.operand))
        elif isinstance(node.op, ast.USub):
            return self.const_type(-self.visit(node.operand).value, self.env)
        elif isinstance(node.op, ast.UAdd):
            raise NotImplementedError('Unary addition not supported')
项目:PYSL    作者:sparkon    | 项目源码 | 文件源码
def unop_str(op: ast.AST) -> str:
    if isinstance(op, ast.UAdd):
        return '+'
    if isinstance(op, ast.USub):
        return '-'
    if isinstance(op, ast.Not):
        return '!'
    if isinstance(op, ast.Invert):
        return '~'
    error(loc(op), "Invalid unary operator encountered: {0}:{1}. Check supported intrinsics.".format(op.lineno, op.col_offset))
    return 'INVALID_UNOP'
项目:TerpreT    作者:51alg    | 项目源码 | 文件源码
def visit_UnaryOp(self, node):
        term = self.visit(node.operand)
        if self.__is_bool(term):
            if isinstance(node.op, ast.Not):
                return Not(term)
            elif isinstance(node.op, ast.Invert):
                return Not(term)
            else:
                raise Exception("Unsupported bool unary operation %s" % unparse(node))

        if DATA_TYPE == "int":
            if isinstance(node.op, ast.USub):
                return -term
            elif isinstance(node.op, ast.Not):
                if is_is_int(term):
                    term = term == IntVal(1)
                return Not(term)
            else:
                raise Exception("Unsupported integer unary operation %s" % unparse(node))
        elif DATA_TYPE.startswith("bit_"):
            if isinstance(node.op, ast.Not):
                return ~term
            elif isinstance(node.op, ast.Invert):
                return ~term
            else:
                raise Exception("Unsupported bitvector unary operation %s" % unparse(node))
        else:
            raise Exception("Unsupported unary operation %s" % unparse(node))
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def syn_UnaryOp(self, ctx, e):
        if isinstance(e.op, (ast.Not, ast.Invert)):
            raise _errors.TyError("Invalid unary operator for operand of type ieee.", e)
        else:
            return self
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def syn_UnaryOp(self, ctx, e):
        if not isinstance(e.op, (ast.Not, ast.Invert)):
            return self
        else:
            raise _errors.TyError("Invalid unary operator for operand of type cplx.", e)