Skip to content

Commit

Permalink
Add support for keyword arguments initialization to TableWriterFactor…
Browse files Browse the repository at this point in the history
…y instantiation
  • Loading branch information
thombashi committed Aug 30, 2020
1 parent 6ee5ebc commit dbf1b6f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
12 changes: 8 additions & 4 deletions pytablewriter/_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TableWriterFactory:
"""

@classmethod
def create_from_file_extension(cls, file_extension: str) -> AbstractTableWriter:
def create_from_file_extension(cls, file_extension: str, **kwargs) -> AbstractTableWriter:
"""
Create a table writer class instance from a file extension.
Supported file extensions are as follows:
Expand Down Expand Up @@ -52,6 +52,8 @@ def create_from_file_extension(cls, file_extension: str) -> AbstractTableWriter:
:param str file_extension:
File extension string (case insensitive).
:param kwargs:
Keyword arguments that passing to writer class constructor.
:return:
Writer instance that coincides with the ``file_extension``.
:rtype:
Expand All @@ -77,7 +79,7 @@ def create_from_file_extension(cls, file_extension: str) -> AbstractTableWriter:

logger.debug("create a {} instance".format(table_format.writer_class.__name__))

return table_format.writer_class()
return table_format.writer_class(**kwargs)

raise WriterNotFoundError(
"\n".join(
Expand All @@ -90,7 +92,7 @@ def create_from_file_extension(cls, file_extension: str) -> AbstractTableWriter:
)

@classmethod
def create_from_format_name(cls, format_name: str) -> AbstractTableWriter:
def create_from_format_name(cls, format_name: str, **kwargs) -> AbstractTableWriter:
"""
Create a table writer class instance from a format name.
Supported file format names are as follows:
Expand Down Expand Up @@ -126,6 +128,8 @@ def create_from_format_name(cls, format_name: str) -> AbstractTableWriter:
============================================= ===================================
:param str format_name: Format name string (case insensitive).
:param kwargs:
Keyword arguments that passing to writer class constructor.
:return: Writer instance that coincides with the ``format_name``:
:rtype:
:py:class:`~pytablewriter.writer._table_writer.TableWriterInterface`
Expand All @@ -141,7 +145,7 @@ def create_from_format_name(cls, format_name: str) -> AbstractTableWriter:
):
logger.debug("create a {} instance".format(table_format.writer_class.__name__))

return table_format.writer_class()
return table_format.writer_class(**kwargs)

raise WriterNotFoundError(
"\n".join(
Expand Down
44 changes: 42 additions & 2 deletions test/test_writer_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"""

import itertools
import sys

import pytest

import pytablewriter as ptw
from pytablewriter.typehint import Integer


class Test_WriterFactory_get_format_names:
Expand Down Expand Up @@ -170,9 +172,28 @@ class Test_WriterFactory_create_from_file_extension:
),
)
def test_normal(self, value, expected):
writer = ptw.TableWriterFactory.create_from_file_extension(value)
table_name = "dummy"
headers = ["a", "b"]
value_matrix = [[1, 2]]
type_hints = [Integer, Integer]
is_formatting_float = False

writer = ptw.TableWriterFactory.create_from_file_extension(
value,
table_name=table_name,
headers=headers,
value_matrix=value_matrix,
type_hints=type_hints,
is_formatting_float=is_formatting_float,
)

print(type(writer), file=sys.stderr)
assert isinstance(writer, expected)
assert writer.table_name == table_name
assert writer.headers == headers
assert writer.value_matrix == value_matrix
assert writer.type_hints == type_hints
assert writer.is_formatting_float == is_formatting_float

@pytest.mark.parametrize(
["value", "expected"],
Expand Down Expand Up @@ -242,9 +263,28 @@ class Test_FileLoaderFactory_create_from_format_name:
],
)
def test_normal(self, format_name, expected):
writer = ptw.TableWriterFactory.create_from_format_name(format_name)
table_name = "dummy"
headers = ["a", "b"]
value_matrix = [[1, 2]]
type_hints = [Integer, Integer]
is_formatting_float = False

writer = ptw.TableWriterFactory.create_from_format_name(
format_name,
table_name=table_name,
headers=headers,
value_matrix=value_matrix,
type_hints=type_hints,
is_formatting_float=is_formatting_float,
)

print(format_name, type(writer), file=sys.stderr)
assert isinstance(writer, expected)
assert writer.table_name == table_name
assert writer.headers == headers
assert writer.value_matrix == value_matrix
assert writer.type_hints == type_hints
assert writer.is_formatting_float == is_formatting_float

@pytest.mark.parametrize(
["format_name", "expected"],
Expand Down

0 comments on commit dbf1b6f

Please sign in to comment.