Skip to content

Commit ad4dd4a

Browse files
chriscrsmithmergify[bot]
authored andcommitted
implementing Variant.__repr__
added tests for contents edit changelog fix changelog entry
1 parent ba5d66a commit ad4dd4a

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

python/CHANGELOG.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
--------------------
2+
[0.5.5] - 2023-01-XX
3+
--------------------
4+
5+
**Features**
6+
7+
- Add ``__repr__`` for variants to return a string representation of the raw data
8+
without spewing megabytes of text (:user:`chriscrsmith`, :pr:`2695`, :issue:`2694`)
9+
10+
111
--------------------
212
[0.5.4] - 2023-01-13
313
--------------------

python/tests/test_genotypes.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MIT License
22
#
3-
# Copyright (c) 2019-2022 Tskit Developers
3+
# Copyright (c) 2019-2023 Tskit Developers
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -2215,3 +2215,17 @@ def test_variant_html_repr_no_site(self):
22152215
html = v._repr_html_()
22162216
ElementTree.fromstring(html)
22172217
assert len(html) > 1600
2218+
2219+
def test_variant_repr(self, ts_fixture):
2220+
v = next(ts_fixture.variants())
2221+
str_rep = repr(v)
2222+
assert len(str_rep) > 0 and len(str_rep) < 10000
2223+
assert re.search(r"\AVariant", str_rep)
2224+
assert re.search(rf"\'site\': Site\(id={v.site.id}", str_rep)
2225+
assert re.search(rf"position={v.position}", str_rep)
2226+
alleles = re.escape("'alleles': " + str(v.alleles))
2227+
assert re.search(rf"{alleles}", str_rep)
2228+
assert re.search(r"\'genotypes\': array\(\[", str_rep)
2229+
assert re.search(rf"position={v.position}", str_rep)
2230+
assert re.search(rf"\'has_missing_data\': {v.has_missing_data}", str_rep)
2231+
assert re.search(rf"\'isolated_as_missing\': {v.isolated_as_missing}", str_rep)

python/tskit/genotypes.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# MIT License
33
#
4-
# Copyright (c) 2018-2022 Tskit Developers
4+
# Copyright (c) 2018-2023 Tskit Developers
55
# Copyright (c) 2015-2018 University of Oxford
66
#
77
# Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -340,6 +340,17 @@ def _repr_html_(self) -> str:
340340
"""
341341
return util.variant_html(self)
342342

343+
def __repr__(self):
344+
d = {
345+
"site": self.site,
346+
"samples": self.samples,
347+
"alleles": self.alleles,
348+
"genotypes": self.genotypes,
349+
"has_missing_data": self.has_missing_data,
350+
"isolated_as_missing": self.isolated_as_missing,
351+
}
352+
return f"Variant({repr(d)})"
353+
343354

344355
#
345356
# Miscellaneous auxiliary methods.

0 commit comments

Comments
 (0)