Python ast.Invert() Examples
The following are 16
code examples of ast.Invert().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ast
, or try the search function
.
Example #1
Source File: pattern_matcher.py From sspam with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #2
Source File: helper.py From YAPyPy with MIT License | 5 votes |
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, )
Example #3
Source File: pytables.py From recruit with Apache License 2.0 | 5 votes |
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')
Example #4
Source File: pre_processing.py From sspam with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)
Example #5
Source File: pattern_matcher.py From sspam with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #6
Source File: asttools.py From sspam with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #7
Source File: asttools.py From sspam with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)
Example #8
Source File: pytables.py From vnpy_crypto with MIT License | 5 votes |
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')
Example #9
Source File: pytables.py From Computable with MIT License | 5 votes |
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')
Example #10
Source File: BehavioralRTLIRGenL2Pass.py From pymtl3 with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #11
Source File: pytables.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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')
Example #12
Source File: _recompute.py From icontract with MIT License | 5 votes |
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
Example #13
Source File: parser.py From vecpy with MIT License | 5 votes |
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)
Example #14
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
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)
Example #15
Source File: pytables.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
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')
Example #16
Source File: pytables.py From elasticintel with GNU General Public License v3.0 | 5 votes |
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')