本文整理汇总了Python中ast.Sub方法的典型用法代码示例。如果您正苦于以下问题:Python ast.Sub方法的具体用法?Python ast.Sub怎么用?Python ast.Sub使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ast
的用法示例。
在下文中一共展示了ast.Sub方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: visit_AugAssign
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [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(";")
示例2: augassign_rewrite
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [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]
示例3: test_nodeclasses
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [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()
示例4: parse
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [as 别名]
def parse(self):
@self.pg.production('program : PRINT OPEN_PAREN expression CLOSE_PAREN SEMI_COLON')
def program(p):
return Print(self.builder, self.module, self.printf, p[2])
@self.pg.production('expression : expression SUM expression')
@self.pg.production('expression : expression SUB expression')
def expression(p):
left = p[0]
right = p[2]
operator = p[1]
if operator.gettokentype() == 'SUM':
return Sum(self.builder, self.module, left, right)
elif operator.gettokentype() == 'SUB':
return Sub(self.builder, self.module, left, right)
@self.pg.production('expression : NUMBER')
def number(p):
return Number(self.builder, self.module, p[0].value)
@self.pg.error
def error_handle(token):
raise ValueError(token)
示例5: test_nodeclasses
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [as 别名]
def test_nodeclasses(self):
x = ast.BinOp(1, 2, 3, lineno=0)
self.assertEquals(x.left.n, 1)
self.assertEquals(int(x.op), 2)
self.assertEquals(x.right.n, 3)
self.assertEquals(x.lineno, 0)
# node raises exception when not given enough arguments
self.assertRaises(TypeError, ast.BinOp, 1, 2)
# can set attributes through kwargs too
x = ast.BinOp(left=1, op=2, right=3, lineno=0)
self.assertEquals(x.left.n, 1)
self.assertEquals(int(x.op), 2)
self.assertEquals(x.right.n, 3)
self.assertEquals(x.lineno, 0)
# this used to fail because Sub._fields was None
x = ast.Sub()
示例6: visit_BinOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [as 别名]
def visit_BinOp(self, node):
self.visit(node.left)
self.visit(node.right)
node.signed = node.left.signed or node.right.signed
# special treatement of subtraction unless in a top-level rhs
if isinstance(node.op, ast.Sub) and not hasattr(node, 'isRhs'):
node.signed = True
示例7: arith_expr_rewrite
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [as 别名]
def arith_expr_rewrite(head, tail):
if tail:
for op, each in tail:
head = ast.BinOp(
head,
{
'+': ast.Add,
'-': ast.Sub
}[op.value](),
each,
**loc @ op,
)
return head
示例8: visit_BinOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [as 别名]
def visit_BinOp(self, node):
'Change operator - to a *(-1)'
self.generic_visit(node)
if isinstance(node.op, ast.Sub):
node.op = ast.Add()
cond_mult = (isinstance(node.right, ast.BinOp) and
isinstance(node.right.op, ast.Mult))
if cond_mult:
if isinstance(node.right.left, ast.Num):
coeff = node.right.left
operand = node.right.right
elif isinstance(node.right.right, ast.Num):
coeff = node.right.right
operand = node.right.left
else:
node.right = ast.BinOp(ast.Num(-1), ast.Mult(), node.right)
return node
# trying to "simplify" constant coeffs if possible
if self.nbits:
if (-coeff.n) % 2**self.nbits == 1:
node.right = operand
else:
coeff.n = -coeff.n % 2**self.nbits
else:
coeff.n = -coeff.n
else:
node.right = ast.BinOp(ast.Num(-1), ast.Mult(), node.right)
return node
示例9: test_no_fields
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [as 别名]
def test_no_fields(self):
# this used to fail because Sub._fields was None
x = ast.Sub()
self.assertEqual(x._fields, ())
示例10: onReturnPressed
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [as 别名]
def onReturnPressed(self):
expr = str(self.ui.lineEdit.text())
import ast
import operator as op
# supported operators
operators = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul,
ast.Div: op.floordiv, ast.Pow: op.pow, ast.USub: op.neg}
def eval_expr(expr):
return eval_(ast.parse(expr, mode='eval').body)
def eval_(node):
if isinstance(node, ast.Num):
return node.n
elif isinstance(node, ast.BinOp):
return operators[type(node.op)](eval_(node.left), eval_(node.right))
elif isinstance(node, ast.UnaryOp):
return operators[type(node.op)](eval_(node.operand))
elif isinstance(node, object):
# handle constants
k = str(node.id).upper()
if k in self.konstants:
return self.konstants[k](k)
else:
raise TypeError(node)
else:
raise TypeError(node)
try:
result = eval_expr(expr)
except Exception as e:
self.ui.label.setText('error.')
return
self.ui.label.setText('{0} ({1})'.format(hex(result), result))
self.onResult(result)
示例11: visit_UnaryOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [as 别名]
def visit_UnaryOp(self, node):
assert isinstance(node.op, ast.USub), \
'only unary minus is supported, not {}'.format(node.op.__class__.__name__)
self.visit(ast.Num(n=0))
self.visit(node.operand)
self.visit(ast.Sub())
示例12: __init__
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [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
示例13: stringify_operation
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [as 别名]
def stringify_operation(op: Any) -> str:
if isinstance(op, ast.Add):
return '+'
if isinstance(op, ast.Sub):
return '-'
if isinstance(op, ast.Mult):
return '*'
if isinstance(op, ast.Div) or isinstance(op, ast.FloorDiv):
return '/'
return str(op)
示例14: visit_BinOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [as 别名]
def visit_BinOp(self, node: ast.BinOp) -> Any:
"""Recursively visit the left and right operand, respectively, and apply the operation on the results."""
# pylint: disable=too-many-branches
left = self.visit(node=node.left)
right = self.visit(node=node.right)
if isinstance(node.op, ast.Add):
result = left + right
elif isinstance(node.op, ast.Sub):
result = left - right
elif isinstance(node.op, ast.Mult):
result = left * right
elif isinstance(node.op, ast.Div):
result = left / right
elif isinstance(node.op, ast.FloorDiv):
result = left // right
elif isinstance(node.op, ast.Mod):
result = left % right
elif isinstance(node.op, ast.Pow):
result = left**right
elif isinstance(node.op, ast.LShift):
result = left << right
elif isinstance(node.op, ast.RShift):
result = left >> right
elif isinstance(node.op, ast.BitOr):
result = left | right
elif isinstance(node.op, ast.BitXor):
result = left ^ right
elif isinstance(node.op, ast.BitAnd):
result = left & right
elif isinstance(node.op, ast.MatMult):
result = left @ right
else:
raise NotImplementedError("Unhandled op of {}: {}".format(node, node.op))
self.recomputed_values[node] = result
return result
示例15: visit_Add
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Sub [as 别名]
def visit_Add(self, node):
node = ast.Sub()
return node