當前位置: 首頁>>代碼示例>>Python>>正文


Python ast.UAdd方法代碼示例

本文整理匯總了Python中ast.UAdd方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.UAdd方法的具體用法?Python ast.UAdd怎麽用?Python ast.UAdd使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在ast的用法示例。


在下文中一共展示了ast.UAdd方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: visit_UnaryOp

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import UAdd [as 別名]
def visit_UnaryOp(self, node):
        self.visit(node.operand)
        op = node.op
        node.obj = node.operand.obj
        if isinstance(op, ast.Not):
            node.obj = bool()
        elif isinstance(op, ast.UAdd):
            node.obj = int(-1)
        elif isinstance(op, ast.USub):
            node.obj = int(-1) 
開發者ID:myhdl,項目名稱:myhdl,代碼行數:12,代碼來源:_analyze.py

示例2: factor_rewrite

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import UAdd [as 別名]
def factor_rewrite(mark: Tokenizer, factor, power):
    return power if power else ast.UnaryOp(
        **(loc @ mark),
        op={
            '~': ast.Invert,
            '+': ast.UAdd,
            '-': ast.USub
        }[mark.value](),
        operand=factor,
    ) 
開發者ID:Xython,項目名稱:YAPyPy,代碼行數:12,代碼來源:helper.py

示例3: visitUnaryOp

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import UAdd [as 別名]
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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:10,代碼來源:utils.py

示例4: visit_UnaryOp

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import UAdd [as 別名]
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') 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:9,代碼來源:pytables.py

示例5: visit_UnaryOp

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import UAdd [as 別名]
def visit_UnaryOp(self, node):
        'Change USub and Invert'
        operand = self.visit(node.operand)
        if isinstance(node.op, ast.UAdd):
            return operand
        if isinstance(node.op, ast.USub):
            return ast.BinOp(ast.Num(-1), ast.Mult(), operand)
        if isinstance(node.op, ast.Invert):
            return ast.BinOp(ast.Num(-1), ast.BitXor(), operand)
        assert False, 'unhandled node type: ' + ast.dump(node) 
開發者ID:quarkslab,項目名稱:sspam,代碼行數:12,代碼來源:cse.py

示例6: visitUnaryOp

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import UAdd [as 別名]
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) 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:10,代碼來源:utils.py

示例7: __init__

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import UAdd [as 別名]
def __init__( s, component ):
    super().__init__( component )
    s.loop_var_env = set()
    s.tmp_var_env = set()

    # opmap maps an ast operator to its RTLIR counterpart.
    s.opmap = {
      # Bool operators
      # Note: we do not support boolean operators because Python does
      # not allow overloading And and Or operators. Using them in
      # expressions might lead to inconsistent semantics.
      # ast.And    : bir.And(),       ast.Or     : bir.Or(),
      # Unary operators
      # Note: ast.Not is disallowed because it is a boolean operator
      # ast.Not    : bir.Not(),
      ast.Invert : bir.Invert(),
      ast.UAdd   : bir.UAdd(),      ast.USub   : bir.USub(),
      # Binary operators
      ast.Add    : bir.Add(),       ast.Sub    : bir.Sub(),
      ast.Mult   : bir.Mult(),      ast.Div    : bir.Div(),
      ast.Mod    : bir.Mod(),       ast.Pow    : bir.Pow(),
      ast.LShift : bir.ShiftLeft(), ast.RShift : bir.ShiftRightLogic(),
      ast.BitOr  : bir.BitOr(),     ast.BitAnd : bir.BitAnd(),
      ast.BitXor : bir.BitXor(),
      # Compare bir.bir.operators
      ast.Eq     : bir.Eq(),        ast.NotEq  : bir.NotEq(),
      ast.Lt     : bir.Lt(),        ast.LtE    : bir.LtE(),
      ast.Gt     : bir.Gt(),        ast.GtE    : bir.GtE()
    }

  # Override 
開發者ID:pymtl,項目名稱:pymtl3,代碼行數:33,代碼來源:BehavioralRTLIRGenL2Pass.py

示例8: visit_UnaryOp

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import UAdd [as 別名]
def visit_UnaryOp(self, node: ast.UnaryOp) -> Any:
        """Visit the node operand and apply the operation on the result."""
        if isinstance(node.op, ast.UAdd):
            result = +self.visit(node=node.operand)
        elif isinstance(node.op, ast.USub):
            result = -self.visit(node=node.operand)
        elif isinstance(node.op, ast.Not):
            result = not self.visit(node=node.operand)
        elif isinstance(node.op, ast.Invert):
            result = ~self.visit(node=node.operand)
        else:
            raise NotImplementedError("Unhandled op of {}: {}".format(node, node.op))

        self.recomputed_values[node] = result
        return result 
