MCPcopy Create free account
hub / github.com/ipython/ipython / compute_item_matrix

Function compute_item_matrix

IPython/utils/text.py:650–707  ·  view source on GitHub ↗

Returns a nested list, and info to columnize items Parameters ---------- items list of strings to columize row_first : (default False) Whether to compute columns for a row-first matrix instead of column-first (default). empty : (default None) def

(items, row_first=False, empty=None, *args, **kwargs)

Source from the content-addressed store, hash-verified

648
649
650def compute_item_matrix(items, row_first=False, empty=None, *args, **kwargs) :
651 """Returns a nested list, and info to columnize items
652
653 Parameters
654 ----------
655
656 items
657 list of strings to columize
658 row_first : (default False)
659 Whether to compute columns for a row-first matrix instead of
660 column-first (default).
661 empty : (default None)
662 default value to fill list if needed
663 separator_size : int (default=2)
664 How much characters will be used as a separation between each columns.
665 displaywidth : int (default=80)
666 The width of the area onto which the columns should enter
667
668 Returns
669 -------
670
671 strings_matrix
672
673 nested list of string, the outer most list contains as many list as
674 rows, the innermost lists have each as many element as columns. If the
675 total number of elements in `items` does not equal the product of
676 rows*columns, the last element of some lists are filled with `None`.
677
678 dict_info
679 some info to make columnize easier:
680
681 num_columns
682 number of columns
683 max_rows
684 maximum number of rows (final number may be less)
685 column_widths
686 list of with of each columns
687 optimal_separator_width
688 best separator width between columns
689
690 Examples
691 --------
692 ::
693
694 In [1]: l = ['aaa','b','cc','d','eeeee','f','g','h','i','j','k','l']
695 In [2]: list, info = compute_item_matrix(l, displaywidth=12)
696 In [3]: list
697 Out[3]: [['aaa', 'f', 'k'], ['b', 'g', 'l'], ['cc', 'h', None], ['d', 'i', None], ['eeeee', 'j', None]]
698 In [4]: ideal = {'num_columns': 3, 'column_widths': [5, 1, 1], 'optimal_separator_width': 2, 'max_rows': 5}
699 In [5]: all((info[k] == ideal[k] for k in ideal.keys()))
700 Out[5]: True
701 """
702 info = _find_optimal(list(map(len, items)), row_first, *args, **kwargs)
703 nrow, ncol = info['max_rows'], info['num_columns']
704 if row_first:
705 return ([[_get_or_default(items, r * ncol + c, default=empty) for c in range(ncol)] for r in range(nrow)], info)
706 else:
707 return ([[_get_or_default(items, c * nrow + r, default=empty) for c in range(ncol)] for r in range(nrow)], info)

Callers 1

columnizeFunction · 0.85

Calls 2

_find_optimalFunction · 0.85
_get_or_defaultFunction · 0.85

Tested by

no test coverage detected