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

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

项目:opyum    作者:Amper    | 项目源码 | 文件源码
def _is_numeric_pow(self, node: ast.BinOp) -> bool:
        if isinstance(node.op, ast.Pow):
            left, right = node.left, node.right
            if isinstance(left, (ast.Name, ast.Num)):
                if isinstance(right, ast.Num):
                    degree = right.n
                elif isinstance(right, ast.UnaryOp)\
                and  isinstance(right.op, (ast.USub, ast.UAdd))\
                and  isinstance(right.operand, ast.Num):
                    degree = right.operand.n
                else:
                    return False
                if isinstance(degree, float):
                    degree = int(degree) if degree.is_integer() else degree
                return isinstance(degree, int)
        return False
项目: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
项目: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
项目:radar    作者:amoose136    | 项目源码 | 文件源码
def visitUnaryOp(self, node):
        import ast
        if isinstance(node.op, ast.UAdd):
            return +self.visit(node.operand)
        elif isinstance(node.op, ast.USub):
            return -self.visit(node.operand)
        else:
            raise SyntaxError("Unknown unary op: %r" % node.op)
项目:krpcScripts    作者:jwvanderbeck    | 项目源码 | 文件源码
def visitUnaryOp(self, node):
        import ast
        if isinstance(node.op, ast.UAdd):
            return +self.visit(node.operand)
        elif isinstance(node.op, ast.USub):
            return -self.visit(node.operand)
        else:
            raise SyntaxError("Unknown unary op: %r" % node.op)
项目: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')
项目:vizgen    作者:uva-graphics    | 项目源码 | 文件源码
def is_int_constant_py_ast(s):
    """
    py_ast version on is_int_constant in redbaron_util.py
    """
    s = s.strip()
    rootnode = get_ast(s).body
    if len(rootnode) == 1 and isinstance(rootnode[0], ast.Expr):
        node = rootnode[0].value
        if isinstance(node, ast.UnaryOp):
            if isinstance(node.op, (ast.USub, ast.UAdd)):
                return isinstance(node.operand, ast.Num)
        else:
            return isinstance(node, ast.Num)
    return False
项目:PYSL    作者:sparkon    | 项目源码 | 文件源码
def TO_INT(node: ast) -> (bool, int):
    if isinstance(node, ast.Num):
        return (True, node.n)
    if isinstance(node, ast.UnaryOp) and isinstance(node.operand, ast.Num):
        if isinstance(node.op, ast.UAdd):
            return (True, +node.operand.n)
        if isinstance(node.op, ast.USub):
            return (True, -node.operand.n)
        error(loc(node), "Expected +/- Num")
        return (False, 0)
    error(loc(node), "Expected signed integer")
    return (False, 0)
项目: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'
项目:aws-lambda-numpy    作者:vitolimandibhrata    | 项目源码 | 文件源码
def visitUnaryOp(self, node):
        import ast
        if isinstance(node.op, ast.UAdd):
            return +self.visit(node.operand)
        elif isinstance(node.op, ast.USub):
            return -self.visit(node.operand)
        else:
            raise SyntaxError("Unknown unary op: %r" % node.op)
项目:lambda-numba    作者:rlhotovy    | 项目源码 | 文件源码
def visitUnaryOp(self, node):
        import ast
        if isinstance(node.op, ast.UAdd):
            return +self.visit(node.operand)
        elif isinstance(node.op, ast.USub):
            return -self.visit(node.operand)
        else:
            raise SyntaxError("Unknown unary op: %r" % node.op)
项目:deliver    作者:orchestor    | 项目源码 | 文件源码
def visitUnaryOp(self, node):
        import ast
        if isinstance(node.op, ast.UAdd):
            return +self.visit(node.operand)
        elif isinstance(node.op, ast.USub):
            return -self.visit(node.operand)
        else:
            raise SyntaxError("Unknown unary op: %r" % node.op)
项目:mutpy    作者:mutpy    | 项目源码 | 文件源码
def get_operator_type(self):
        return ast.UAdd, ast.USub
项目:mutpy    作者:mutpy    | 项目源码 | 文件源码
def mutate_USub(self, node):
        return ast.UAdd()
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def ana_Unary_Name_constructor(self, ctx, e):
        id = e.operand.id
        if id != "Inf":
            raise _errors.TyError("Invalid ieee literal.", e)
        if not isinstance(e.op, (ast.UAdd, ast.USub)):
            raise _errors.TyError("Invalid unary operator on ieee literal.", e)
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def syn_idx_Unary_Name_constructor(cls, ctx, e, inc_idx):
        id = e.operand.id
        if id != "Inf":
            raise _errors.TyError("Invalid ieee literal.", e)
        if not isinstance(e.op, (ast.UAdd, ast.USub)):
            raise _errors.TyError("Invalid unary operator on ieee literal.", e)
        return ()
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def translate_Unary_Name_constructor(self, ctx, e):
        if isinstance(e.op, ast.UAdd):
            argument = "Inf"
        else:
            argument = "-Inf"
        return astx.builtin_call("float", [ast.Str(s=argument)])
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def ana_pat_Unary_Name_constructor(self, ctx, pat):
        id = pat.operand.id
        if id != "Inf":
            raise _errors.TyError("Invalid ieee literal pattern.", pat)
        if not isinstance(pat.op, (ast.UAdd, ast.USub)):
            raise _errors.TyError(
                "Invalid unary operator on ieee literal pattern.", pat)
        return _util.odict()
项目:Alfred    作者:jkachhadia    | 项目源码 | 文件源码
def visitUnaryOp(self, node):
        import ast
        if isinstance(node.op, ast.UAdd):
            return +self.visit(node.operand)
        elif isinstance(node.op, ast.USub):
            return -self.visit(node.operand)
        else:
            raise SyntaxError("Unknown unary op: %r" % node.op)
项目:flake8-bugbear    作者:PyCQA    | 项目源码 | 文件源码
def visit_UAdd(self, node):
        trailing_nodes = list(map(type, self.node_window[-4:]))
        if trailing_nodes == [ast.UnaryOp, ast.UAdd, ast.UnaryOp, ast.UAdd]:
            originator = self.node_window[-4]
            self.errors.append(
                B002(originator.lineno, originator.col_offset)
            )
        self.generic_visit(node)