我们从Python开源项目中,提取了以下22个代码示例,用于说明如何使用ast.UAdd()。
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
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
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
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)
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)])
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')
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
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)
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'
def get_operator_type(self): return ast.UAdd, ast.USub
def mutate_USub(self, node): return ast.UAdd()
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)
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 ()
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)])
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()
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)