Skip to content

Commit

Permalink
Merge pull request #552 from GavinHuttley/develop
Browse files Browse the repository at this point in the history
ENH: added str, repr dunder methods for table.Columns
  • Loading branch information
GavinHuttley committed Mar 4, 2020
2 parents cc58162 + 61a8898 commit 4b715e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cogent3/util/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,18 @@ def __setstate__(self, data):

self.__dict__.update(new.__dict__)

def __repr__(self):
d = [f"'{c}': {v.dtype}" for c, v in self.items()]
num = len(d)
v = d[:5]
if num > 5:
v.append(f"... + {num - 5} more")
txt = f"{self.__class__.__name__}({', '.join(v)})"
return txt

def __str__(self):
return repr(self)

def iter_rows(self):
columns = [self[c] for c in self]
for row in zip(*columns):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_util/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ def test_index_name(self):
with self.assertRaises(ValueError):
t.index_name

def test_column_repr_str(self):
"""repr and str of Columns"""
t = Table(header=list("abcdefg"), data=[[0, 1.1, 2, 3, 4, 5.5, 6.0]])
r = repr(t.columns)
s = str(t.columns)
self.assertIsInstance(r, str)
self.assertEqual(r, s)

def test_slicing_columns(self):
"""works using names, ints, bool array"""
t = Table(header=self.t5_header, data=self.t5_rows)
Expand Down

0 comments on commit 4b715e1

Please sign in to comment.