@@ -269,7 +269,7 @@ class TableSchemaFormatter(BaseFormatter):
269269
270270
271271def format_object_summary (obj , formatter , is_justify = True ,
272- name = None , is_multi = False ):
272+ name = None , line_break_each_value = False ):
273273 """
274274 Return the formatted obj as a unicode string
275275
@@ -283,8 +283,10 @@ def format_object_summary(obj, formatter, is_justify=True,
283283 should justify the display
284284 name : name, optional
285285 defaults to the class name of the obj
286- is_multi : bool, default False
287- Is ``obj`` a :class:`MultiIndex` or not
286+ line_break_each_value : bool, default False
287+ If True, inserts a line break for each value of ``obj``.
288+ If False, only break lines when the a line of values gets wider
289+ than the display width
288290
289291 Returns
290292 -------
@@ -304,7 +306,11 @@ def format_object_summary(obj, formatter, is_justify=True,
304306 space2 = "\n %s" % (' ' * (len (name ) + 2 ))
305307
306308 n = len (obj )
307- sep = ',' if not is_multi else (',\n ' + ' ' * len (name ))
309+ if not line_break_each_value :
310+ sep = ','
311+ else :
312+ # If we want to align on each value, we need a different separator.
313+ sep = (',\n ' + ' ' * len (name ))
308314 max_seq_items = get_option ('display.max_seq_items' ) or n
309315
310316 # are we a truncated display
@@ -330,10 +336,10 @@ def best_len(values):
330336
331337 if n == 0 :
332338 summary = '[], '
333- elif n == 1 and not is_multi :
339+ elif n == 1 and not line_break_each_value :
334340 first = formatter (obj [0 ])
335341 summary = '[%s], ' % first
336- elif n == 2 and not is_multi :
342+ elif n == 2 and not line_break_each_value :
337343 first = formatter (obj [0 ])
338344 last = formatter (obj [- 1 ])
339345 summary = '[%s, %s], ' % (first , last )
@@ -349,22 +355,31 @@ def best_len(values):
349355
350356 # adjust all values to max length if needed
351357 if is_justify :
352- head , tail = _justify (head , tail , display_width , best_len ,
353- is_truncated , is_multi )
354- if is_multi :
358+ if line_break_each_value :
359+ head , tail = _justify (head , tail )
360+ elif (is_truncated or not (len (', ' .join (head )) < display_width and
361+ len (', ' .join (tail )) < display_width )):
362+ max_length = max (best_len (head ), best_len (tail ))
363+ head = [x .rjust (max_length ) for x in head ]
364+ tail = [x .rjust (max_length ) for x in tail ]
365+ # If we are not truncated and we are only a single
366+ # line, then don't justify
367+
368+ if line_break_each_value :
369+ # truncate vertically if wider than max_space
355370 max_space = display_width - len (space2 )
356371 item = tail [0 ]
357- for i in reversed (range (1 , len (item ) + 1 )):
358- if len (_pprint_seq (item , max_seq_items = i )) < max_space :
372+ for max_items in reversed (range (1 , len (item ) + 1 )):
373+ if len (_pprint_seq (item , max_seq_items = max_items )) < max_space :
359374 break
360- head = [_pprint_seq (x , max_seq_items = i ) for x in head ]
361- tail = [_pprint_seq (x , max_seq_items = i ) for x in tail ]
375+ head = [_pprint_seq (x , max_seq_items = max_items ) for x in head ]
376+ tail = [_pprint_seq (x , max_seq_items = max_items ) for x in tail ]
362377
363378 summary = ""
364379 line = space2
365380
366- for i in range (len (head )):
367- word = head [i ] + sep + ' '
381+ for max_items in range (len (head )):
382+ word = head [max_items ] + sep + ' '
368383 summary , line = _extend_line (summary , line , word ,
369384 display_width , space2 )
370385
@@ -373,8 +388,8 @@ def best_len(values):
373388 summary += line .rstrip () + space2 + '...'
374389 line = space2
375390
376- for i in range (len (tail ) - 1 ):
377- word = tail [i ] + sep + ' '
391+ for max_items in range (len (tail ) - 1 ):
392+ word = tail [max_items ] + sep + ' '
378393 summary , line = _extend_line (summary , line , word ,
379394 display_width , space2 )
380395
@@ -384,7 +399,7 @@ def best_len(values):
384399 summary += line
385400 summary += '],'
386401
387- if len (summary ) > (display_width ) or is_multi :
402+ if len (summary ) > (display_width ) or line_break_each_value :
388403 summary += space1
389404 else : # one row
390405 summary += ' '
@@ -395,50 +410,41 @@ def best_len(values):
395410 return summary
396411
397412
398- def _justify (head , tail , display_width , best_len ,
399- is_truncated = False , is_multi = False ):
400- """
401- Justify each item in head and tail, so they align properly.
413+ def _justify (head , tail ):
402414 """
403- if is_multi :
404- max_length = _max_level_item_length (head + tail )
405- head = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
406- for seq in head ]
407- tail = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
408- for seq in tail ]
409- elif (is_truncated or not (len (', ' .join (head )) < display_width and
410- len (', ' .join (tail )) < display_width )):
411- max_length = max (best_len (head ), best_len (tail ))
412- head = [x .rjust (max_length ) for x in head ]
413- tail = [x .rjust (max_length ) for x in tail ]
414-
415- return head , tail
416-
417-
418- def _max_level_item_length (seq ):
419- """
420- For each position for the sequences in ``seq``, find the largest length.
421-
422- Used for justifying individual values in a :class:`pandas.MultiIndex`.
415+ Justify each item in each list-like in head and tail, so each item
416+ right-aligns when the two list-likes are stacked vertically.
423417
424418 Parameters
425419 ----------
426- seq : list-like of list-likes of strings
420+ head : list-like of list-likes of strings
421+ tail : list-like of list-likes of strings
427422
428423 Returns
429424 -------
430- max_length : list of ints
425+ head : list of tuples of strings
426+ tail : list of tuples of strings
431427
432428 Examples
433429 --------
434- >>> _max_level_item_length ([['s ', 'ab'] , ['abc', 'a ']])
435- [3, 2]
430+ >>> _justify ([['a ', 'b']] , [[ 'abc', 'abcd ']])
431+ ([(' a', ' b')], [('abc', 'abcd')])
436432 """
437- max_length = [0 ] * len (seq [0 ])
438- for inner_seq in seq :
433+ combined = head + tail # type: Sequence[Sequence[str]]
434+
435+ # For each position for the sequences in ``combined``,
436+ # find the length of the largest string.
437+ max_length = [0 ] * len (combined [0 ]) # type: List[int]
438+ for inner_seq in combined :
439439 length = [len (item ) for item in inner_seq ]
440440 max_length = [max (x , y ) for x , y in zip (max_length , length )]
441- return max_length
441+
442+ # justify each item in each list-like in head and tail using max_length
443+ head = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
444+ for seq in head ]
445+ tail = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
446+ for seq in tail ]
447+ return head , tail
442448
443449
444450def format_object_attrs (obj ):
0 commit comments