Skip to content

Commit

Permalink
Merge pull request #571 from GavinHuttley/develop
Browse files Browse the repository at this point in the history
BUG: Table.tolist() now respects input column order
  • Loading branch information
GavinHuttley committed Mar 12, 2020
2 parents 797bfde + bf168cd commit 722cb90
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/cogent3/util/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1710,11 +1710,8 @@ def tolist(self, columns=None):
if len(columns) == 1:
result = self.columns[columns[0]].tolist()
return result
if set(columns) == set(self.columns.order):
subtable = self
else:
subtable = self[:, columns]

subtable = self.get_columns(columns)
result = subtable.columns.array.tolist()

return result
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 @@ -709,6 +709,14 @@ def test_tolist(self):
self.assertEqual(t3.tolist("id"), [6, 7])
self.assertEqual(t3.tolist("foo"), ["abc", "bca"])

def test_tolist_column_order(self):
"""column order of input reflected in result"""
t3 = Table(header=self.t3_header, data=self.t3_rows)
rev_order = ["id", "foo", "bar"]
rev_order.reverse()
result = t3.tolist(rev_order)
self.assertEqual(result[0], list(reversed(self.t3_rows[0][:])))

def test_to_dict(self):
"""cast to 2D dict"""
t = Table(header=self.t7_header, data=self.t7_rows, digits=1)
Expand Down

0 comments on commit 722cb90

Please sign in to comment.