Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def visit_UnaryOp(self, node):
with self.delimit(node, node.op) as delimiters:
set_precedence(delimiters.p, node.operand)
# In Python 2.x, a unary negative of a literal
# number is merged into the number itself. This
# bit of ugliness means it is useful to know
# what the parent operation was...
node.operand._p_op = node.op
sym = get_op_symbol(node.op)
self.write(sym, ' ' if sym.isalpha() else '', node.operand)
def visit_BinOp(self, node):
op, left, right = node.op, node.left, node.right
with self.delimit(node, op) as delimiters:
ispow = isinstance(op, ast.Pow)
p = delimiters.p
set_precedence((Precedence.Pow + 1) if ispow else p, left)
set_precedence(Precedence.PowRHS if ispow else (p + 1), right)
self.write(left, get_op_symbol(op, ' %s '), right)
def visit_BoolOp(self, node):
with self.delimit(node, node.op) as delimiters:
op = get_op_symbol(node.op, ' %s ')
set_precedence(delimiters.p + 1, *node.values)
for idx, value in enumerate(node.values):
self.write(idx and op or '', value)
def visit_Compare(self, node):
with self.delimit(node, node.ops[0]) as delimiters:
set_precedence(delimiters.p + 1, node.left, *node.comparators)
self.visit(node.left)
for op, right in zip(node.ops, node.comparators):
self.write(get_op_symbol(op, ' %s '), right)
def visit_AugAssign(self, node):
set_precedence(node, node.value, node.target)
self.statement(node, node.target, get_op_symbol(node.op, ' %s= '),
node.value)