Show More
@@ -618,25 +618,30 b' class DollarFormatter(FullEvalFormatter):' | |||||
618 | # Utils to columnize a list of string |
|
618 | # Utils to columnize a list of string | |
619 | #----------------------------------------------------------------------------- |
|
619 | #----------------------------------------------------------------------------- | |
620 |
|
620 | |||
621 | def _chunks(l, n): |
|
621 | def _col_chunks(l, nrows, row_first=False): | |
622 | """Yield successive n-sized chunks from l.""" |
|
622 | """Yield successive nrows-sized column chunks from l.""" | |
623 | for i in py3compat.xrange(0, len(l), n): |
|
623 | if row_first: | |
624 | yield l[i:i+n] |
|
624 | ncols = (len(l) // nrows) + (len(l) % nrows > 0) | |
|
625 | for i in py3compat.xrange(ncols): | |||
|
626 | yield [l[j] for j in py3compat.xrange(i, len(l), nrows)] | |||
|
627 | else: | |||
|
628 | for i in py3compat.xrange(0, len(l), nrows): | |||
|
629 | yield l[i:(i + nrows)] | |||
625 |
|
630 | |||
626 |
|
631 | |||
627 |
def _find_optimal(rlist , separator_size=2 |
|
632 | def _find_optimal(rlist, row_first=False, separator_size=2, displaywidth=80): | |
628 | """Calculate optimal info to columnize a list of string""" |
|
633 | """Calculate optimal info to columnize a list of string""" | |
629 |
for nrow in range(1, len(rlist)+1) |
|
634 | for nrow in range(1, len(rlist) + 1): | |
630 |
c |
|
635 | col_widths = list(map(max, _col_chunks(rlist, nrow, row_first))) | |
631 |
sumlength = sum(c |
|
636 | sumlength = sum(col_widths) | |
632 |
ncols = len(c |
|
637 | ncols = len(col_widths) | |
633 |
if sumlength+separator_size*(ncols-1) <= displaywidth |
|
638 | if sumlength + separator_size * (ncols - 1) <= displaywidth: | |
634 |
break |
|
639 | break | |
635 |
return {'columns |
|
640 | return {'num_columns': ncols, | |
636 | 'optimal_separator_width':(displaywidth - sumlength)/(ncols-1) if (ncols -1) else 0, |
|
641 | 'optimal_separator_width': (displaywidth - sumlength) / (ncols - 1) if (ncols - 1) else 0, | |
637 |
' |
|
642 | 'num_rows': nrow, | |
638 |
'column |
|
643 | 'column_widths': col_widths | |
639 | } |
|
644 | } | |
640 |
|
645 | |||
641 |
|
646 | |||
642 | def _get_or_default(mylist, i, default=None): |
|
647 | def _get_or_default(mylist, i, default=None): | |
@@ -647,7 +652,7 b' def _get_or_default(mylist, i, default=None):' | |||||
647 | return mylist[i] |
|
652 | return mylist[i] | |
648 |
|
653 | |||
649 |
|
654 | |||
650 | def compute_item_matrix(items, empty=None, *args, **kwargs) : |
|
655 | def compute_item_matrix(items, row_first=False, empty=None, *args, **kwargs) : | |
651 | """Returns a nested list, and info to columnize items |
|
656 | """Returns a nested list, and info to columnize items | |
652 |
|
657 | |||
653 | Parameters |
|
658 | Parameters | |
@@ -655,6 +660,9 b' def compute_item_matrix(items, empty=None, *args, **kwargs) :' | |||||
655 |
|
660 | |||
656 | items |
|
661 | items | |
657 | list of strings to columize |
|
662 | list of strings to columize | |
|
663 | row_first : (default False) | |||
|
664 | Whether to to compute columns for a row-first matrix instead ofr | |||
|
665 | column-first (default). | |||
658 | empty : (default None) |
|
666 | empty : (default None) | |
659 | default value to fill list if needed |
|
667 | default value to fill list if needed | |
660 | separator_size : int (default=2) |
|
668 | separator_size : int (default=2) | |
@@ -675,11 +683,11 b' def compute_item_matrix(items, empty=None, *args, **kwargs) :' | |||||
675 | dict_info |
|
683 | dict_info | |
676 | some info to make columnize easier: |
|
684 | some info to make columnize easier: | |
677 |
|
685 | |||
678 |
columns |
|
686 | num_columns | |
679 | number of columns |
|
687 | number of columns | |
680 |
|
|
688 | num_rows | |
681 | number of rows |
|
689 | number of rows | |
682 |
column |
|
690 | column_widths | |
683 | list of with of each columns |
|
691 | list of with of each columns | |
684 | optimal_separator_width |
|
692 | optimal_separator_width | |
685 | best separator width between columns |
|
693 | best separator width between columns | |
@@ -689,24 +697,27 b' def compute_item_matrix(items, empty=None, *args, **kwargs) :' | |||||
689 | :: |
|
697 | :: | |
690 |
|
698 | |||
691 | In [1]: l = ['aaa','b','cc','d','eeeee','f','g','h','i','j','k','l'] |
|
699 | In [1]: l = ['aaa','b','cc','d','eeeee','f','g','h','i','j','k','l'] | |
692 | ...: compute_item_matrix(l,displaywidth=12) |
|
700 | ...: compute_item_matrix(l, displaywidth=12) | |
693 | Out[1]: |
|
701 | Out[1]: | |
694 | ([['aaa', 'f', 'k'], |
|
702 | ([['aaa', 'f', 'k'], | |
695 | ['b', 'g', 'l'], |
|
703 | ['b', 'g', 'l'], | |
696 | ['cc', 'h', None], |
|
704 | ['cc', 'h', None], | |
697 | ['d', 'i', None], |
|
705 | ['d', 'i', None], | |
698 | ['eeeee', 'j', None]], |
|
706 | ['eeeee', 'j', None]], | |
699 |
{'column |
|
707 | {'num_columns': 3, | |
700 |
'column |
|
708 | 'column_widths': [5, 1, 1], | |
701 | 'optimal_separator_width': 2, |
|
709 | 'optimal_separator_width': 2, | |
702 |
' |
|
710 | 'num_rows': 5}) | |
703 | """ |
|
711 | """ | |
704 | info = _find_optimal(list(map(len, items)), *args, **kwargs) |
|
712 | info = _find_optimal(list(map(len, items)), row_first, *args, **kwargs) | |
705 |
nrow, ncol = info[' |
|
713 | nrow, ncol = info['num_rows'], info['num_columns'] | |
706 | return ([[ _get_or_default(items, c*nrow+i, default=empty) for c in range(ncol) ] for i in range(nrow) ], info) |
|
714 | if row_first: | |
|
715 | return ([[_get_or_default(items, c * nrow + r, default=empty) for r in range(nrow)] for c in range(ncol)], info) | |||
|
716 | else: | |||
|
717 | return ([[_get_or_default(items, c * nrow + r, default=empty) for c in range(ncol)] for r in range(nrow)], info) | |||
707 |
|
718 | |||
708 |
|
719 | |||
709 | def columnize(items, separator=' ', displaywidth=80): |
|
720 | def columnize(items, row_first=False, separator=' ', displaywidth=80): | |
710 | """ Transform a list of strings into a single string with columns. |
|
721 | """ Transform a list of strings into a single string with columns. | |
711 |
|
722 | |||
712 | Parameters |
|
723 | Parameters | |
@@ -714,6 +725,10 b" def columnize(items, separator=' ', displaywidth=80):" | |||||
714 | items : sequence of strings |
|
725 | items : sequence of strings | |
715 | The strings to process. |
|
726 | The strings to process. | |
716 |
|
727 | |||
|
728 | row_first : (default False) | |||
|
729 | Whether to to compute columns for a row-first matrix instead ofr | |||
|
730 | column-first (default). | |||
|
731 | ||||
717 | separator : str, optional [default is two spaces] |
|
732 | separator : str, optional [default is two spaces] | |
718 | The string that separates columns. |
|
733 | The string that separates columns. | |
719 |
|
734 | |||
@@ -724,11 +739,11 b" def columnize(items, separator=' ', displaywidth=80):" | |||||
724 | ------- |
|
739 | ------- | |
725 | The formatted string. |
|
740 | The formatted string. | |
726 | """ |
|
741 | """ | |
727 |
if not items |
|
742 | if not items: | |
728 | return '\n' |
|
743 | return '\n' | |
729 | matrix, info = compute_item_matrix(items, separator_size=len(separator), displaywidth=displaywidth) |
|
744 | matrix, info = compute_item_matrix(items, row_first=row_first, separator_size=len(separator), displaywidth=displaywidth) | |
730 | fmatrix = [filter(None, x) for x in matrix] |
|
745 | fmatrix = [filter(None, x) for x in matrix] | |
731 |
sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['column |
|
746 | sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['column_widths'])]) | |
732 | return '\n'.join(map(sjoin, fmatrix))+'\n' |
|
747 | return '\n'.join(map(sjoin, fmatrix))+'\n' | |
733 |
|
748 | |||
734 |
|
749 |
General Comments 0
You need to be logged in to leave comments.
Login now