@@ -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,11 @@ 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
+ 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 ))
308
314
max_seq_items = get_option ('display.max_seq_items' ) or n
309
315
310
316
# are we a truncated display
@@ -330,10 +336,10 @@ def best_len(values):
330
336
331
337
if n == 0 :
332
338
summary = '[], '
333
- elif n == 1 and not is_multi :
339
+ elif n == 1 and not line_break_each_value :
334
340
first = formatter (obj [0 ])
335
341
summary = '[%s], ' % first
336
- elif n == 2 and not is_multi :
342
+ elif n == 2 and not line_break_each_value :
337
343
first = formatter (obj [0 ])
338
344
last = formatter (obj [- 1 ])
339
345
summary = '[%s, %s], ' % (first , last )
@@ -349,9 +355,15 @@ def best_len(values):
349
355
350
356
# adjust all values to max length if needed
351
357
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 :
355
367
max_space = display_width - len (space2 )
356
368
item = tail [0 ]
357
369
for i in reversed (range (1 , len (item ) + 1 )):
@@ -384,7 +396,7 @@ def best_len(values):
384
396
summary += line
385
397
summary += '],'
386
398
387
- if len (summary ) > (display_width ) or is_multi :
399
+ if len (summary ) > (display_width ) or line_break_each_value :
388
400
summary += space1
389
401
else : # one row
390
402
summary += ' '
@@ -395,23 +407,40 @@ def best_len(values):
395
407
return summary
396
408
397
409
398
- def _justify (head , tail , display_width , best_len ,
399
- is_truncated = False , is_multi = False ):
410
+ def _justify (head , tail ):
400
411
"""
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')])
402
429
"""
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 )]
414
438
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 ]
415
444
return head , tail
416
445
417
446
0 commit comments