Skip to content

Commit fdce34d

Browse files
authored
Merge pull request #10 from python-tableformatter/readme_cutandpaste
Improved README
2 parents 5cd4727 + 12c8ca2 commit fdce34d

File tree

3 files changed

+80
-14
lines changed

3 files changed

+80
-14
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ to this function is ``rows`` which is an Iterable of Iterables such as a list of
5050
a 2D [numpy](http://www.numpy.org) array. ``generate_table`` outputs a nicely formatted table:
5151

5252
```Python
53-
>>> from tableformatter import generate_table
53+
from tableformatter import generate_table
5454

55-
>>> rows = [('A1', 'A2', 'A3', 'A4'),
56-
... ('B1', 'B2\nB2\nB2', 'B3', 'B4'),
57-
... ('C1', 'C2', 'C3', 'C4'),
58-
... ('D1', 'D2', 'D3', 'D4')]
59-
>>> print(generate_table(rows))
55+
rows = [('A1', 'A2', 'A3', 'A4'),
56+
('B1', 'B2\nB2\nB2', 'B3', 'B4'),
57+
('C1', 'C2', 'C3', 'C4'),
58+
('D1', 'D2', 'D3', 'D4')]
59+
print(generate_table(rows))
6060
╔════╤════╤════╤════╗
6161
║ A1 │ A2 │ A3 │ A4 ║
6262
║ B1 │ B2 │ B3 │ B4 ║
@@ -74,8 +74,8 @@ Column Headers
7474
The second argument to ``generate_table`` named ``columns`` is optional and defines a list of column headers to be used.
7575

7676
```Python
77-
>>> cols = ['Col1', 'Col2', 'Col3', 'Col4']
78-
>>> print(generate_table(rows, cols))
77+
cols = ['Col1', 'Col2', 'Col3', 'Col4']
78+
print(generate_table(rows, cols))
7979
╔══════╤══════╤══════╤══════╗
8080
║ Col1 │ Col2 │ Col3 │ Col4 ║
8181
╠══════╪══════╪══════╪══════╣
@@ -99,9 +99,9 @@ Supported grid sytles are:
9999
* **SparseGrid** - sparse grid with no lines at all to conserve both vertical and horizontal space
100100

101101
```Python
102-
>>> from tableformatter import FancyGrid
102+
from tableformatter import FancyGrid
103103

104-
>>> print(generate_table(rows, grid_style=FancyGrid))
104+
print(generate_table(rows, grid_style=FancyGrid))
105105
╔════╤════╤════╤════╗
106106
║ A1 │ A2 │ A3 │ A4 ║
107107
╟────┼────┼────┼────╢
@@ -122,7 +122,7 @@ this and print "rows" up-to-down and "columns" left-to-right then that is easily
122122
to ``generate_table``:
123123

124124
```Python
125-
>>> print(generate_table(rows, cols, transpose=True))
125+
print(generate_table(rows, cols, transpose=True))
126126
╔══════╦════╤════╤════╤════╗
127127
║ Col1 ║ A1 │ B1 │ C1 │ D1 ║
128128
║ Col2 ║ A2 │ B2 │ C2 │ D2 ║

tableformatter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __subclasshook__(cls, C):
3737
__version__ = '0.1.0'
3838

3939

40-
def _text_wrap(text, width=70):
40+
def _text_wrap(text: str, width: int=70) -> List[str]:
4141
"""Wrap a single paragraph of text, returning a list of wrapped lines.
4242
4343
Reformat the single paragraph in 'text' so it fits in lines of no
@@ -225,7 +225,7 @@ def _wrap_chunks(self, chunks):
225225
return lines
226226

227227

228-
def _translate_tabs(text):
228+
def _translate_tabs(text: str) -> str:
229229
"""Translate tab characters into spaces for measurement"""
230230
tabpos = text.find('\t')
231231
while tabpos >= 0:
@@ -239,7 +239,7 @@ def _translate_tabs(text):
239239
return text
240240

241241

242-
def _printable_width(text):
242+
def _printable_width(text: str) -> int:
243243
"""Returns the printable width of a string accounting for escape characters and wide-display unicode characters"""
244244
return _wcswidth(_translate_tabs(text))
245245

tests/test_utils.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# coding=utf-8
2+
"""
3+
Unit testing of tableformatter utility functtions.
4+
"""
5+
import pytest
6+
7+
import tableformatter as tf
8+
9+
10+
@pytest.fixture
11+
def empty_text():
12+
return ''
13+
14+
15+
@pytest.fixture
16+
def sample_text():
17+
return 'FooBar'
18+
19+
@pytest.fixture
20+
def tabbed_text():
21+
return 'Foo\tBar'
22+
23+
24+
# Test text wrapping
25+
def test_empty_wrap(empty_text):
26+
expected = []
27+
wrapped = tf._text_wrap(empty_text)
28+
assert expected == wrapped
29+
30+
def test_no_wrap(sample_text):
31+
expected = [sample_text]
32+
wrapped = tf._text_wrap(sample_text)
33+
assert expected == wrapped
34+
35+
def test_simple_wrap(sample_text):
36+
expected = ['Foo', 'Bar']
37+
wrapped = tf._text_wrap(sample_text, width=3)
38+
assert expected == wrapped
39+
40+
41+
# Test tab translation and printable width calculation
42+
def test_empty_translation(empty_text):
43+
expected = empty_text
44+
translated = tf._translate_tabs(empty_text)
45+
assert expected == translated
46+
47+
def test_empty_width(empty_text):
48+
assert tf._printable_width(empty_text) == len(empty_text)
49+
50+
def test_translation_no_tabs(sample_text):
51+
expected = sample_text
52+
translated = tf._translate_tabs(sample_text)
53+
assert expected == translated
54+
55+
def test_width_no_tabs(sample_text):
56+
assert tf._printable_width(sample_text) == len(sample_text)
57+
58+
def test_translation_with_tabs(tabbed_text):
59+
expected = 'Foo Bar'
60+
translated = tf._translate_tabs(tabbed_text)
61+
assert expected == translated
62+
assert expected != tabbed_text
63+
64+
def test_width_with_tabs(tabbed_text):
65+
expected = 'Foo Bar'
66+
assert tf._printable_width(tabbed_text) == len(expected)

0 commit comments

Comments
 (0)