示例#1
0
def newBinOp(o):
    if type(o) == type(_ast.Add()):
        return _ast.Sub()
    elif type(o) == type(_ast.Sub()):
        return _ast.Add()#elif type(o) == type(_ast.)
    elif type(o) == type(_ast.Mult()):
        return _ast.FloorDiv()
    elif type(o) == type(_ast.FloorDiv()):
        return _ast.Mult()
    else:
        return o
示例#2
0
 def test_simple_sums(self):
     ast = self.ast
     mod = self.get_ast("x = 4 + 5")
     expr = mod.body[0].value
     assert isinstance(expr, ast.BinOp)
     assert isinstance(expr.op, ast.Add)
     expr.op = ast.Sub()
     assert isinstance(expr.op, ast.Sub)
     co = compile(mod, "<example>", "exec")
     ns = {}
     exec co in ns
     assert ns["x"] == -1
     mod = self.get_ast("4 < 5 < 6", "eval")
     assert isinstance(mod.body, ast.Compare)
     assert len(mod.body.ops) == 2
     for op in mod.body.ops:
         assert isinstance(op, ast.Lt)
     mod.body.ops[0] = ast.Gt()
     co = compile(mod, "<string>", "eval")
     assert not eval(co)
示例#3
0
def Sub():
    return _ast.Sub()
示例#4
0
# Definição da função
func = body[0]
# <_ast.FunctionDef at 0x7f3173b02908>

# Corpo da função
f_body = func.body[0]
# <_ast.Return at 0x7f3173b02940>

# Valor da função
f_body.value
# <_ast.BinOp at 0x7f3173b02978>

# Operação da função
f_body.value.op
# <_ast.Add at 0x7f60a4339278>

exec(compile(ast_module, filename="<ast>", mode="exec"))

print(funcao_soma)

assert soma(1, 1) == 2

ast_module.body[0].body[0].value.op = _ast.Sub()

exec(compile(ast_module, filename="<ast>", mode="exec"))

inspect.getsource(soma)

assert soma(1, 1) == 2