Skip to content

Commit

Permalink
Merge pull request #365 from ChristopherBradley/continuation_table
Browse files Browse the repository at this point in the history
Issues #352 and #363
  • Loading branch information
GavinHuttley committed Nov 8, 2019
2 parents 07d6539 + 0d71a72 commit 1f04426
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
6 changes: 3 additions & 3 deletions doc/examples/handling_tabular_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1070,9 +1070,9 @@ We can likewise specify a writer, using a custom field formatter and provide thi

.. doctest::

>>> from cogent3.format.table import FormatFields, SeparatorFormatWriter
>>> formatter = FormatFields([(0,'"%s"'), (1,'"%s"')])
>>> writer = SeparatorFormatWriter(formatter=formatter, sep=" | ")
>>> from cogent3.format.table import format_fields, separator_formatter
>>> formatter = format_fields([(0,'"%s"'), (1,'"%s"')])
>>> writer = separator_formatter(formatter=formatter, sep=" | ")
>>> for formatted in writer(comma_sep, has_header=True):
... print(formatted)
edge.name | edge.parent | length | x | y | z
Expand Down
6 changes: 3 additions & 3 deletions src/cogent3/format/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ def separator_format(header, formatted_table, title=None, legend=None, sep=None)
return table


def FormatFields(formats):
def format_fields(formats):
"""Formats row fields by index.
Parameters
Expand All @@ -543,7 +543,7 @@ def callable(line, index_format=index_format):
return callable


def SeparatorFormatWriter(formatter=None, ignore=None, sep=","):
def separator_formatter(formatter=None, ignore=None, sep=","):
"""Returns a writer for a delimited tabular file. The writer has a
has_header argument which ignores the formatter for a header line. Default
format is string. Does not currently handle Titles or Legends.
Expand All @@ -562,7 +562,7 @@ def SeparatorFormatWriter(formatter=None, ignore=None, sep=","):

def callable(lines, formatter=formatter, has_header=False):
if not formatter:
formatter = FormatFields([(i, "%s") for i in range(len(lines[0]))])
formatter = format_fields([(i, "%s") for i in range(len(lines[0]))])
header_done = None
for line in lines:
if has_header and not header_done:
Expand Down
1 change: 0 additions & 1 deletion src/cogent3/maths/stats/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1971,4 +1971,3 @@ def get_ltm_cells(cells):
# remove duplicates
new_cells = sorted(set(new_cells))
return new_cells

37 changes: 19 additions & 18 deletions tests/test_util/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

__author__ = "Thomas La"
__copyright__ = "Copyright 2007-2019, The Cogent Project"
__credits__ = ["Gavin Huttley", "Thomas La"]
__credits__ = ["Gavin Huttley", "Thomas La", "Christopher Bradley"]
__license__ = "BSD-3"
__version__ = "2019.10.24a"
__maintainer__ = "Gavin Huttley"
Expand Down Expand Up @@ -67,6 +67,10 @@ class TableTests(TestCase):
t5_header = ["a", "b", "c", "d"]
t5_rows = [[1, 1, 1, 1], [2, 0, 1, 1], [1, 3, 2, 2]]

# test table 6
t6_header = ["id", "foo", "bar"]
t6_rows = [["60", " | ", "666"], ["70", "bca", "777"]]

def test_appended(self):
"""test the table appended method"""
t2 = Table(header=self.t2_header, rows=self.t2_rows)
Expand Down Expand Up @@ -159,17 +163,18 @@ def test_grid_table_format(self):
"""test the table grid_table_format method"""
from cogent3.format.table import grid_table_format

t6_header = ["id", "foo", "bar"]
t6_rows = [["60", " | ", "666"], ["70", "bca", "777"]]
formatted_grid = grid_table_format(
t6_header, t6_rows, title="Test", legend="Units"
self.t6_header, self.t6_rows, title="Test", legend="Units"
)
self.assertEqual(len(formatted_grid.split("\n")), len(t6_rows) * 2 + 7)
self.assertEqual(len(formatted_grid.split("\n")), len(self.t6_rows) * 2 + 7)

formatted_grid = grid_table_format(
t6_header, t6_rows, title="Really Long Title", legend="Extra Long Legend"
self.t6_header,
self.t6_rows,
title="Really Long Title",
legend="Extra Long Legend",
)
self.assertEqual(len(formatted_grid.split("\n")), len(t6_rows) * 2 + 7 + 2)
self.assertEqual(len(formatted_grid.split("\n")), len(self.t6_rows) * 2 + 7 + 2)

def test_joined(self):
"""test the table joined method"""
Expand Down Expand Up @@ -197,16 +202,14 @@ def test_markdown(self):
"""Exercising the table markdown method"""
from cogent3.format.table import markdown

t6_header = ["id", "foo", "bar"]
t6_rows = [["60", " | ", "666"], ["70", "bca", "777"]]
markdown_table = markdown(t6_header, t6_rows, justify="crl")
markdown_table = markdown(self.t6_header, self.t6_rows, justify="crl")
markdown_list = markdown_table.split("\n")
self.assertEqual(markdown_list[2].count(r"|"), 5)
# the pipe symbol should have been escaped
self.assertEqual(markdown_list[2].count(r"\|"), 1)

with self.assertRaises(ValueError):
_ = markdown(t6_header, t6_rows, justify="cr1")
_ = markdown(self.t6_header, self.t6_rows, justify="cr1")

def test_normalized(self):
"""test the table normalized method"""
Expand Down Expand Up @@ -326,22 +329,20 @@ def test_separator_format(self):
"""testing separator_format with title and legend, and contents that match the separator"""
from cogent3.format.table import separator_format

t6_header = ["id", "foo", "bar"]
t6_rows = [["60", " | ", "666"], ["70", "bca", "777"]]
with self.assertRaises(RuntimeError):
_ = separator_format(t6_header, t6_rows)
_ = separator_format(self.t6_header, self.t6_rows)
separated_table = separator_format(
t6_header, t6_rows, sep=" | ", title="Test", legend="Units"
self.t6_header, self.t6_rows, sep=" | ", title="Test", legend="Units"
)
self.assertEqual(len(separated_table.split("\n")), len(t6_rows) + 3)
self.assertEqual(len(separated_table.split("\n")), len(self.t6_rows) + 3)

def test_separator_format_writer(self):
"""exercising separator_format_writer"""
from cogent3.format.table import SeparatorFormatWriter
from cogent3.format.table import separator_formatter

t3 = Table(self.t3_header, rows=self.t3_rows)
comma_sep = t3.to_string(sep=",").splitlines()
writer = SeparatorFormatWriter(sep=" | ")
writer = separator_formatter(sep=" | ")
formatted = [
f for f in writer([l.split(",") for l in comma_sep], has_header=True)
]
Expand Down

0 comments on commit 1f04426

Please sign in to comment.