Skip to content

Commit

Permalink
Add from_tablib method
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Jun 10, 2018
1 parent 7736323 commit 6a03ffe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pytablewriter/writer/_table_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,14 @@ def from_dataframe(self, dataframe):
self.__get_typehint_from_dtype(dtype) for dtype in dataframe.dtypes
]

def from_tablib(self, tablib_dataset):
"""
Set tabular attributes to the writer from :py:class:`tablib.Dataset`.
"""

self.header_list = tablib_dataset.headers
self.value_matrix = [row for row in tablib_dataset]

def write_table(self):
"""
|write_table|.
Expand Down
28 changes: 28 additions & 0 deletions test/test_markdown_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,31 @@ def test_exception(self, table, header, value, expected):

with pytest.raises(expected):
writer.write_table_iter()


class Test_MarkdownTableWriter_from_tablib(object):

def test_normal_multiple_write(self, capsys):
import tablib

data = tablib.Dataset()
data.headers = ['a', 'b', 'c']
data.append(['1', 'AA', 'abc'])
data.append(['2', 'BB', 'zzz'])

writer = table_writer_class()
writer.from_tablib(data)
writer.write_table()

expected = dedent("""\
| a | b | c |
|--:|---|---|
| 1|AA |abc|
| 2|BB |zzz|
""")

out, err = capsys.readouterr()
print_test_result(expected=expected, actual=out, error=err)

assert out == expected

0 comments on commit 6a03ffe

Please sign in to comment.