本文整理匯總了Python中ast.Mult方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.Mult方法的具體用法?Python ast.Mult怎麽用?Python ast.Mult使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.Mult方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_MutateAST_visit_augassign
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def test_MutateAST_visit_augassign(augassign_file, augassign_expected_locs):
"""Test mutation for AugAssign: +=, -=, /=, *=."""
tree = Genome(augassign_file).ast
test_mutation = "AugAssign_Div"
testing_tree = deepcopy(tree)
mutated_tree = MutateAST(target_idx=augassign_expected_locs[0], mutation=test_mutation).visit(
testing_tree
)
mast = MutateAST(readonly=True)
mast.visit(mutated_tree)
assert len(mast.locs) == 4
for loc in mast.locs:
# spot check on mutation from Add tp Div
if loc.lineno == 1 and loc.col_offset == 4:
assert loc.op_type == test_mutation
# spot check on not-mutated location still being Mult
if loc.lineno == 5 and loc.col_offset == 4:
assert loc.op_type == "AugAssign_Mult"
示例2: visit_AugAssign
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def visit_AugAssign(self, node):
# XXX apparently no signed context required for augmented assigns
left, op, right = node.target, node.op, node.value
isFunc = False
pre, suf = "", ""
if isinstance(op, (ast.Add, ast.Sub, ast.Mult, ast.Mod, ast.FloorDiv)):
pre, suf = self.inferBinaryOpCast(node, left, right, op)
elif isinstance(op, (ast.LShift, ast.RShift)):
isFunc = True
pre, suf = self.inferShiftOpCast(node, left, right, op)
self.visit(left)
self.write(" := ")
self.write(pre)
if isFunc:
self.write("%s(" % opmap[type(op)])
self.visit(left)
if isFunc:
self.write(", ")
else:
self.write(" %s " % opmap[type(op)])
self.visit(right)
if isFunc:
self.write(")")
self.write(suf)
self.write(";")
示例3: term_rewrite
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def term_rewrite(head, tail):
if tail:
for op, each in tail:
head = ast.BinOp(
head,
{
'*': ast.Mult,
'@': ast.MatMult,
'%': ast.Mod,
'//': ast.FloorDiv,
'/': ast.Div
}[op.value](),
each,
**loc @ op,
)
return head
示例4: augassign_rewrite
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def augassign_rewrite(it: Tokenizer):
return {
'+=': ast.Add,
'-=': ast.Sub,
'*=': ast.Mult,
'/=': ast.Div,
'//=': ast.FloorDiv,
'@=': ast.MatMult,
'%=': ast.Mod,
'&=': ast.BitAnd,
'|=': ast.BitOr,
'^=': ast.BitXor,
'<<=': ast.LShift,
'>>=': ast.RShift,
'**=': ast.Pow,
}[it.value]
示例5: test_afterSubMult
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def test_afterSubMult(self):
'Tests after SubToMult pre-processing'
tests = [("1 + 2 - 3", ast.BoolOp(ast.Add(), [ast.Num(1), ast.Num(2),
ast.BinOp(ast.Num(-1),
ast.Mult(),
ast.Num(3))])),
("1 + 2 - 3 + 4", ast.BoolOp(ast.Add(),
[ast.Num(1),
ast.Num(2),
ast.BinOp(ast.Num(-1),
ast.Mult(),
ast.Num(3)),
ast.Num(4)])),
("(1 + 2) - (3 + 4)",
ast.BoolOp(ast.Add(),
[ast.Num(1), ast.Num(2),
ast.BinOp(ast.Num(-1), ast.Mult(),
ast.BinOp(ast.Num(3), ast.Add(),
ast.Num(4)))]))]
for teststring, ref_ast in tests:
test_ast = ast.parse(teststring, mode="eval").body
test_ast = pre_processing.all_preprocessings(test_ast)
test_ast = Flattening(ast.Add).visit(test_ast)
self.assertTrue(Comparator().visit(test_ast, ref_ast))
示例6: test_noflattening
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def test_noflattening(self):
'Tests where nothing should be flattened'
corresp = [(["a + b", "b + a"],
ast.BinOp(ast.Name('a', ast.Load()),
ast.Add(),
ast.Name('b', ast.Load()))),
(["c*d", "d*c"],
ast.BinOp(ast.Name('c', ast.Load()),
ast.Mult(),
ast.Name('d', ast.Load()))),
(["a + c*d", "d*c + a"],
ast.BinOp(ast.Name('a', ast.Load()), ast.Add(),
ast.BinOp(ast.Name('c', ast.Load()), ast.Mult(),
ast.Name('d', ast.Load()))))]
for refstring, result in corresp:
self.generic_flattening(refstring, result)
示例7: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def visit_UnaryOp(self, node):
'Change -x to (-1)*x'
self.generic_visit(node)
if isinstance(node.op, ast.USub):
ope = node.operand
cond_mult = (isinstance(ope, ast.BinOp) and
isinstance(ope.op, ast.Mult))
if cond_mult:
if isinstance(ope.left, ast.Num):
node = ast.BinOp(ast.Num(-ope.left.n), ast.Mult(),
ope.right)
elif isinstance(ope.right, ast.Num):
node = ast.BinOp(ope.left, ast.Mult(),
ast.Num(-ope.right.n))
else:
node = ast.BinOp(ast.Num(-1), ast.Mult(), ope)
else:
node = ast.BinOp(ast.Num(-1), ast.Mult(), ope)
return node
示例8: check_neg
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def check_neg(self, target, pattern):
'Check (-1)*... pattern that could be in another form'
if self.is_wildcard(pattern.right):
wkey = pattern.right.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.right.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.BinOp(ast.Num(-1),
ast.Mult(), target)
return True
return self.check_eq_z3(target, pattern)
示例9: visit_BoolOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def visit_BoolOp(self, node):
'A custom BoolOp can be used in flattened AST'
if type(node.op) not in (ast.Add, ast.Mult,
ast.BitXor, ast.BitAnd, ast.BitOr):
return self.generic_visit(node)
# get constant parts of node:
list_cste = [child for child in node.values
if isinstance(child, ast.Num)]
if len(list_cste) < 2:
return self.generic_visit(node)
rest_values = [n for n in node.values if n not in list_cste]
fake_node = Unflattening().visit(ast.BoolOp(node.op, list_cste))
fake_node = ast.Expression(fake_node)
ast.fix_missing_locations(fake_node)
code = compile(fake_node, '<constant folding>', 'eval')
obj_env = globals().copy()
exec code in obj_env
value = eval(code, obj_env)
new_node = ast.Num(value)
rest_values.append(new_node)
return ast.BoolOp(node.op, rest_values)
示例10: visit_BinOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def visit_BinOp(self, node1, node2):
'Check type of operation and operands'
if type(node1.op) != type(node2.op):
return False
# if operation is commutative, left and right operands are
# interchangeable
cond1 = (self.visit(node1.left, node2.left) and
self.visit(node1.right, node2.right))
cond2 = (self.visit(node1.left, node2.right)
and self.visit(node1.right, node2.left))
# non-commutative comparator
if not self.commut:
return cond1
if isinstance(node1.op, (ast.Add, ast.Mult,
ast.BitAnd, ast.BitOr, ast.BitXor)):
if cond1 or cond2:
return True
else:
return False
else:
if cond1:
return True
return False
示例11: test_nodeclasses
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def test_nodeclasses(self):
# IronPyhon performs argument typechecking
l=ast.Str('A')
o=ast.Mult()
r=ast.Num('13')
x=ast.BinOp(l,o,r,lineno=42)
self.assertEqual(x.left, l)
self.assertEqual(x.op, o)
self.assertEqual(x.right, r)
self.assertEqual(x.lineno, 42)
# node raises exception when not given enough arguments
self.assertRaises(TypeError, ast.BinOp, l, o)
# can set attributes through kwargs too
x = ast.BinOp(left=l, op=o, right=r, lineno=42)
self.assertEqual(x.left, l)
self.assertEqual(x.op, o)
self.assertEqual(x.right, r)
self.assertEqual(x.lineno, 42)
# this used to fail because Sub._fields was None
x = ast.Sub()
示例12: test_create_mutant_with_cache
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def test_create_mutant_with_cache(binop_file, stdoutIO):
"""Change ast.Add to ast.Mult in a mutation including pycache changes."""
genome = Genome(source_file=binop_file)
# this target is the add_five() function, changing add to mult
end_lineno = None if sys.version_info < (3, 8) else 10
end_col_offset = None if sys.version_info < (3, 8) else 16
target_idx = LocIndex(
ast_class="BinOp",
lineno=10,
col_offset=11,
op_type=ast.Add,
end_lineno=end_lineno,
end_col_offset=end_col_offset,
)
mutation_op = ast.Mult
mutant = genome.mutate(target_idx, mutation_op, write_cache=True)
# uses the redirection for stdout to capture the value from the final output of binop_file
with stdoutIO() as s:
exec(mutant.mutant_code)
assert int(s.getvalue()) == 25
tag = sys.implementation.cache_tag
expected_cfile = binop_file.parent / "__pycache__" / ".".join([binop_file.stem, tag, "pyc"])
assert mutant.src_file == binop_file
assert mutant.cfile == expected_cfile
assert mutant.src_idx == target_idx
示例13: mock_Mutant
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def mock_Mutant():
"""Mock mutant definition."""
return Mutant(
mutant_code=None,
src_file=Path("src.py"),
cfile=Path("__pycache__") / "src.pyc",
loader=None,
mode=1,
source_stats={"mtime": 1, "size": 1},
src_idx=LocIndex(ast_class="BinOp", lineno=1, col_offset=2, op_type=ast.Add),
mutation=ast.Mult,
)
示例14: add_five_to_mult_mutant
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def add_five_to_mult_mutant(binop_file, stdoutIO, binop_Add_LocIdx):
"""Mutant that takes add_five op ADD to MULT. Fails if mutation code does not work."""
genome = Genome(source_file=binop_file)
mutation_op = ast.Mult
mutant = genome.mutate(binop_Add_LocIdx, mutation_op, write_cache=True)
# uses the redirection for stdout to capture the value from the final output of binop_file
with stdoutIO() as s:
exec(mutant.mutant_code)
assert int(s.getvalue()) == 25
return mutant
示例15: test_differentops
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mult [as 別名]
def test_differentops(self):
'Test with other types of operators'
tests = [("(3 & 5 & 6)",
ast.BoolOp(ast.BitAnd(),
[ast.Num(3), ast.Num(5), ast.Num(6)])),
("(1 ^ 2 ^ 3) - 4",
ast.BinOp(ast.BoolOp(ast.BitXor(),
[ast.Num(1), ast.Num(2), ast.Num(3)]),
ast.Add(),
ast.BinOp(ast.Num(-1), ast.Mult(), ast.Num(4)))),
("((1 + 2 + 3) & (4 + 5))",
ast.BinOp(ast.BoolOp(ast.Add(),
[ast.Num(1), ast.Num(2), ast.Num(3)]),
ast.BitAnd(),
ast.BinOp(ast.Num(4), ast.Add(), ast.Num(5)))),
("(1 & 2 & 3) - (4 & 5)",
ast.BinOp(ast.BoolOp(ast.BitAnd(),
[ast.Num(1), ast.Num(2), ast.Num(3)]),
ast.Add(),
ast.BinOp(ast.Num(-1), ast.Mult(),
ast.BinOp(ast.Num(4), ast.BitAnd(),
ast.Num(5))))),
("(1 & 2 & 3) << (4 & 5)",
ast.BinOp(ast.BoolOp(ast.BitAnd(),
[ast.Num(1), ast.Num(2), ast.Num(3)]),
ast.LShift(),
ast.BinOp(ast.Num(4), ast.BitAnd(), ast.Num(5))))]
for teststring, ref_ast in tests:
test_ast = ast.parse(teststring, mode="eval").body
test_ast = pre_processing.all_preprocessings(test_ast)
test_ast = Flattening().visit(test_ast)
self.assertTrue(Comparator().visit(test_ast, ref_ast))