Skip to content

Commit 2ac70d1

Browse files
committed
Improve string and html output
1 parent fb200e2 commit 2ac70d1

File tree

6 files changed

+237
-128
lines changed

6 files changed

+237
-128
lines changed

python/CHANGELOG.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44

55
**Breaking changes**
66

7-
- `Mutation.position` and `Mutation.index` which were deprecated in 0.2.2 (Sep '19) have
7+
- ``Mutation.position`` and ``Mutation.index`` which were deprecated in 0.2.2 (Sep '19) have
88
been removed.
99

1010
**Features**
1111

12-
- Add `Table.append` method for adding rows from classes such as `SiteTableRow` and
13-
`Site` (:user:`benjeffery`, :issue:`1111`, :pr:`1254`).
12+
- Add ``Table.append`` method for adding rows from classes such as ``SiteTableRow`` and
13+
``Site`` (:user:`benjeffery`, :issue:`1111`, :pr:`1254`).
1414

1515
- SVG visualization of a single tree allows all mutations on an edge to be plotted
1616
via the ``all_edge_mutations`` param (:user:`hyanwong`,:issue:`1253`, :pr:`1258`).
1717

18-
- Entity classes such as `Mutation`, `Node` are now python dataclasses
18+
- Entity classes such as ``Mutation``, ``Node`` are now python dataclasses
1919
(:user:`benjeffery`, :pr:`1261`).
2020

2121
- Metadata decoding for table row access is now lazy (:user:`benjeffery`, :pr:`1261`).
2222

23+
- Improve display of tables when ``print``ed, limiting lines to ``tskit.MAX_LINES``
24+
(:user:`benjeffery`,:issue:`1270`, :pr:`1300`)
2325

2426
**Fixes**
2527

python/tests/test_tables.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,16 @@ def test_str(self):
540540
table = self.table_class()
541541
table.set_columns(**input_data)
542542
s = str(table)
543-
assert len(s.splitlines()) == num_rows + 1
543+
assert len(s.splitlines()) == num_rows + 4
544+
input_data = self.make_input_data(41)
545+
table = self.table_class()
546+
table.set_columns(**input_data)
547+
print(table)
548+
assert "1 rows skipped" in str(table)
549+
tskit.set_print_options(max_lines=None)
550+
assert "1 rows skipped" not in str(table)
551+
tskit.set_print_options(max_lines=40)
552+
tskit.MAX_LINES = 40
544553

545554
def test_repr_html(self):
546555
for num_rows in [0, 10, 40, 50]:
@@ -555,8 +564,8 @@ def test_repr_html(self):
555564
if num_rows == 50:
556565
assert len(html.splitlines()) == num_rows + 11
557566
assert (
558-
html.split("</tr>")[21]
559-
== "\n<tr><td><em>... skipped 10 rows ...</em></td>"
567+
html.split("</tr>")[21] == "\n<tr><td><em>10 rows skipped "
568+
"(tskit.set_print_options)</em></td>"
560569
)
561570
else:
562571
assert len(html.splitlines()) == num_rows + 20

python/tests/test_util.py

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
import itertools
2727
import math
2828
import pickle
29+
import textwrap
2930

3031
import numpy as np
3132
import pytest
3233
from numpy.testing import assert_array_equal
3334

3435
import tests.tsutil as tsutil
36+
import tskit
3537
import tskit.util as util
3638
from tskit import UNKNOWN_TIME
3739

@@ -407,35 +409,83 @@ def test_obj_to_collapsed_html(obj, expected):
407409
)
408410

409411

412+
def test_truncate_string_end():
413+
assert util.truncate_string_end("testing") == "testing"
414+
assert util.truncate_string_end("testing", 7) == "testing"
415+
assert util.truncate_string_end("testing", 5) == "te..."
416+
417+
410418
def test_unicode_table():
411419
assert (
412420
util.unicode_table(
413421
[["5", "6", "7", "8"], ["90", "10", "11", "12"]],
414422
header=["1", "2", "3", "4"],
415423
)
416-
== """╔══╤══╤══╤══╗
417-
║1 │2 │3 │4 ║
418-
╠══╪══╪══╪══╣
419-
║5 │ 6│ 7│ 8║
420-
╟──┼──┼──┼──╢
421-
║90│10│11│12║
422-
╚══╧══╧══╧══╝
423-
"""
424+
== textwrap.dedent(
425+
"""
426+
╔══╤══╤══╤══╗
427+
║1 │2 │3 │4 ║
428+
╠══╪══╪══╪══╣
429+
║5 │ 6│ 7│ 8║
430+
╟──┼──┼──┼──╢
431+
║90│10│11│12║
432+
╚══╧══╧══╧══╝
433+
"""
434+
)[1:]
435+
)
436+
437+
assert (
438+
util.unicode_table(
439+
[
440+
["1", "2", "3", "4"],
441+
["5", "6", "7", "8"],
442+
"__skipped__",
443+
["90", "10", "11", "12"],
444+
],
445+
title="TITLE",
446+
)
447+
== textwrap.dedent(
448+
"""
449+
╔═══════════╗
450+
║TITLE ║
451+
╠══╤══╤══╤══╣
452+
║1 │ 2│ 3│ 4║
453+
╟──┼──┼──┼──╢
454+
║5 │ 6│ 7│ 8║
455+
╟──┴──┴──┴──╢
456+
║ rows skipp║
457+
╟──┬──┬──┬──╢
458+
║90│10│11│12║
459+
╚══╧══╧══╧══╝
460+
"""
461+
)[1:]
424462
)
425463

426464
assert (
427465
util.unicode_table(
428466
[["1", "2", "3", "4"], ["5", "6", "7", "8"], ["90", "10", "11", "12"]],
429467
title="TITLE",
468+
row_separator=False,
430469
)
431-
== """╔═══════════╗
432-
║TITLE ║
433-
╠══╤══╤══╤══╣
434-
║1 │ 2│ 3│ 4║
435-
╟──┼──┼──┼──╢
436-
║5 │ 6│ 7│ 8║
437-
╟──┼──┼──┼──╢
438-
║90│10│11│12║
439-
╚══╧══╧══╧══╝
440-
"""
470+
== textwrap.dedent(
471+
"""
472+
╔═══════════╗
473+
║TITLE ║
474+
╠══╤══╤══╤══╣
475+
║1 │ 2│ 3│ 4║
476+
║5 │ 6│ 7│ 8║
477+
║90│10│11│12║
478+
╚══╧══╧══╧══╝
479+
"""
480+
)[1:]
441481
)
482+
483+
484+
def test_set_printoptions():
485+
assert tskit._print_options == {"max_lines": 40}
486+
util.set_print_options(max_lines=None)
487+
assert tskit._print_options == {"max_lines": None}
488+
util.set_print_options(max_lines=40)
489+
assert tskit._print_options == {"max_lines": 40}
490+
with pytest.raises(TypeError):
491+
util.set_print_options(40)

python/tskit/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747
ALLELES_ACGT = ("A", "C", "G", "T")
4848

4949
#: Special NAN value used to indicate unknown mutation times
50-
"""
51-
Say what
52-
"""
5350
UNKNOWN_TIME = _tskit.UNKNOWN_TIME
5451

52+
#: Options for printing to strings and HTML, modify with tskit.set_print_options.
53+
_print_options = {"max_lines": 40}
54+
5555
from tskit.provenance import __version__ # NOQA
5656
from tskit.provenance import validate_provenance # NOQA
5757
from tskit.formats import * # NOQA

0 commit comments

Comments
 (0)