当前位置: 首页>>代码示例>>Python>>正文


Python ast.Invert方法代码示例

本文整理汇总了Python中ast.Invert方法的典型用法代码示例。如果您正苦于以下问题:Python ast.Invert方法的具体用法?Python ast.Invert怎么用?Python ast.Invert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ast的用法示例。


在下文中一共展示了ast.Invert方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: check_not

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Invert [as 别名]
def check_not(self, target, pattern):
        'Check NOT pattern node that could be in another form'
        if self.is_wildcard(pattern.operand):
            wkey = pattern.operand.id
            if isinstance(target, ast.Num):
                if wkey not in self.wildcards:
                    mod = 2**self.nbits
                    self.wildcards[wkey] = ast.Num((~target.n) % mod)
                    return True
                else:
                    wilds2 = self.wildcards[pattern.operand.id]
                    num = ast.Num((~target.n) % 2**self.nbits)
                    return asttools.Comparator().visit(wilds2, num)
            else:
                if wkey not in self.wildcards:
                    self.wildcards[wkey] = ast.UnaryOp(ast.Invert(),
                                                       target)
                    return True
            return self.check_eq_z3(target, pattern)
        else:
            subpattern = pattern.operand
            newtarget = ast.UnaryOp(ast.Invert(), target)
            return self.check_eq_z3(newtarget, subpattern) 
开发者ID:quarkslab,项目名称:sspam,代码行数:25,代码来源:pattern_matcher.py

示例2: factor_rewrite

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Invert [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: visit_UnaryOp

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Invert [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

示例4: visit_UnaryOp

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Invert [as 别名]
def visit_UnaryOp(self, node):
        'Change ~x to - x - 1'

        if isinstance(node.op, ast.Invert):
            return ast.BinOp(ast.UnaryOp(ast.USub(), node.operand),
                             ast.Add(),
                             ast.Num(-1))
        return self.generic_visit(node) 
开发者ID:quarkslab,项目名称:sspam,代码行数:10,代码来源:pre_processing.py

示例5: check_pattern

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Invert [as 别名]
def check_pattern(self, target, pattern):
        'Try to match pattern written in different ways'

        if asttools.CheckConstExpr().visit(pattern):
            if isinstance(target, ast.Num):
                # if pattern is only a constant, evaluate and compare
                # to target
                pattcopy = deepcopy(pattern)
                eval_pat = asttools.ConstFolding(pattcopy,
                                                 self.nbits).visit(pattcopy)
                return self.visit(target, eval_pat)

        if isinstance(target, ast.Num):
            # check that wildcards in pattern have not been affected
            return self.get_model(target, pattern)

        # deal with NOT that could have been evaluated before
        notnode = (isinstance(pattern, ast.UnaryOp) and
                   isinstance(pattern.op, ast.Invert))
        if notnode:
            return self.check_not(target, pattern)

        # deal with (-1)*B that could have been evaluated
        negnode = (isinstance(pattern, ast.BinOp)
                   and isinstance(pattern.op, ast.Mult)
                   and isinstance(pattern.left, ast.Num)
                   and pattern.left.n == -1)
        if negnode:
            return self.check_neg(target, pattern)

        # deal with 2*B
        multnode = (isinstance(pattern, ast.BinOp) and
                    isinstance(pattern.op, ast.Mult))
        if multnode:
            return self.check_twomult(target, pattern)

        # return self.general_check(target, pattern)
        return False 
开发者ID:quarkslab,项目名称:sspam,代码行数:40,代码来源:pattern_matcher.py

示例6: visit_UnaryOp

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Invert [as 别名]
def visit_UnaryOp(self, node):
        'Replace bitwise unaryop with function call'
        self.generic_visit(node)
        if isinstance(node.op, ast.Invert):
            return ast.Call(ast.Name('mnot', ast.Load()),
                            [node.operand], [], None, None)
        return node 
开发者ID:quarkslab,项目名称:sspam,代码行数:9,代码来源:asttools.py

示例7: visit_Call

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Invert [as 别名]
def visit_Call(self, node):
        'Replace custom function with bitwise operators'
        self.generic_visit(node)
        if isinstance(node.func, ast.Name):
            if len(node.args) == 2:
                if node.func.id == "mand":
                    op = ast.BitAnd()
                elif node.func.id == "mxor":
                    op = ast.BitXor()
                elif node.func.id == "mor":
                    op = ast.BitOr()
                elif node.func.id == "mlshift":
                    op = ast.LShift()
                elif node.func.id == "mrshift":
                    op = ast.RShift()
                else:
                    return node

                return ast.BinOp(node.args[0],
                                 op,
                                 node.args[1])

            elif len(node.args) == 1 and node.func.id == "mnot":
                arg = node.args[0]
                self.generic_visit(node)
                return ast.UnaryOp(ast.Invert(), arg)

        return self.generic_visit(node) 
开发者ID:quarkslab,项目名称:sspam,代码行数:30,代码来源:asttools.py

示例8: __init__

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Invert [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

示例9: visit_UnaryOp

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Invert [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

示例10: unaryop

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Invert [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: p_factor_3

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Invert [as 别名]
def p_factor_3(p):
    '''factor : TILDE factor'''
    #               1      2
    op = ast.Invert(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.Invert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。