@@ -269,7 +269,7 @@ class TableSchemaFormatter(BaseFormatter):
269
269
270
270
271
271
def format_object_summary (obj , formatter , is_justify = True ,
272
- name = None , is_multi = False ):
272
+ name = None , line_break_each_value = False ):
273
273
"""
274
274
Return the formatted obj as a unicode string
275
275
@@ -283,8 +283,10 @@ def format_object_summary(obj, formatter, is_justify=True,
283
283
should justify the display
284
284
name : name, optional
285
285
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
288
290
289
291
Returns
290
292
-------
@@ -304,7 +306,7 @@ def format_object_summary(obj, formatter, is_justify=True,
304
306
space2 = "\n %s" % (' ' * (len (name ) + 2 ))
305
307
306
308
n = len (obj )
307
- sep = ',' if not is_multi else (',\n ' + ' ' * len (name ))
309
+ sep = ',' if not line_break_each_value else (',\n ' + ' ' * len (name ))
308
310
max_seq_items = get_option ('display.max_seq_items' ) or n
309
311
310
312
# are we a truncated display
@@ -330,10 +332,10 @@ def best_len(values):
330
332
331
333
if n == 0 :
332
334
summary = '[], '
333
- elif n == 1 and not is_multi :
335
+ elif n == 1 and not line_break_each_value :
334
336
first = formatter (obj [0 ])
335
337
summary = '[%s], ' % first
336
- elif n == 2 and not is_multi :
338
+ elif n == 2 and not line_break_each_value :
337
339
first = formatter (obj [0 ])
338
340
last = formatter (obj [- 1 ])
339
341
summary = '[%s, %s], ' % (first , last )
@@ -349,9 +351,15 @@ def best_len(values):
349
351
350
352
# adjust all values to max length if needed
351
353
if is_justify :
352
- head , tail = _justify (head , tail , display_width , best_len ,
353
- is_truncated , is_multi )
354
- if is_multi :
354
+ if line_break_each_value :
355
+ head , tail = _justify (head , tail )
356
+ elif (is_truncated or not (len (', ' .join (head )) < display_width and
357
+ len (', ' .join (tail )) < display_width )):
358
+ max_length = max (best_len (head ), best_len (tail ))
359
+ head = [x .rjust (max_length ) for x in head ]
360
+ tail = [x .rjust (max_length ) for x in tail ]
361
+
362
+ if line_break_each_value :
355
363
max_space = display_width - len (space2 )
356
364
item = tail [0 ]
357
365
for i in reversed (range (1 , len (item ) + 1 )):
@@ -384,7 +392,7 @@ def best_len(values):
384
392
summary += line
385
393
summary += '],'
386
394
387
- if len (summary ) > (display_width ) or is_multi :
395
+ if len (summary ) > (display_width ) or line_break_each_value :
388
396
summary += space1
389
397
else : # one row
390
398
summary += ' '
@@ -395,23 +403,38 @@ def best_len(values):
395
403
return summary
396
404
397
405
398
- def _justify (head , tail , display_width , best_len ,
399
- is_truncated = False , is_multi = False ):
406
+ def _justify (head , tail ):
400
407
"""
401
- Justify each item in head and tail, so they align properly.
408
+ Justify each item in each list-like in head and tail, so each item
409
+ right-aligns when the two list-likes are stacked vertically.
410
+
411
+ Parameters
412
+ ----------
413
+ head : list-like of list-likes of strings
414
+ tail : list-like of list-likes of strings
415
+
416
+ Returns
417
+ -------
418
+ head : list of tuples of strings
419
+ tail : list of tuples of strings
420
+
421
+ Examples
422
+ --------
423
+ >>> _justify([['a', 'b']], [['abc', 'abcd']])
424
+ ([(' a', ' b')], [('abc', 'abcd')])
402
425
"""
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 ]
426
+ seq = head + tail # type: List[str]
427
+ # For each position for the sequences in ``seq``, find the largest length.
428
+ max_length = [0 ] * len (seq [0 ]) # type: List[int]
429
+ for inner_seq in seq :
430
+ length = [len (item ) for item in inner_seq ]
431
+ max_length = [max (x , y ) for x , y in zip (max_length , length )]
414
432
433
+ # justify each item in each list-like in head and tail using max_length
434
+ head = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
435
+ for seq in head ]
436
+ tail = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
437
+ for seq in tail ]
415
438
return head , tail
416
439
417
440
0 commit comments