@@ -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,22 +355,31 @@ 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
+ # 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
355
370
max_space = display_width - len (space2 )
356
371
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 :
359
374
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 ]
362
377
363
378
summary = ""
364
379
line = space2
365
380
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 + ' '
368
383
summary , line = _extend_line (summary , line , word ,
369
384
display_width , space2 )
370
385
@@ -373,8 +388,8 @@ def best_len(values):
373
388
summary += line .rstrip () + space2 + '...'
374
389
line = space2
375
390
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 + ' '
378
393
summary , line = _extend_line (summary , line , word ,
379
394
display_width , space2 )
380
395
@@ -384,7 +399,7 @@ def best_len(values):
384
399
summary += line
385
400
summary += '],'
386
401
387
- if len (summary ) > (display_width ) or is_multi :
402
+ if len (summary ) > (display_width ) or line_break_each_value :
388
403
summary += space1
389
404
else : # one row
390
405
summary += ' '
@@ -395,50 +410,41 @@ def best_len(values):
395
410
return summary
396
411
397
412
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 ):
402
414
"""
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.
423
417
424
418
Parameters
425
419
----------
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
427
422
428
423
Returns
429
424
-------
430
- max_length : list of ints
425
+ head : list of tuples of strings
426
+ tail : list of tuples of strings
431
427
432
428
Examples
433
429
--------
434
- >>> _max_level_item_length ([['s ', 'ab'] , ['abc', 'a ']])
435
- [3, 2]
430
+ >>> _justify ([['a ', 'b']] , [[ 'abc', 'abcd ']])
431
+ ([(' a', ' b')], [('abc', 'abcd')])
436
432
"""
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 :
439
439
length = [len (item ) for item in inner_seq ]
440
440
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
442
448
443
449
444
450
def format_object_attrs (obj ):
0 commit comments