Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
- Arguments after ``ploidy`` in ``write_vcf`` marked as keyword only
(:user:`jeromekelleher`, :pr:`2329`, :issue:`2315`).

- When metadata equal to ``b''`` is printed to text or HTML tables it will render as
an empty string rather than ``"b''"``. (:user:`hyanwong`, :issue:`2349`, :pr:`2351`)

----------------------
[0.4.1] - 2022-01-11
----------------------
Expand Down
4 changes: 4 additions & 0 deletions python/tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,13 @@ def test_str(self):
input_data = self.make_input_data(41)
table = self.table_class()
table.set_columns(**input_data)
blank_meta_row = 39
if "metadata" in input_data:
table[blank_meta_row] = table[blank_meta_row].replace(metadata=b"")
assert "1 rows skipped" in str(table)
tskit.set_print_options(max_lines=None)
assert "1 rows skipped" not in str(table)
assert "b''" not in str(table)
tskit.set_print_options(max_lines=40)
tskit.MAX_LINES = 40

Expand Down
10 changes: 9 additions & 1 deletion python/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,19 @@ def test_obj_to_collapsed_html(obj, expected):


def test_truncate_string_end():
assert util.truncate_string_end("testing") == "testing"
assert util.truncate_string_end("testing", 40) == "testing"
assert util.truncate_string_end("testing", 7) == "testing"
assert util.truncate_string_end("testing", 5) == "te..."


def test_render_metadata():
assert util.render_metadata({}) == "{}"
assert util.render_metadata("testing") == "testing"
assert util.render_metadata(b"testing") == "b'testing'"
assert util.render_metadata(b"testing", 6) == "b't..."
assert util.render_metadata(b"") == ""


def test_unicode_table():
assert (
util.unicode_table(
Expand Down
20 changes: 7 additions & 13 deletions python/tskit/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ def _text_header_and_rows(self, limit=None):
row.flags,
location_str,
parents_str,
util.truncate_string_end(str(row.metadata)),
util.render_metadata(row.metadata),
).split("\t")
)
return headers, rows
Expand Down Expand Up @@ -1111,7 +1111,7 @@ def _text_header_and_rows(self, limit=None):
row.population,
row.individual,
row.time,
util.truncate_string_end(str(row.metadata)),
util.render_metadata(row.metadata),
).split("\t")
)
return headers, rows
Expand Down Expand Up @@ -1309,7 +1309,7 @@ def _text_header_and_rows(self, limit=None):
row.right,
row.parent,
row.child,
util.truncate_string_end(str(row.metadata)),
util.render_metadata(row.metadata),
).split("\t")
)
return headers, rows
Expand Down Expand Up @@ -1527,7 +1527,7 @@ def _text_header_and_rows(self, limit=None):
row.source,
row.dest,
row.time,
util.truncate_string_end(str(row.metadata)),
util.render_metadata(row.metadata),
).split("\t")
)
return headers, rows
Expand Down Expand Up @@ -1729,7 +1729,6 @@ def _text_header_and_rows(self, limit=None):
range(self.num_rows - (limit - (limit // 2)), self.num_rows),
)
for j in indexes:

if j == -1:
rows.append(f"__skipped__{self.num_rows-limit}")
else:
Expand All @@ -1739,7 +1738,7 @@ def _text_header_and_rows(self, limit=None):
j,
row.position,
row.ancestral_state,
util.truncate_string_end(str(row.metadata)),
util.render_metadata(row.metadata),
).split("\t")
)
return headers, rows
Expand Down Expand Up @@ -1962,7 +1961,7 @@ def _text_header_and_rows(self, limit=None):
row.time,
row.derived_state,
row.parent,
util.truncate_string_end(str(row.metadata)),
util.render_metadata(row.metadata),
).split("\t")
)
return headers, rows
Expand Down Expand Up @@ -2210,12 +2209,7 @@ def _text_header_and_rows(self, limit=None):
if j == -1:
rows.append(f"__skipped__{self.num_rows-limit}")
else:
rows.append(
(
str(j),
util.truncate_string_end(str(self[j].metadata), length=70),
)
)
rows.append((str(j), util.render_metadata(self[j].metadata, length=70)))
return headers, rows

def set_columns(self, metadata=None, metadata_offset=None, metadata_schema=None):
Expand Down
8 changes: 7 additions & 1 deletion python/tskit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def obj_to_collapsed_html(d, name=None, open_depth=0):
return f"{name} {d}"


def truncate_string_end(string, length=40):
def truncate_string_end(string, length):
"""
If a string is longer than "length" then snip out the middle and replace with an
ellipsis.
Expand All @@ -363,6 +363,12 @@ def truncate_string_end(string, length=40):
return f"{string[:length-3]}..."


def render_metadata(md, length=40):
if md == b"":
return ""
return truncate_string_end(str(md), length)


def unicode_table(rows, title=None, header=None, row_separator=True):
"""
Convert a table (list of lists) of strings to a unicode table. If a row contains
Expand Down