def _Tuple(self, t): self.write("(") if len(t.elts) == 1: elt = t.elts[0] self.dispatch(elt) self.write(",") else: interleave(lambda: self.write(","), self.dispatch, t.elts) self.write(")")
def _ImportFrom(self, t): # A from __future__ import may affect unparsing, so record it. if t.module and t.module == '__future__': self.future_imports.extend(n.name for n in t.names) self.fill('from ') self.write('.' * t.level) if t.module: self.write(t.module) self.write(' import ') interleave(lambda: self.write(','), self.dispatch, t.names)
def _generic_With(self, t, async_=False): self.fill("async with " if async_ else "with ") if hasattr(t, 'items'): interleave(lambda: self.write(","), self.dispatch, t.items) else: self.dispatch(t.context_expr) if t.optional_vars: self.write(" as ") self.dispatch(t.optional_vars) self.enter() self.dispatch(t.body) self.leave()
def _Dict(self, t): self.write("{") def write_key_value_pair(k, v): self.dispatch(k) self.write(":") self.dispatch(v) def write_item(item): k, v = item if k is None: # for dictionary unpacking operator in dicts {**{'y': 2}} # see PEP 448 for details self.write("**") self.dispatch(v) else: write_key_value_pair(k, v) interleave(lambda: self.write(","), write_item, zip(t.keys, t.values)) self.write("}")
def _Dict(self, t): self.write("{") noncomment_keys = [] comment_groups = [] comment_group = [] _LOG.info('keys : %s', t.keys) _LOG.info('values: %s', t.values) for key in t.keys: if isinstance(key, Comment): comment_group.append(key) continue comment_groups.append(comment_group) comment_group = [] noncomment_keys.append(key) noncomment_values = [] comment_groups_iter = iter(comment_groups) try: comment_group = next(comment_groups_iter) except StopIteration: comment_group = None for value in t.values: if isinstance(value, Comment): comment_group.append(value) continue noncomment_values.append(value) try: comment_group = next(comment_groups_iter) except StopIteration: comment_group = None def write_triple(triple): (k, v, comments) = triple for comment in comments: self.dispatch(comment) self.dispatch(k) self.write(": ") self.dispatch(v) interleave(lambda: self.write(", "), write_triple, zip(noncomment_keys, noncomment_values, comment_groups)) self.write("}")
def _generic_With(self, t, async_=False): """Unparse With or AsyncWith node. Rather than handling just: With/AsyncWith(withitem* items, stmt* body) handle: With/AsyncWith(withitem* items, stmt* body, string? type_comment) """ if not hasattr(t, 'type_comment') or t.type_comment is None: super()._generic_With(t, async_) return self.fill("async with " if async_ else "with ") interleave(lambda: self.write(", "), self.dispatch, t.items) self.enter() self._write_type_comment(t.type_comment) self.dispatch(t.body) self.leave()
def _BoolOp(self, syntax): # TODO: push this to astunparse (upstream) self.write('(') op_ = ' {} '.format(self.boolops[syntax.op.__class__.__name__]) interleave(lambda: self.write(op_), self.dispatch, syntax.values) self.write(')')
def _Delete(self, t): self.fill("del ") interleave(lambda: self.write(","), self.dispatch, t.targets)
def _ExtSlice(self, t): interleave(lambda: self.write(','), self.dispatch, t.dims)
def _Import(self, t): self.fill('import ') interleave(lambda: self.write(','), self.dispatch, t.names)
def _List(self, t): self.write("[") interleave(lambda: self.write(","), self.dispatch, t.elts) self.write("]")
def _Nonlocal(self, t): self.fill("nonlocal ") interleave(lambda: self.write(","), self.write, t.names)
def _Global(self, t): self.fill("global ") interleave(lambda: self.write(","), self.write, t.names)
"""Unparse With or AsyncWith node. Rather than handling just: With/AsyncWith(withitem* items, stmt* body) handle: With/AsyncWith(withitem* items, stmt* body, string? type_comment) """ if not hasattr(t, 'type_comment') or t.type_comment is None: super()._generic_With(t, async) return self.fill("async with " if async else "with ") interleave(lambda: self.write(", "), self.dispatch, t.items) self.enter() self._write_type_comment(t.type_comment) self.dispatch(t.body) self.leave() def _FormattedValue(self, t): self.write("{") self.dispatch(t.value) if t.conversion is not None and t.conversion != -1: self.write("!") self.write(self.format_conversions[t.conversion]) if t.format_spec is not None: self.write(":") if isinstance(t.format_spec, ast.Str) or isinstance( t.format_spec, typed_ast.ast3.Str):