Skip to content

Commit 51739fc

Browse files
committed
Added unit tests for supported data types
Also: - Updated README.md to briefly mention supported data types - Updated tox.ini to install numpy and pandas for testing supported data types
1 parent d18dc9b commit 51739fc

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ print(tf.generate_table(rows))
6969

7070
*NOTE: Rendering of tables looks much better in Python than it appears in this Markdown file.*
7171

72+
See the [simple_text.py](https://github.com/python-tableformatter/tableformatter/blob/master/examples/simple_text.py) and
73+
[simple_object.py](https://github.com/python-tableformatter/tableformatter/blob/master/examples/simple_object.py) examples
74+
for more basic usage.
75+
76+
## Supported Data Types
77+
The following tabular data types are supported:
78+
* list of lists or another iterable of iterables
79+
* two-dimensional NumPy arrays
80+
* NumPy record arrays (names as columns)
81+
* pandas.DataFrame
82+
* list or another iterable of arbitrary non-iterable objects (column specifier required)
83+
* list or another iterable of dicts (dict keys iterated through as rows where each key must be a hashable iterable)
84+
* dict of iterables (keys as columns)
85+
86+
See the [data_types.py](https://github.com/python-tableformatter/tableformatter/blob/master/examples/data_types.py)
87+
example for more info.
88+
7289

7390
## Column Headers
7491
The second argument to ``generate_table`` named ``columns`` is optional and defines a list of column headers to be used.
@@ -88,7 +105,6 @@ print(tf.generate_table(rows, cols))
88105
╚══════╧══════╧══════╧══════╝
89106
```
90107

91-
92108
## Grid Style
93109
The third argument to ``generated`` table named ``grid_style`` is optional and specifies how the table lines are drawn.
94110

tests/test_data_types.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# coding=utf-8
2+
"""
3+
Unit testing the variety of data types supported by tableformatter.
4+
"""
5+
import numpy as np
6+
import pandas as pd
7+
import tableformatter as tf
8+
9+
10+
EXPECTED_BASIC = '''
11+
╔═══╤═══╤═══╤═══╗
12+
║ 1 │ 2 │ 3 │ 4 ║
13+
║ 5 │ 6 │ 7 │ 8 ║
14+
╚═══╧═══╧═══╧═══╝
15+
'''.lstrip('\n')
16+
17+
EXPECTED_WITH_HEADERS = '''
18+
╔══════╤══════╤══════╤══════╗
19+
║ col1 │ col2 │ col3 │ col4 ║
20+
╠══════╪══════╪══════╪══════╣
21+
║ 1 │ 2 │ 3 │ 4 ║
22+
║ 5 │ 6 │ 7 │ 8 ║
23+
╚══════╧══════╧══════╧══════╝
24+
'''.lstrip('\n')
25+
26+
iteralbe_of_iterables = [[1, 2, 3, 4],
27+
[5, 6, 7, 8]]
28+
np_2d_array = np.array(iteralbe_of_iterables)
29+
d = {'col1': [1, 5], 'col2': [2, 6], 'col3': [3, 7], 'col4': [4, 8]}
30+
df = pd.DataFrame(data=d)
31+
32+
33+
def test_iterable_of_iterables():
34+
table = tf.generate_table(iteralbe_of_iterables)
35+
assert table == EXPECTED_BASIC
36+
37+
38+
def test_numpy_2d_array():
39+
np_2d_array = np.array(iteralbe_of_iterables)
40+
table = tf.generate_table(np_2d_array)
41+
assert table == EXPECTED_BASIC
42+
43+
44+
def test_iterable_of_dicts():
45+
iterable_of_dicts = [{1: 'a', 2: 'b', 3: 'c', 4: 'd'},
46+
{5: 'e', 6: 'f', 7: 'g', 8: 'h'}]
47+
table = tf.generate_table(iterable_of_dicts)
48+
assert table == EXPECTED_BASIC
49+
50+
51+
def test_numpy_record_array():
52+
np_rec_array = np.rec.array([(1, 2., 'Hello'),
53+
(2, 3., "World")],
54+
dtype=[('foo', 'i4'), ('bar', 'f4'), ('baz', 'U10')])
55+
table = tf.generate_table(np_rec_array)
56+
expected = '''
57+
╔═════╤═════╤═══════╗
58+
║ foo │ bar │ baz ║
59+
╠═════╪═════╪═══════╣
60+
║ 1 │ 2.0 │ Hello ║
61+
║ 2 │ 3.0 │ World ║
62+
╚═════╧═════╧═══════╝
63+
'''.lstrip('\n')
64+
assert table == expected
65+
66+
67+
def test_pandas_dataframe():
68+
table = tf.generate_table(df)
69+
assert table == EXPECTED_WITH_HEADERS
70+
71+
72+
def test_dict_of_iterables():
73+
table = tf.generate_table(d)
74+
assert table == EXPECTED_WITH_HEADERS
75+
76+
77+
class MyRowObject(object):
78+
"""Simple object to demonstrate using a list of non-iterable objects with TableFormatter"""
79+
def __init__(self, field1: int, field2: int, field3: int, field4: int):
80+
self.field1 = field1
81+
self.field2 = field2
82+
self._field3 = field3
83+
self.field4 = field4
84+
85+
def get_field3(self):
86+
"""Demonstrates accessing object functions"""
87+
return self._field3
88+
89+
90+
def test_iterable_of_non_iterable_objects():
91+
rows = [MyRowObject(1, 2, 3, 4),
92+
MyRowObject(5, 6, 7, 8)]
93+
columns = (tf.Column('col1', attrib='field1'),
94+
tf.Column('col2', attrib='field2'),
95+
tf.Column('col3', attrib='get_field3'),
96+
tf.Column('col4', attrib='field4'))
97+
table = tf.generate_table(rows, columns)
98+
assert table == EXPECTED_WITH_HEADERS

tox.ini

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ setenv =
1212
[testenv:py34]
1313
deps =
1414
codecov
15+
numpy
16+
pandas
1517
pytest
1618
pytest-cov
1719
commands =
@@ -21,6 +23,8 @@ commands =
2123
[testenv:py35]
2224
deps =
2325
codecov
26+
numpy
27+
pandas
2428
pytest
2529
pytest-cov
2630
commands =
@@ -29,12 +33,16 @@ commands =
2933

3034
[testenv:py35-win]
3135
deps =
36+
numpy
37+
pandas
3238
pytest
3339
commands = py.test -v
3440

3541
[testenv:py36]
3642
deps =
3743
codecov
44+
numpy
45+
pandas
3846
pytest
3947
pytest-cov
4048
commands =
@@ -44,6 +52,8 @@ commands =
4452
[testenv:py36-win]
4553
deps =
4654
codecov
55+
numpy
56+
pandas
4757
pytest
4858
pytest-cov
4959
commands =
@@ -52,6 +62,8 @@ commands =
5262

5363
[testenv:py37]
5464
deps =
65+
numpy
66+
pandas
5567
pytest
5668
commands = py.test -v
5769

0 commit comments

Comments
 (0)