Skip to content

Commit ecbd67c

Browse files
author
Daniel Aslau
committed
break_long_words and break_on_hyphens as named arguments
1 parent 95eb24e commit ecbd67c

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

README.md

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -927,14 +927,6 @@ and thus no automate multiline wrapping will take place.
927927
The wrapping uses the python standard [textwrap.wrap](https://docs.python.org/3/library/textwrap.html#textwrap.wrap)
928928
function with default parameters - aside from width.
929929

930-
BREAK_LONG_WORDS and BREAK_LONG_WORDS can be used to turn off the TextWrapper break_long_words and break_on_hyphens options.
931-
932-
```python
933-
import tabulate
934-
tabulate.BREAK_LONG_WORDS = False
935-
tabulate.BREAK_ON_HYPHENS = False
936-
```
937-
938930
This example demonstrates usage of automatic multiline wrapping, though typically
939931
the lines being wrapped would probably be significantly longer than this.
940932

@@ -948,6 +940,31 @@ the lines being wrapped would probably be significantly longer than this.
948940
+------------+---------+
949941
```
950942

943+
Text is preferably wrapped on whitespaces and right after the hyphens in hyphenated words.
944+
945+
break_long_words (default: True) If true, then words longer than width will be broken in order to ensure that no lines are longer than width.
946+
If it is false, long words will not be broken, and some lines may be longer than width.
947+
(Long words will be put on a line by themselves, in order to minimize the amount by which width is exceeded.)
948+
949+
break_on_hyphens (default: True) If true, wrapping will occur preferably on whitespaces and right after hyphens in compound words, as it is customary in English.
950+
If false, only whitespaces will be considered as potentially good places for line breaks.
951+
952+
```pycon
953+
>>> print(tabulate([["John Smith", "Middle-Manager"]], headers=["Name", "Title"], tablefmt="grid", maxcolwidths=[None, 5], break_long_words=False))
954+
+------------+---------+
955+
| Name | Title |
956+
+============+=========+
957+
| John Smith | Middle- |
958+
| | Manager |
959+
+------------+---------+
960+
>>> print(tabulate([["John Smith", "Middle-Manager"]], headers=["Name", "Title"], tablefmt="grid", maxcolwidths=[None, 5], break_long_words=False, break_on_hyphens=False))
961+
+------------+----------------+
962+
| Name | Title |
963+
+============+================+
964+
| John Smith | Middle-Manager |
965+
+------------+----------------+
966+
```
967+
951968
### Adding Separating lines
952969
One might want to add one or more separating lines to highlight different sections in a table.
953970

tabulate/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ def _is_file(f):
3333

3434
# Whether or not to preserve leading/trailing whitespace in data.
3535
PRESERVE_WHITESPACE = False
36+
3637
# TextWrapper breaks words longer than 'width'.
37-
BREAK_LONG_WORDS = True
38+
_BREAK_LONG_WORDS = True
3839
# TextWrapper is breaking hyphenated words.
39-
BREAK_ON_HYPHENS = True
40+
_BREAK_ON_HYPHENS = True
4041

4142
_DEFAULT_FLOATFMT = "g"
4243
_DEFAULT_INTFMT = ""
@@ -1511,7 +1512,7 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
15111512
return rows, headers
15121513

15131514

1514-
def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True):
1515+
def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True, break_long_words=_BREAK_LONG_WORDS, break_on_hyphens=_BREAK_ON_HYPHENS):
15151516
if len(list_of_lists):
15161517
num_cols = len(list_of_lists[0])
15171518
else:
@@ -1528,7 +1529,7 @@ def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True):
15281529
continue
15291530

15301531
if width is not None:
1531-
wrapper = _CustomTextWrap(width=width, break_long_words=BREAK_LONG_WORDS, break_on_hyphens=BREAK_ON_HYPHENS)
1532+
wrapper = _CustomTextWrap(width=width, break_long_words=break_long_words, break_on_hyphens=break_on_hyphens)
15321533
# Cast based on our internal type handling
15331534
# Any future custom formatting of types (such as datetimes)
15341535
# may need to be more explicit than just `str` of the object
@@ -1588,6 +1589,8 @@ def tabulate(
15881589
maxcolwidths=None,
15891590
rowalign=None,
15901591
maxheadercolwidths=None,
1592+
break_long_words=_BREAK_LONG_WORDS,
1593+
break_on_hyphens=_BREAK_ON_HYPHENS,
15911594
):
15921595
"""Format a fixed width table for pretty printing.
15931596
@@ -2086,7 +2089,7 @@ def tabulate(
20862089

20872090
numparses = _expand_numparse(disable_numparse, num_cols)
20882091
list_of_lists = _wrap_text_to_colwidths(
2089-
list_of_lists, maxcolwidths, numparses=numparses
2092+
list_of_lists, maxcolwidths, numparses=numparses, break_long_words=break_long_words, break_on_hyphens=break_on_hyphens
20902093
)
20912094

20922095
if maxheadercolwidths is not None:
@@ -2100,7 +2103,7 @@ def tabulate(
21002103

21012104
numparses = _expand_numparse(disable_numparse, num_cols)
21022105
headers = _wrap_text_to_colwidths(
2103-
[headers], maxheadercolwidths, numparses=numparses
2106+
[headers], maxheadercolwidths, numparses=numparses, break_long_words=break_long_words, break_on_hyphens=break_on_hyphens
21042107
)[0]
21052108

21062109
# empty values in the first column of RST tables should be escaped (issue #82)

test/test_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def test_tabulate_signature():
5252
("maxcolwidths", None),
5353
("rowalign", None),
5454
("maxheadercolwidths", None),
55+
("break_long_words", True),
56+
("break_on_hyphens", True),
5557
]
5658
_check_signature(tabulate, expected_sig)
5759

test/test_output.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,30 +2979,26 @@ def test_break_long_words():
29792979
table_headers = ["h1", "h2", "h3"]
29802980
test_table = [[" foo1", " bar2 ", "foo3"]]
29812981

2982-
# Table is not wrapped
2983-
tabulate_module.BREAK_LONG_WORDS = False
2982+
# Table is not wrapped on 3 letters due to long word
29842983
expected = "h1 h2 h3\n---- ---- ----\nfoo1 bar2 foo3"
2985-
result = tabulate(test_table, table_headers, maxcolwidths=3)
2984+
result = tabulate(test_table, table_headers, maxcolwidths=3, break_long_words=False)
29862985
assert_equal(expected, result)
29872986

2988-
# Table is wrapped on 2 letters
2989-
tabulate_module.BREAK_LONG_WORDS = True
2987+
# Table max width is 3 letters
29902988
expected = "h1 h2 h3\n---- ---- ----\nf ba foo\noo1 r2 3"
2991-
result = tabulate(test_table, table_headers, maxcolwidths=3)
2989+
result = tabulate(test_table, table_headers, maxcolwidths=3, break_long_words=True)
29922990
assert_equal(expected, result)
29932991

29942992
def test_break_on_hyphens():
29952993
"Output: Default table output, with break on hyphens true."
2996-
tabulate_module.BREAK_ON_HYPHENS = False
29972994
table_headers = ["h1", "h2", "h3"]
29982995
test_table = [[" foo-bar", " bar-bar ", "foo-foo"]]
2999-
# Table is wrapped on 2 letters
2996+
# Table max width is 5, long lines breaks on hyphens
30002997
expected = "h1 h2 h3\n---- ---- -----\nfoo bar- foo-f\n-bar bar oo"
3001-
result = tabulate(test_table, table_headers, maxcolwidths=5)
2998+
result = tabulate(test_table, table_headers, maxcolwidths=5, break_on_hyphens=False)
30022999
assert_equal(expected, result)
30033000

3004-
# Table is no longer wrapped
3005-
tabulate_module.BREAK_ON_HYPHENS = True
3001+
# Table data is no longer breaks on hyphens
30063002
expected = "h1 h2 h3\n---- ---- ----\nfoo- bar- foo-\nbar bar foo"
3007-
result = tabulate(test_table, table_headers, maxcolwidths=5)
3003+
result = tabulate(test_table, table_headers, maxcolwidths=5, break_on_hyphens=True)
30083004
assert_equal(expected, result)

0 commit comments

Comments
 (0)