本文整理匯總了Python中ast.USub方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.USub方法的具體用法?Python ast.USub怎麽用?Python ast.USub使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.USub方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [as 別名]
def visit_UnaryOp(self, node):
# in python3 a negative Num is represented as an USub of a positive Num
# Fix: restore python2 behavior by a shortcut: invert value of Num, inherit
# vhdl type from UnaryOp node, and visit the modified operand
if isinstance(node.op, ast.USub) and isinstance(node.operand, ast.Num):
node.operand.n = -node.operand.n
node.operand.vhd = node.vhd
self.visit(node.operand)
return
pre, suf = self.inferCast(node.vhd, node.vhdOri)
self.write(pre)
self.write("(")
self.write(opmap[type(node.op)])
self.visit(node.operand)
self.write(")")
self.write(suf)
示例2: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [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
示例3: unary_minus_expression_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [as 別名]
def unary_minus_expression_ast_to_ir2(ast_node: ast.UnaryOp,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
current_stmt_line: int):
assert isinstance(ast_node.op, ast.USub)
if in_match_pattern:
raise CompilationError(compilation_context, ast_node,
'The "-" operator is not allowed in match patterns')
expr = expression_ast_to_ir2(ast_node.operand,
compilation_context,
in_match_pattern,
check_var_reference,
match_lambda_argument_names,
current_stmt_line)
if expr.expr_type != ir2.IntType():
raise CompilationError(compilation_context, ast_node.operand,
'The "-" operator is only supported for ints, but this value has type %s.' % str(expr.expr_type))
return ir2.IntUnaryMinusExpr(expr=expr)
示例4: visit_Hook
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [as 別名]
def visit_Hook(self, node: parsing.Hook) -> ast.expr:
"""Generates python code calling a hook.
self.evalHook('hookname', self.ruleNodes[-1])
"""
return ast.Call(
ast.Attribute(
ast.Name('self', ast.Load()), 'evalHook', ast.Load()),
[
ast.Str(node.name),
ast.Subscript(
ast.Attribute(
ast.Name('self', ast.Load()), 'ruleNodes', ast.Load()),
ast.Index(ast.UnaryOp(ast.USub(), ast.Num(1))),
ast.Load())],
[],
None,
None)
示例5: _get_non_negative_nodes
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [as 別名]
def _get_non_negative_nodes(
self,
left: Optional[ast.AST],
right: Optional[ast.AST] = None,
):
non_negative_numbers = []
for node in filter(None, (left, right)):
real_node = unwrap_unary_node(node)
correct_node = (
isinstance(real_node, ast.Num) and
real_node.n in self._meaningless_operations and
not (real_node.n == 1 and walk.is_contained(node, ast.USub))
)
if correct_node:
non_negative_numbers.append(real_node)
return non_negative_numbers
示例6: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [as 別名]
def visit_UnaryOp(self, node):
self.visit(node.operand)
node.signed = node.operand.signed
if isinstance(node.op, ast.USub):
node.obj = int(-1)
if isinstance(node.operand, ast.Num):
node.signed = True
示例7: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [as 別名]
def visit_UnaryOp(self, node):
self.visit(node.operand)
op = node.op
node.obj = node.operand.obj
if isinstance(op, ast.Not):
node.obj = bool()
elif isinstance(op, ast.UAdd):
node.obj = int(-1)
elif isinstance(op, ast.USub):
node.obj = int(-1)
示例8: factor_rewrite
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [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,
)
示例9: visitUnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [as 別名]
def visitUnaryOp(self, node):
import ast
if isinstance(node.op, ast.UAdd):
return +self.visit(node.operand)
elif isinstance(node.op, ast.USub):
return -self.visit(node.operand)
else:
raise SyntaxError("Unknown unary op: %r" % node.op)
示例10: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [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')
示例11: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [as 別名]
def visit_UnaryOp(self, node):
'Change USub and Invert'
operand = self.visit(node.operand)
if isinstance(node.op, ast.UAdd):
return operand
if isinstance(node.op, ast.USub):
return ast.BinOp(ast.Num(-1), ast.Mult(), operand)
if isinstance(node.op, ast.Invert):
return ast.BinOp(ast.Num(-1), ast.BitXor(), operand)
assert False, 'unhandled node type: ' + ast.dump(node)
示例12: test_docexample
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [as 別名]
def test_docexample(self):
# used to fail on ironpython for various reason
node = ast.UnaryOp(ast.USub(), ast.Num(5, lineno=0, col_offset=0),
lineno=0, col_offset=0)
# the same with zero argument constructors
node = ast.UnaryOp()
node.op = ast.USub()
node.operand = ast.Num()
node.operand.n = 5
node.operand.lineno = 0
node.operand.col_offset = 0
node.lineno = 0
node.col_offset = 0
示例13: onReturnPressed
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [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)
示例14: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [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())
示例15: visit_For
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import USub [as 別名]
def visit_For(self, node):
# Turn for+range loop into a while loop:
# i = start
# while i < stop:
# body
# i = i + step
assert isinstance(node.iter, ast.Call) and \
node.iter.func.id == 'range', \
'for can only be used with range()'
range_args = node.iter.args
if len(range_args) == 1:
start = ast.Num(n=0)
stop = range_args[0]
step = ast.Num(n=1)
elif len(range_args) == 2:
start, stop = range_args
step = ast.Num(n=1)
else:
start, stop, step = range_args
if (isinstance(step, ast.UnaryOp) and
isinstance(step.op, ast.USub) and
isinstance(step.operand, ast.Num)):
# Handle negative step
step = ast.Num(n=-step.operand.n)
assert isinstance(step, ast.Num) and step.n != 0, \
'range() step must be a nonzero integer constant'
self.visit(ast.Assign(targets=[node.target], value=start))
test = ast.Compare(
left=node.target,
ops=[ast.Lt() if step.n > 0 else ast.Gt()],
comparators=[stop],
)
incr = ast.Assign(
targets=[node.target],
value=ast.BinOp(left=node.target, op=ast.Add(), right=step),
)
self.visit(ast.While(test=test, body=node.body + [incr]))