Skip to content

Commit 1d5fd5d

Browse files
committed
Change __repr__() methods for ASI classes.
1 parent 103f491 commit 1d5fd5d

File tree

3 files changed

+61
-18
lines changed

3 files changed

+61
-18
lines changed

pyvdrm/asi2.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class Score(object):
4444
score = None
4545

4646
def __init__(self, score, residues):
47+
""" Initialize.
48+
49+
:param bool|float score: value of the score
50+
:param residues: sequence of Mutations
51+
"""
4752
self.score = score
4853
self.residues = set(residues)
4954

@@ -54,7 +59,7 @@ def __sub__(self, other):
5459
return Score(self.score - other.score, self.residues | other.residues)
5560

5661
def __repr__(self):
57-
return str(self.score)
62+
return "Score({!r}, {!r})".format(self.score, self.residues)
5863

5964
def __eq__(self, other):
6065
return self.score == other.score
@@ -130,9 +135,6 @@ def __call__(self, x):
130135

131136
raise NotImplementedError
132137

133-
def __repr__(self):
134-
return "({} {})".format(self.operation, self.limit)
135-
136138

137139
class ScoreExpr(AsiExpr):
138140
"""Score expressions propagate DRM scores"""
@@ -158,10 +160,6 @@ def __call__(self, mutations):
158160
return Score(0, [])
159161
return Score(score, result.residues)
160162

161-
def __repr__(self):
162-
operation, score = self.children
163-
return "{}=>{}".format(str(operation), str(score))
164-
165163

166164
class ScoreList(AsiExpr):
167165
"""Lists of scores are either summed or maxed"""
@@ -210,15 +208,15 @@ def __call__(self, args):
210208
class AsiMutations(object):
211209
"""List of mutations given an ambiguous pattern"""
212210

213-
def __init__(self, pos, label, args):
211+
def __init__(self, _label=None, _pos=None, args=None):
214212
"""Initialize set of mutations from a potentially ambiguous residue
215213
"""
216-
if pos and label:
217-
pass
218-
self.mutations = MutationSet(''.join(args))
214+
self.mutations = args and MutationSet(''.join(args))
219215

220216
def __repr__(self):
221-
return str(self.mutations)
217+
if self.mutations is None:
218+
return "AsiMutations()"
219+
return "AsiMutations(args={!r})".format(str(self.mutations))
222220

223221
def __call__(self, env):
224222
intersection = set(env) & self.mutations.mutations

pyvdrm/drm.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ def __call__(self, args):
5656
"""Evaluate child tokens with args"""
5757
return self.children(args)
5858

59-
def __repr__(self):
60-
"""Pretty print syntax tree"""
61-
return str(type(self))
62-
6359

6460
class AsiBinaryExpr(AsiExpr):
6561
"""Subclass with syntactic sugar for boolean ops"""

pyvdrm/tests/test_asi2.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22
from functools import reduce
3-
from pyvdrm.asi2 import ASI2
3+
from pyvdrm.asi2 import ASI2, AsiMutations, Score
44
from pyvdrm.vcf import Mutation, MutationSet
55

66

@@ -128,5 +128,54 @@ def test_chained_and(self):
128128
self.assertEqual(rule(mus("40F 67G 215Y")), 15)
129129

130130

131+
class TestAsiMutations(unittest.TestCase):
132+
def test_init_args(self):
133+
expected_mutation_set = MutationSet('Q80KR')
134+
m = AsiMutations(args='Q80KR')
135+
136+
self.assertEqual(expected_mutation_set, m.mutations)
137+
self.assertEqual(expected_mutation_set.wildtype, m.mutations.wildtype)
138+
139+
def test_init_none(self):
140+
m = AsiMutations()
141+
142+
self.assertIsNone(m.mutations)
143+
144+
def test_repr(self):
145+
expected_repr = "AsiMutations(args='Q80KR')"
146+
m = AsiMutations(args='Q80KR')
147+
148+
r = repr(m)
149+
150+
self.assertEqual(expected_repr, r)
151+
152+
def test_repr_none(self):
153+
expected_repr = "AsiMutations()"
154+
m = AsiMutations()
155+
156+
r = repr(m)
157+
158+
self.assertEqual(expected_repr, r)
159+
160+
161+
class TestScore(unittest.TestCase):
162+
def test_init(self):
163+
expected_value = 10
164+
expected_mutations = {Mutation('A23R')}
165+
166+
score = Score(expected_value, expected_mutations)
167+
168+
self.assertEqual(expected_value, score.score)
169+
self.assertEqual(expected_mutations, score.residues)
170+
171+
def test_repr(self):
172+
expected_repr = "Score(10, {Mutation('A23R')})"
173+
score = Score(10, {Mutation('A23R')})
174+
175+
r = repr(score)
176+
177+
self.assertEqual(expected_repr, r)
178+
179+
131180
if __name__ == '__main__':
132181
unittest.main()

0 commit comments

Comments
 (0)