開發者ID:Parquery,項目名稱:icontract,代碼行數:17,代碼來源:_recompute.py

示例9: visit_UnaryOp

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import UAdd [as 別名]
def visit_UnaryOp(self, node):
        """
        Adjust operand value and discard unary operations, where possible.

        This is done so that negative decimal literals are accurately represented.
        """
        self.generic_visit(node)

        # TODO once grammar is updated, remove this
        # UAdd has no effect on the value of it's operand, so it is discarded
        if isinstance(node.op, python_ast.UAdd):
            return node.operand

        is_sub = isinstance(node.op, python_ast.USub)
        is_num = (
            hasattr(node.operand, "n")
            and not isinstance(node.operand.n, bool)
            and isinstance(node.operand.n, (int, Decimal))
        )
        if is_sub and is_num:
            node.operand.n = 0 - node.operand.n
            node.operand.col_offset = node.col_offset
            node.operand.node_source_code = node.node_source_code
            return node.operand
        else:
            return node 
開發者ID:vyperlang,項目名稱:vyper,代碼行數:28,代碼來源:annotation.py

示例10: unaryop

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import UAdd [as 別名]
def unaryop(self, block, node):
    operand = self.expression(block, node.operand)
    if isinstance(node.op, ast.UAdd):
      #No-Op
      var = operand
    elif isinstance(node.op, ast.USub):
      if operand.value is not None:
        #It's a literal, return the negation
        var = self.add_literal(-operand.value)
      else:
        #It's an expression, subtract from zero (faster than multiplying by -1)
        var = self.add_variable(None)
        zero = self.add_literal(0, 'ZERO')
        operation = BinaryOperation(zero, Operator.subtract, operand)
        assignment = Assignment(var, operation)
        block.add(assignment)
    elif isinstance(node.op, ast.Invert):
      #Make sure it's numeric
      if operand.is_mask:
        raise Exception('Unary invert requires a numeric operand')
      #Bit-flip
      var = self.add_variable(None)
      operation = UnaryOperation(Operator.bit_not, operand)
      assignment = Assignment(var, operation)
      block.add(assignment)
    elif isinstance(node.op, ast.Not):
      #Make sure it's boolean
      if not operand.is_mask:
        raise Exception('Unary not requires a boolean operand')
      #Invert the mask
      var = self.add_variable(None, is_mask=True)
      operation = UnaryOperation(Operator.bool_not, operand)
      assignment = Assignment(var, operation)
      block.add(assignment)
    else:
      raise Exception('Unexpected UnaryOp (%s)'%(node.op.__class__))
    return var

  #Parses a comparison operator (AST CmpOp) 
開發者ID:undefx,項目名稱:vecpy,代碼行數:41,代碼來源:parser.py

示例11: is_interesting_expression

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import UAdd [as 別名]
def is_interesting_expression(node):
    # type: (ast.AST) -> bool
    """
    If this expression has a value that may not be exactly what it looks like,
    return True. Put differently, return False if this is just a literal.
    """
    return (isinstance(node, ast.expr) and
            not (isinstance(node, (ast.Num, ast.Str, getattr(ast, 'NameConstant', ()))) or
                 isinstance(getattr(node, 'ctx', None),
                            (ast.Store, ast.Del)) or
                 (isinstance(node, ast.UnaryOp) and
                  isinstance(node.op, (ast.UAdd, ast.USub)) and
                  isinstance(node.operand, ast.Num)) or
                 (isinstance(node, (ast.List, ast.Tuple, ast.Dict)) and
                  not any(is_interesting_expression(n) for n in ast.iter_child_nodes(node))))) 
開發者ID:alexmojaki,項目名稱:executing,代碼行數:17,代碼來源:bird.py

示例12: visit_UAdd

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import UAdd [as 別名]
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) 
開發者ID:PyCQA,項目名稱:flake8-bugbear,代碼行數:8,代碼來源:bugbear.py

示例13: p_factor_1

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import UAdd [as 別名]
def p_factor_1(p):
    '''factor : PLUS factor'''
    #              1      2
    op = ast.UAdd(rule=inspect.currentframe().f_code.co_name, **p[1][1])
    p[0] = ast.UnaryOp(op, p[2], rule=inspect.currentframe().f_code.co_name)
    inherit_lineno(p[0], op) 
開發者ID:histogrammar,項目名稱:histogrammar-python,代碼行數:8,代碼來源:hgawk_grammar.py


注:本文中的ast.UAdd方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。