##// END OF EJS Templates
fix docstring, and debug leftover
Matthias BUSSONNIER -
Show More
@@ -24,7 +24,7 b' import textwrap'
24 24 from string import Formatter
25 25
26 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 28 from IPython.utils import py3compat
29 29 from IPython.utils.io import nlprint
30 30 from IPython.utils.data import flatten
@@ -668,13 +668,13 b' def _chunks(l, n):'
668 668 for i in xrange(0, len(l), n):
669 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 672 """Calculate optimal info to columnize a list of string"""
673 673 for nrow in range(1, len(rlist)+1) :
674 chk = [max(l) for l in _chunks(rlist, nrow) ]
674 chk = map(max,_chunks(rlist, nrow))
675 675 sumlength = sum(chk)
676 676 ncols = len(chk)
677 if sumlength+sepsize*(ncols-1) <= displaywidth :
677 if sumlength+separator_size*(ncols-1) <= displaywidth :
678 678 break;
679 679 return {'columns_numbers' : ncols,
680 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 689 else :
690 690 return mylist[i]
691 691
692 @skip_doctest
692 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 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 743 info = _find_optimal(map(len, items), *args, **kwargs)
700 744 nrow, ncol = info['rows_numbers'], info['columns_numbers']
@@ -720,9 +764,7 b" def columnize(items, separator=' ', displaywidth=80):"
720 764 """
721 765 if not items :
722 766 return '\n'
723 matrix, info = compute_item_matrix(items, sepsize=len(separator), displaywidth=displaywidth)
724 #sep = ' '*min(info['optimal_separator_width'], 9)
725 fmatrix = matrix
767 matrix, info = compute_item_matrix(items, separator_size=len(separator), displaywidth=displaywidth)
726 768 fmatrix = [filter(None, x) for x in matrix]
727 769 sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['columns_width'])])
728 770 return '\n'.join(map(sjoin, fmatrix))+'\n'
General Comments 0
You need to be logged in to leave comments. Login now