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_Num(self, node,
# constants
new=sys.version_info >= (3, 0)):
with self.delimit(node) as delimiters:
self._handle_numeric_constant(node.n)
# We can leave the delimiters handling in visit_Num
# since this is meant to handle a Python 2.x specific
# issue and ast.Constant exists only in 3.6+
# The Python 2.x compiler merges a unary minus
# with a number. This is a premature optimization
# that we deal with here...
if not new and delimiters.discard:
if not isinstance(node.n, complex) and node.n < 0:
pow_lhs = Precedence.Pow + 1
delimiters.discard = delimiters.pp != pow_lhs
else:
op = self.get__p_op(node)
delimiters.discard = not isinstance(op, ast.USub)
def _handle_string_constant(self, node, value, is_joined=False):
# embedded is used to control when we might want
# to use a triple-quoted string. We determine
# if we are in an assignment and/or in an expression
precedence = self.get__pp(node)
embedded = ((precedence > Precedence.Expr) +
(precedence >= Precedence.Assign))
# Flush any pending newlines, because we're about
# to severely abuse the result list.
self.write('')
result = self.result
# Calculate the string representing the line
# we are working on, up to but not including
# the string we are adding.
res_index, str_index = self.colinfo
current_line = self.result[res_index:]
if str_index:
current_line[0] = current_line[0][str_index:]
current_line = ''.join(current_line)
def __getattr__(self, name, defaults=dict(keywords=(),
_pp=Precedence.highest).get):
""" Get an attribute of the node.
like dict.get (returns None if doesn't exist)
"""
if not name.startswith('get_'):
raise AttributeError
geta = getattr
shortname = name[4:]
default = defaults(shortname)
def getter(node):
return geta(node, shortname, default)
setattr(self, name, getter)
return getter
def _handle_string_constant(self, node, value, is_joined=False):
# embedded is used to control when we might want
# to use a triple-quoted string. We determine
# if we are in an assignment and/or in an expression
precedence = self.get__pp(node)
embedded = ((precedence > Precedence.Expr) +
(precedence >= Precedence.Assign))
# Flush any pending newlines, because we're about
# to severely abuse the result list.
self.write('')
result = self.result
# Calculate the string representing the line
# we are working on, up to but not including
# the string we are adding.
res_index, str_index = self.colinfo
current_line = self.result[res_index:]
if str_index:
current_line[0] = current_line[0][str_index:]
current_line = ''.join(current_line)
def loop_args(args, defaults):
set_precedence(Precedence.Comma, defaults)
padding = [None] * (len(args) - len(defaults))
for arg, default in zip(args, padding + defaults):
self.write(write_comma, arg)
self.conditional_write('=', default)