Show More
@@ -24,7 +24,7 b' import textwrap' | |||||
24 | from string import Formatter |
|
24 | from string import Formatter | |
25 |
|
25 | |||
26 | from IPython.external.path import path |
|
26 | from IPython.external.path import path | |
27 | from IPython.testing.skipdoctest import skip_doctest_py3 |
|
27 | from IPython.testing.skipdoctest import skip_doctest_py3, skip_doctest | |
28 | from IPython.utils import py3compat |
|
28 | from IPython.utils import py3compat | |
29 | from IPython.utils.io import nlprint |
|
29 | from IPython.utils.io import nlprint | |
30 | from IPython.utils.data import flatten |
|
30 | from IPython.utils.data import flatten | |
@@ -668,13 +668,13 b' def _chunks(l, n):' | |||||
668 | for i in xrange(0, len(l), n): |
|
668 | for i in xrange(0, len(l), n): | |
669 | yield l[i:i+n] |
|
669 | yield l[i:i+n] | |
670 |
|
670 | |||
671 | def _find_optimal(rlist , sepsize=2 , displaywidth=80): |
|
671 | def _find_optimal(rlist , separator_size=2 , displaywidth=80): | |
672 | """Calculate optimal info to columnize a list of string""" |
|
672 | """Calculate optimal info to columnize a list of string""" | |
673 | for nrow in range(1, len(rlist)+1) : |
|
673 | for nrow in range(1, len(rlist)+1) : | |
674 |
chk = |
|
674 | chk = map(max,_chunks(rlist, nrow)) | |
675 | sumlength = sum(chk) |
|
675 | sumlength = sum(chk) | |
676 | ncols = len(chk) |
|
676 | ncols = len(chk) | |
677 | if sumlength+sepsize*(ncols-1) <= displaywidth : |
|
677 | if sumlength+separator_size*(ncols-1) <= displaywidth : | |
678 | break; |
|
678 | break; | |
679 | return {'columns_numbers' : ncols, |
|
679 | return {'columns_numbers' : ncols, | |
680 | 'optimal_separator_width':(displaywidth - sumlength)/(ncols-1) if (ncols -1) else 0, |
|
680 | 'optimal_separator_width':(displaywidth - sumlength)/(ncols-1) if (ncols -1) else 0, | |
@@ -689,12 +689,56 b' def _get_or_default(mylist, i, default=None):' | |||||
689 | else : |
|
689 | else : | |
690 | return mylist[i] |
|
690 | return mylist[i] | |
691 |
|
691 | |||
|
692 | @skip_doctest | |||
692 | def compute_item_matrix(items, *args, **kwargs) : |
|
693 | def compute_item_matrix(items, *args, **kwargs) : | |
693 | """ Transform a list of strings into a nested list to columnize |
|
694 | """Returns a nested list, and info to columnize items | |
|
695 | ||||
|
696 | Parameters : | |||
|
697 | ------------ | |||
|
698 | ||||
|
699 | items : | |||
|
700 | list of strings to columize | |||
|
701 | separator_size : int (default=2) | |||
|
702 | How much caracters will be used as a separation between each columns. | |||
|
703 | displaywidth : int (default=80) | |||
|
704 | The width of the area onto wich the columns should enter | |||
|
705 | ||||
|
706 | Returns : | |||
|
707 | --------- | |||
694 |
|
708 | |||
695 | Returns a tuple of (strings_matrix, dict_info) |
|
709 | Returns a tuple of (strings_matrix, dict_info) | |
696 |
|
710 | |||
697 | innermost lists are rows, see columnize for options info |
|
711 | strings_matrix : | |
|
712 | ||||
|
713 | nested list of string, the outer most list contains as many list as | |||
|
714 | rows, the innermost lists have each as many element as colums. If the | |||
|
715 | total number of elements in `items` does not equal the product of | |||
|
716 | rows*columns, the last element of some lists are filled with `None`. | |||
|
717 | ||||
|
718 | dict_info : | |||
|
719 | some info to make columnize easier: | |||
|
720 | ||||
|
721 | columns_numbers : number of columns | |||
|
722 | rows_numbers : number of rows | |||
|
723 | columns_width : list of with of each columns | |||
|
724 | optimal_separator_width : best separator width between columns | |||
|
725 | ||||
|
726 | Exemple : | |||
|
727 | --------- | |||
|
728 | ||||
|
729 | In [1]: l = ['aaa','b','cc','d','eeeee','f','g','h','i','j','k','l'] | |||
|
730 | ...: compute_item_matrix(l,displaywidth=12) | |||
|
731 | Out[1]: | |||
|
732 | ([['aaa', 'f', 'k'], | |||
|
733 | ['b', 'g', 'l'], | |||
|
734 | ['cc', 'h', None], | |||
|
735 | ['d', 'i', None], | |||
|
736 | ['eeeee', 'j', None]], | |||
|
737 | {'columns_numbers': 3, | |||
|
738 | 'columns_width': [5, 1, 1], | |||
|
739 | 'optimal_separator_width': 2, | |||
|
740 | 'rows_numbers': 5}) | |||
|
741 | ||||
698 | """ |
|
742 | """ | |
699 | info = _find_optimal(map(len, items), *args, **kwargs) |
|
743 | info = _find_optimal(map(len, items), *args, **kwargs) | |
700 | nrow, ncol = info['rows_numbers'], info['columns_numbers'] |
|
744 | nrow, ncol = info['rows_numbers'], info['columns_numbers'] | |
@@ -720,9 +764,7 b" def columnize(items, separator=' ', displaywidth=80):" | |||||
720 | """ |
|
764 | """ | |
721 | if not items : |
|
765 | if not items : | |
722 | return '\n' |
|
766 | return '\n' | |
723 | matrix, info = compute_item_matrix(items, sepsize=len(separator), displaywidth=displaywidth) |
|
767 | matrix, info = compute_item_matrix(items, separator_size=len(separator), displaywidth=displaywidth) | |
724 | #sep = ' '*min(info['optimal_separator_width'], 9) |
|
|||
725 | fmatrix = matrix |
|
|||
726 | fmatrix = [filter(None, x) for x in matrix] |
|
768 | fmatrix = [filter(None, x) for x in matrix] | |
727 | sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['columns_width'])]) |
|
769 | sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['columns_width'])]) | |
728 | return '\n'.join(map(sjoin, fmatrix))+'\n' |
|
770 | return '\n'.join(map(sjoin, fmatrix))+'\n' |
General Comments 0
You need to be logged in to leave comments.
Login now