@@ -265,7 +265,7 @@ class TableSchemaFormatter(BaseFormatter):
265
265
266
266
267
267
def format_object_summary (obj , formatter , is_justify = True , name = None ,
268
- indent_for_name = True , is_multi = False ):
268
+ indent_for_name = True , line_break_each_value = False ):
269
269
"""
270
270
Return the formatted obj as a unicode string
271
271
@@ -282,8 +282,10 @@ def format_object_summary(obj, formatter, is_justify=True, name=None,
282
282
indent_for_name : bool, default True
283
283
Whether subsequent lines should be be indented to
284
284
align with the name.
285
- is_multi : bool, default False
286
- Is ``obj`` a :class:`MultiIndex` or not
285
+ line_break_each_value : bool, default False
286
+ If True, inserts a line break for each value of ``obj``.
287
+ If False, only break lines when the a line of values gets wider
288
+ than the display width
287
289
288
290
Returns
289
291
-------
@@ -308,7 +310,11 @@ def format_object_summary(obj, formatter, is_justify=True, name=None,
308
310
space2 = "\n " # space for the opening '['
309
311
310
312
n = len (obj )
311
- sep = ',' if not is_multi else (',\n ' + ' ' * len (name ))
313
+ if not line_break_each_value :
314
+ sep = ','
315
+ else :
316
+ # If we want to align on each value, we need a different separator.
317
+ sep = (',\n ' + ' ' * len (name ))
312
318
max_seq_items = get_option ('display.max_seq_items' ) or n
313
319
314
320
# are we a truncated display
@@ -336,10 +342,10 @@ def best_len(values):
336
342
337
343
if n == 0 :
338
344
summary = '[]{}' .format (close )
339
- elif n == 1 and not is_multi :
345
+ elif n == 1 and not line_break_each_value :
340
346
first = formatter (obj [0 ])
341
347
summary = '[{}]{}' .format (first , close )
342
- elif n == 2 and not is_multi :
348
+ elif n == 2 and not line_break_each_value :
343
349
first = formatter (obj [0 ])
344
350
last = formatter (obj [- 1 ])
345
351
summary = '[{}, {}]{}' .format (first , last , close )
@@ -355,22 +361,31 @@ def best_len(values):
355
361
356
362
# adjust all values to max length if needed
357
363
if is_justify :
358
- head , tail = _justify (head , tail , display_width , best_len ,
359
- is_truncated , is_multi )
360
- if is_multi :
364
+ if line_break_each_value :
365
+ head , tail = _justify (head , tail )
366
+ elif (is_truncated or not (len (', ' .join (head )) < display_width and
367
+ len (', ' .join (tail )) < display_width )):
368
+ max_length = max (best_len (head ), best_len (tail ))
369
+ head = [x .rjust (max_length ) for x in head ]
370
+ tail = [x .rjust (max_length ) for x in tail ]
371
+ # If we are not truncated and we are only a single
372
+ # line, then don't justify
373
+
374
+ if line_break_each_value :
375
+ # truncate vertically if wider than max_space
361
376
max_space = display_width - len (space2 )
362
377
item = tail [0 ]
363
- for i in reversed (range (1 , len (item ) + 1 )):
364
- if len (_pprint_seq (item , max_seq_items = i )) < max_space :
378
+ for max_items in reversed (range (1 , len (item ) + 1 )):
379
+ if len (_pprint_seq (item , max_seq_items = max_items )) < max_space :
365
380
break
366
- head = [_pprint_seq (x , max_seq_items = i ) for x in head ]
367
- tail = [_pprint_seq (x , max_seq_items = i ) for x in tail ]
381
+ head = [_pprint_seq (x , max_seq_items = max_items ) for x in head ]
382
+ tail = [_pprint_seq (x , max_seq_items = max_items ) for x in tail ]
368
383
369
384
summary = ""
370
385
line = space2
371
386
372
- for i in range (len (head )):
373
- word = head [i ] + sep + ' '
387
+ for max_items in range (len (head )):
388
+ word = head [max_items ] + sep + ' '
374
389
summary , line = _extend_line (summary , line , word ,
375
390
display_width , space2 )
376
391
@@ -379,8 +394,8 @@ def best_len(values):
379
394
summary += line .rstrip () + space2 + '...'
380
395
line = space2
381
396
382
- for i in range (len (tail ) - 1 ):
383
- word = tail [i ] + sep + ' '
397
+ for max_items in range (len (tail ) - 1 ):
398
+ word = tail [max_items ] + sep + ' '
384
399
summary , line = _extend_line (summary , line , word ,
385
400
display_width , space2 )
386
401
@@ -394,7 +409,7 @@ def best_len(values):
394
409
close = ']' + close .rstrip (' ' )
395
410
summary += close
396
411
397
- if len (summary ) > (display_width ) or is_multi :
412
+ if len (summary ) > (display_width ) or line_break_each_value :
398
413
summary += space1
399
414
else : # one row
400
415
summary += ' '
@@ -405,50 +420,41 @@ def best_len(values):
405
420
return summary
406
421
407
422
408
- def _justify (head , tail , display_width , best_len ,
409
- is_truncated = False , is_multi = False ):
410
- """
411
- Justify each item in head and tail, so they align properly.
423
+ def _justify (head , tail ):
412
424
"""
413
- if is_multi :
414
- max_length = _max_level_item_length (head + tail )
415
- head = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
416
- for seq in head ]
417
- tail = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
418
- for seq in tail ]
419
- elif (is_truncated or not (len (', ' .join (head )) < display_width and
420
- len (', ' .join (tail )) < display_width )):
421
- max_length = max (best_len (head ), best_len (tail ))
422
- head = [x .rjust (max_length ) for x in head ]
423
- tail = [x .rjust (max_length ) for x in tail ]
424
-
425
- return head , tail
426
-
427
-
428
- def _max_level_item_length (seq ):
429
- """
430
- For each position for the sequences in ``seq``, find the largest length.
431
-
432
- Used for justifying individual values in a :class:`pandas.MultiIndex`.
425
+ Justify each item in each list-like in head and tail, so each item
426
+ right-aligns when the two list-likes are stacked vertically.
433
427
434
428
Parameters
435
429
----------
436
- seq : list-like of list-likes of strings
430
+ head : list-like of list-likes of strings
431
+ tail : list-like of list-likes of strings
437
432
438
433
Returns
439
434
-------
440
- max_length : list of ints
435
+ head : list of tuples of strings
436
+ tail : list of tuples of strings
441
437
442
438
Examples
443
439
--------
444
- >>> _max_level_item_length ([['s ', 'ab'] , ['abc', 'a ']])
445
- [3, 2]
440
+ >>> _justify ([['a ', 'b']] , [[ 'abc', 'abcd ']])
441
+ ([(' a', ' b')], [('abc', 'abcd')])
446
442
"""
447
- max_length = [0 ] * len (seq [0 ])
448
- for inner_seq in seq :
443
+ combined = head + tail # type: Sequence[Sequence[str]]
444
+
445
+ # For each position for the sequences in ``combined``,
446
+ # find the length of the largest string.
447
+ max_length = [0 ] * len (combined [0 ]) # type: List[int]
448
+ for inner_seq in combined :
449
449
length = [len (item ) for item in inner_seq ]
450
450
max_length = [max (x , y ) for x , y in zip (max_length , length )]
451
- return max_length
451
+
452
+ # justify each item in each list-like in head and tail using max_length
453
+ head = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
454
+ for seq in head ]
455
+ tail = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
456
+ for seq in tail ]
457
+ return head , tail
452
458
453
459
454
460
def format_object_attrs (obj ):
0 commit comments