@@ -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,9 +355,15 @@ 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+
366+ if line_break_each_value :
355367 max_space = display_width - len (space2 )
356368 item = tail [0 ]
357369 for i in reversed (range (1 , len (item ) + 1 )):
@@ -384,7 +396,7 @@ def best_len(values):
384396 summary += line
385397 summary += '],'
386398
387- if len (summary ) > (display_width ) or is_multi :
399+ if len (summary ) > (display_width ) or line_break_each_value :
388400 summary += space1
389401 else : # one row
390402 summary += ' '
@@ -395,23 +407,40 @@ def best_len(values):
395407 return summary
396408
397409
398- def _justify (head , tail , display_width , best_len ,
399- is_truncated = False , is_multi = False ):
410+ def _justify (head , tail ):
400411 """
401- Justify each item in head and tail, so they align properly.
412+ Justify each item in each list-like in head and tail, so each item
413+ right-aligns when the two list-likes are stacked vertically.
414+
415+ Parameters
416+ ----------
417+ head : list-like of list-likes of strings
418+ tail : list-like of list-likes of strings
419+
420+ Returns
421+ -------
422+ head : list of tuples of strings
423+ tail : list of tuples of strings
424+
425+ Examples
426+ --------
427+ >>> _justify([['a', 'b']], [['abc', 'abcd']])
428+ ([(' a', ' b')], [('abc', 'abcd')])
402429 """
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 ]
430+ combined = head + tail # type: List[str]
431+
432+ # For each position for the sequences in ``combined``,
433+ # find the length of the largest string.
434+ max_length = [0 ] * len (combined [0 ]) # type: List[int]
435+ for inner_seq in combined :
436+ length = [len (item ) for item in inner_seq ]
437+ max_length = [max (x , y ) for x , y in zip (max_length , length )]
414438
439+ # justify each item in each list-like in head and tail using max_length
440+ head = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
441+ for seq in head ]
442+ tail = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
443+ for seq in tail ]
415444 return head , tail
416445
417446
0 commit comments