MCPcopy
hub / github.com/pandas-dev/pandas / _justify

Function _justify

pandas/io/formats/printing.py:471–511  ·  view source on GitHub ↗

Justify items in head and tail, so they are right-aligned when stacked. Parameters ---------- head : list-like of list-likes of strings tail : list-like of list-likes of strings Returns ------- tuple of list of tuples of strings Same as head and tail, but i

(
    head: list[Sequence[str]], tail: list[Sequence[str]]
)

Source from the content-addressed store, hash-verified

469
470
471def _justify(
472 head: list[Sequence[str]], tail: list[Sequence[str]]
473) -> tuple[list[tuple[str, ...]], list[tuple[str, ...]]]:
474 """
475 Justify items in head and tail, so they are right-aligned when stacked.
476
477 Parameters
478 ----------
479 head : list-like of list-likes of strings
480 tail : list-like of list-likes of strings
481
482 Returns
483 -------
484 tuple of list of tuples of strings
485 Same as head and tail, but items are right aligned when stacked
486 vertically.
487
488 Examples
489 --------
490 >>> _justify([["a", "b"]], [["abc", "abcd"]])
491 ([(' a', ' b')], [('abc', 'abcd')])
492 """
493 combined = head + tail
494
495 # For each position for the sequences in ``combined``,
496 # find the length of the largest string.
497 max_length = [0] * len(combined[0])
498 for inner_seq in combined:
499 length = [len(item) for item in inner_seq]
500 max_length = [max(x, y) for x, y in zip(max_length, length, strict=True)]
501
502 # justify each item in each list-like in head and tail using max_length
503 head_tuples = [
504 tuple(x.rjust(max_len) for x, max_len in zip(seq, max_length, strict=True))
505 for seq in head
506 ]
507 tail_tuples = [
508 tuple(x.rjust(max_len) for x, max_len in zip(seq, max_length, strict=True))
509 for seq in tail
510 ]
511 return head_tuples, tail_tuples
512
513
514class PrettyDict(dict[_KT, _VT]):

Callers 1

format_object_summaryFunction · 0.85

Calls 2

maxFunction · 0.85
rjustMethod · 0.80

Tested by

no test coverage detected