MCPcopy Create free account
hub / github.com/astanin/python-tabulate / _wrap_text_to_colwidths

Function _wrap_text_to_colwidths

tabulate/__init__.py:1666–1718  ·  view source on GitHub ↗
(
    list_of_lists,
    colwidths,
    numparses=True,
    missingval=_DEFAULT_MISSINGVAL,
    break_long_words=_BREAK_LONG_WORDS,
    break_on_hyphens=_BREAK_ON_HYPHENS,
)

Source from the content-addressed store, hash-verified

1664
1665
1666def _wrap_text_to_colwidths(
1667 list_of_lists,
1668 colwidths,
1669 numparses=True,
1670 missingval=_DEFAULT_MISSINGVAL,
1671 break_long_words=_BREAK_LONG_WORDS,
1672 break_on_hyphens=_BREAK_ON_HYPHENS,
1673):
1674 if len(list_of_lists):
1675 num_cols = len(list_of_lists[0])
1676 else:
1677 num_cols = 0
1678 numparses = _expand_iterable(numparses, num_cols, True)
1679
1680 result = []
1681
1682 for row in list_of_lists:
1683 new_row = []
1684 for cell, width, numparse in zip(row, colwidths, numparses):
1685 if _isnumber(cell) and numparse:
1686 new_row.append(cell)
1687 continue
1688
1689 if width is not None:
1690 wrapper = _CustomTextWrap(
1691 width=width,
1692 break_long_words=break_long_words,
1693 break_on_hyphens=break_on_hyphens,
1694 )
1695 # Cast based on our internal type handling. Any future custom
1696 # formatting of types (such as datetimes) may need to be more
1697 # explicit than just `str` of the object. Also doesn't work for
1698 # custom floatfmt/intfmt, nor with any missing/blank cells.
1699 casted_cell = (
1700 missingval
1701 if cell is None
1702 else (
1703 str(cell)
1704 if cell == "" or _isnumber(cell)
1705 else str(_type(cell, numparse)(cell))
1706 )
1707 )
1708 wrapped = [
1709 "\n".join(wrapper.wrap(line))
1710 for line in casted_cell.splitlines()
1711 if line.strip() != ""
1712 ]
1713 new_row.append("\n".join(wrapped))
1714 else:
1715 new_row.append(cell)
1716 result.append(new_row)
1717
1718 return result
1719
1720
1721def _to_str(s, encoding="utf8", errors="ignore"):

Callers 1

tabulateFunction · 0.85

Calls 4

_expand_iterableFunction · 0.85
_isnumberFunction · 0.85
_CustomTextWrapClass · 0.85
_typeFunction · 0.85

Tested by

no test coverage detected