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]]
)
| 469 | |
| 470 | |
| 471 | def _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 | |
| 514 | class PrettyDict(dict[_KT, _VT]): |
no test coverage detected