Skip to content

Commit

Permalink
Add asdict to Dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Jeffery authored and mergify[bot] committed Jul 11, 2023
1 parent a3b095f commit 555913c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion python/CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
--------------------
[0.5.X] - 2023-XX-XX
[0.5.6] - 2023-XX-XX
--------------------

**Features**

- Add ``TreeSequence.impute_unknown_mutations_time`` method to return an array of mutation times based on the times of associated nodes (:user:`duncanMR`, :pr:`2760`, :issue:`2758`)

- Add ``asdict`` to all dataclasses. These are returned when you access a row or
other tree sequence object. (:user:`benjeffery`, :pr:`2759`, :issue:`2719`)

--------------------
[0.5.5] - 2023-05-17
--------------------
Expand Down
8 changes: 8 additions & 0 deletions python/tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ def table_5row(self, test_rows):
table_5row.add_row(**row)
return table_5row

def test_asdict(self, table, test_rows):
for table_row, test_row in zip(table, test_rows):
for k, v in table_row.asdict().items():
if isinstance(v, np.ndarray):
assert np.array_equal(v, test_row[k])
else:
assert v == test_row[k]

def test_max_rows_increment(self):
for bad_value in [-1, -(2**10)]:
with pytest.raises(ValueError):
Expand Down
7 changes: 7 additions & 0 deletions python/tskit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def replace(self, **kwargs):
"""
return dataclasses.replace(self, **kwargs)

def asdict(self, **kwargs):
"""
Return a new dict which maps field names to their corresponding values
in this dataclass.
"""
return dataclasses.asdict(self, **kwargs)


def canonical_json(obj):
"""
Expand Down

0 comments on commit 555913c

Please sign in to comment.