Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(8)

Delta Between Two Patch Sets: Lib/pprint.py

Issue 19105: pprint doesn't use all width
Left Patch Set: Created 5 years, 1 month ago
Right Patch Set: Created 5 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Lib/test/test_pprint.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # Author: Fred L. Drake, Jr. 1 # Author: Fred L. Drake, Jr.
2 # fdrake@acm.org 2 # fdrake@acm.org
3 # 3 #
4 # This is a simple little module I wrote to make life easier. I didn't 4 # This is a simple little module I wrote to make life easier. I didn't
5 # see anything quite like it in the library, though I may have overlooked 5 # see anything quite like it in the library, though I may have overlooked
6 # something. I wrote this when I was trying to read some heavily nested 6 # something. I wrote this when I was trying to read some heavily nested
7 # tuples with fairly non-descriptive content. This is modeled very much 7 # tuples with fairly non-descriptive content. This is modeled very much
8 # after Lisp/Scheme - style pretty-printing of lists. If you find it 8 # after Lisp/Scheme - style pretty-printing of lists. If you find it
9 # useful, thank small children who sleep at night. 9 # useful, thank small children who sleep at night.
10 10
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 context[objid] = 1 220 context[objid] = 1
221 self._format_items(object, stream, 221 self._format_items(object, stream,
222 indent + self._indent_per_level, 222 indent + self._indent_per_level,
223 allowance + len(endchar), 223 allowance + len(endchar),
224 context, level) 224 context, level)
225 del context[objid] 225 del context[objid]
226 write(endchar) 226 write(endchar)
227 return 227 return
228 228
229 if issubclass(typ, str) and len(object) > 0 and r is str.__repr__: 229 if issubclass(typ, str) and len(object) > 0 and r is str.__repr__:
230 def _str_parts(s): 230 chunks = []
231 """ 231 lines = object.splitlines(True)
232 Return a list of string literals comprising the repr() 232 if level == 1:
233 of the given string using literal concatenation. 233 indent += 1
234 """ 234 allowance += 1
235 max_width1 = max_width = self._width - indent 235 max_width1 = max_width = self._width - indent
236 lines = s.splitlines(True) 236 for i, line in enumerate(lines):
237 for i, line in enumerate(lines): 237 rep = repr(line)
238 rep = repr(line) 238 if i == len(lines) - 1:
239 if i == len(lines) - 1: 239 max_width1 -= allowance
240 max_width1 -= allowance 240 if len(rep) <= max_width1:
241 if len(rep) <= max_width1: 241 chunks.append(rep)
242 yield rep 242 else:
243 else: 243 # A list of alternating (non-space, space) strings
244 # A list of alternating (non-space, space) strings 244 parts = re.findall(r'\S*\s*', line)
245 parts = re.findall(r'\S*\s*', line) 245 assert parts
246 assert parts 246 assert not parts[-1]
247 assert not parts[-1] 247 parts.pop() # drop empty last part
248 parts.pop() # drop empty last part 248 max_width2 = max_width
249 max_width2 = max_width 249 current = ''
250 current = '' 250 for j, part in enumerate(parts):
251 for j, part in enumerate(parts): 251 candidate = current + part
252 candidate = current + part 252 if j == len(parts) - 1 and i == len(lines) - 1:
253 if j == len(parts) - 1 and i == len(lines) - 1: 253 max_width2 -= allowance
254 max_width2 -= allowance 254 if len(repr(candidate)) > max_width2:
255 if len(repr(candidate)) > max_width2: 255 if current:
256 if current: 256 chunks.append(repr(current))
257 yield repr(current) 257 current = part
258 current = part 258 else:
259 else: 259 current = candidate
260 current = candidate 260 if current:
261 if current: 261 chunks.append(repr(current))
262 yield repr(current) 262 if len(chunks) == 1:
263 for i, rep in enumerate(_str_parts(object)): 263 write(rep)
264 return
265 if level == 1:
266 write('(')
267 for i, rep in enumerate(chunks):
264 if i > 0: 268 if i > 0:
265 write('\n' + ' '*indent) 269 write('\n' + ' '*indent)
266 write(rep) 270 write(rep)
271 if level == 1:
272 write(')')
267 return 273 return
268 write(rep) 274 write(rep)
269 275
270 def _format_dict_items(self, items, stream, indent, allowance, context, 276 def _format_dict_items(self, items, stream, indent, allowance, context,
271 level): 277 level):
272 write = stream.write 278 write = stream.write
273 delimnl = ',\n' + ' ' * indent 279 delimnl = ',\n' + ' ' * indent
274 last_index = len(items) - 1 280 last_index = len(items) - 1
275 for i, (key, ent) in enumerate(items): 281 for i, (key, ent) in enumerate(items):
276 last = i == last_index 282 last = i == last_index
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 t1 = time.time() 444 t1 = time.time()
439 _safe_repr(object, {}, None, 0) 445 _safe_repr(object, {}, None, 0)
440 t2 = time.time() 446 t2 = time.time()
441 p.pformat(object) 447 p.pformat(object)
442 t3 = time.time() 448 t3 = time.time()
443 print("_safe_repr:", t2 - t1) 449 print("_safe_repr:", t2 - t1)
444 print("pformat:", t3 - t2) 450 print("pformat:", t3 - t2)
445 451
446 if __name__ == "__main__": 452 if __name__ == "__main__":
447 _perfcheck() 453 _perfcheck()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+