def maybe_replace(self, chunk, contract_lines, converted, rest): if contract_lines: if get_quote_type(str(chunk)) in (qt.triple_double, qt.triple_single): lines = converted.split("\\n") lines[-1] += rest lines_fit = all( len(l) <= self.len_limit - chunk.start_idx for l in lines ) converted = converted.replace("\\n", "\n") else: lines_fit = ( len("".join([converted, rest])) <= self.len_limit - chunk.start_idx ) else: lines_fit = True if not contract_lines or lines_fit: self.results.append(converted) self.count_expressions += 1 self.last_line += contract_lines self.last_idx = chunk.end_idx # remove redundant parenthesis if len(self.results) < 2 or not self.results[-2]: return elif len(self.src_lines[self.last_line]) == self.last_idx: return if ( self.results[-2][-1] == "(" and self.src_lines[self.last_line][self.last_idx] == ")" ): for char in reversed(self.results[-2][:-1]): if char in string.whitespace: continue elif char in "(=[+*": break else: return self.results[-2] = self.results[-2][:-1] self.last_idx += 1
def maybe_replace(self, chunk, contract_lines, converted, rest): if contract_lines: if get_quote_type(str(chunk)) in (qt.triple_double, qt.triple_single): lines = converted.split("\\n") lines[-1] += rest lines_fit = all( len(l) <= self.len_limit - chunk.start_idx for l in lines) converted = converted.replace("\\n", "\n") else: lines_fit = (len("".join([converted, rest])) <= self.len_limit - chunk.start_idx) else: lines_fit = True if not contract_lines or lines_fit: self.results.append(converted) self.count_expressions += 1 self.last_line += contract_lines self.last_idx = chunk.end_idx
def test_initial_doesnt_matter(quote_type): code = random.choice(["'abra'", '"bobro"', "'''abra'''", '"""bobro"""']) assert get_quote_type(set_quote_type(code, quote_type)) == quote_type
def test_cycle(code): assert set_quote_type(code, get_quote_type(code)) == code
def test_get_quote_type(code, quote_type): assert get_quote_type(code) == quote_type