Skip to content

Commit 516d100

Browse files
committed
Fix a bunch of PyCharm warnings, and add some missing tests.
1 parent 413cac3 commit 516d100

File tree

6 files changed

+103
-79
lines changed

6 files changed

+103
-79
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/.idea/dictionaries
12
/.idea/misc.xml
23
/.idea/workspace.xml
34

pyvdrm/asi2.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
'''
1+
"""
22
ASI2 Parser definition
3-
'''
3+
"""
44

55
from functools import reduce, total_ordering
66
from pyparsing import (Literal, nums, Word, Forward, Optional, Regex,
77
infixNotation, delimitedList, opAssoc)
88
from pyvdrm.drm import AsiExpr, AsiBinaryExpr, AsiUnaryExpr, DRMParser
99
from pyvdrm.vcf import MutationSet
1010

11+
1112
def maybe_foldl(func, noneable):
12-
'''Safely fold a function over a potentially empty list of
13-
potentially null values'''
13+
"""Safely fold a function over a potentially empty list of
14+
potentially null values"""
1415
if noneable is None:
1516
return None
1617
clean = [x for x in noneable if x is not None]
17-
if clean == []:
18+
if not clean:
1819
return None
1920
return reduce(func, clean)
2021

22+
2123
def maybe_map(func, noneable):
2224
if noneable is None:
2325
return None
@@ -29,14 +31,14 @@ def maybe_map(func, noneable):
2931
if result is None:
3032
continue
3133
r_list.append(result)
32-
if r_list == []:
34+
if not r_list:
3335
return None
3436
return r_list
3537

3638

3739
@total_ordering
3840
class Score(object):
39-
'''Encapsulate a score and the residues that support it'''
41+
"""Encapsulate a score and the residues that support it"""
4042

4143
residues = set([])
4244
score = None
@@ -68,32 +70,32 @@ def __bool__(self):
6870

6971

7072
class Negate(AsiUnaryExpr):
71-
'''Unary negation of boolean child'''
73+
"""Unary negation of boolean child"""
7274
def __call__(self, mutations):
7375
arg = self.children(mutations)
7476
return Score(not arg.score, arg.residues)
7577

7678

7779
class AndExpr(AsiExpr):
78-
'''Fold boolean AND on children'''
80+
"""Fold boolean AND on children"""
7981

8082
def __call__(self, mutations):
8183
scores = map(lambda f: f(mutations), self.children[0])
8284
scores = [Score(False, []) if s is None else s for s in scores]
83-
if scores == []:
85+
if not scores:
8486
raise ValueError
8587

8688
residues = set([])
8789
for s in scores:
88-
if s.score == False:
90+
if not s.score:
8991
return Score(False, [])
9092
residues = residues | s.residues
9193

9294
return Score(True, residues)
9395

9496

9597
class OrExpr(AsiBinaryExpr):
96-
'''Boolean OR on children (binary only)'''
98+
"""Boolean OR on children (binary only)"""
9799

98100
def __call__(self, mutations):
99101
arg1, arg2 = self.children
@@ -111,9 +113,10 @@ def __call__(self, mutations):
111113

112114

113115
class EqualityExpr(AsiExpr):
114-
'''ASI2 inequality expressions'''
116+
"""ASI2 inequality expressions"""
115117

116118
def __init__(self, label, pos, children):
119+
super().__init__(label, pos, children)
117120
self.operation, limit = children
118121
self.limit = int(limit)
119122

@@ -132,10 +135,9 @@ def __repr__(self):
132135

133136

134137
class ScoreExpr(AsiExpr):
135-
'''Score expressions propagate DRM scores'''
138+
"""Score expressions propagate DRM scores"""
136139

137140
def __call__(self, mutations):
138-
operation, score = (None, None)
139141
if len(self.children) == 3:
140142
operation, minus, score = self.children
141143
if minus != '-':
@@ -162,23 +164,23 @@ def __repr__(self):
162164

163165

164166
class ScoreList(AsiExpr):
165-
'''Lists of scores are either summed or maxed'''
167+
"""Lists of scores are either summed or maxed"""
166168

167169
def __call__(self, mutations):
168170
operation, *rest = self.children
169171
if operation == 'MAX':
170172
return maybe_foldl(max, [f(mutations) for f in rest])
171173

172174
# the default operation is sum
173-
return maybe_foldl(lambda x,y: x+y, [f(mutations) for f in self.children])
175+
return maybe_foldl(lambda x, y: x+y, [f(mutations) for f in self.children])
174176

175177

176178
class SelectFrom(AsiExpr):
177-
'''Return True if some number of mutations match'''
179+
"""Return True if some number of mutations match"""
178180

179181
def typecheck(self, tokens):
180-
# if type(tokens[0]) != EqualityExpr:
181-
# raise TypeError
182+
# if type(tokens[0]) != EqualityExpr:
183+
# raise TypeError()
182184
pass
183185

184186
def __call__(self, mutations):
@@ -190,27 +192,27 @@ def __call__(self, mutations):
190192

191193
if operation(passing):
192194
return Score(True, maybe_foldl(
193-
lambda x,y: x.residues.union(y.residues), scored))
195+
lambda x, y: x.residues.union(y.residues), scored))
194196
else:
195197
return None
196198

197199

198200
class AsiScoreCond(AsiExpr):
199-
'''Score condition'''
201+
"""Score condition"""
200202

201203
label = "ScoreCond"
202204

203205
def __call__(self, args):
204-
'''Score conditions evaluate a list of expressions and sum scores'''
205-
return maybe_foldl(lambda x,y: x+y, map(lambda x: x(args), self.children))
206+
"""Score conditions evaluate a list of expressions and sum scores"""
207+
return maybe_foldl(lambda x, y: x+y, map(lambda x: x(args), self.children))
206208

207209

208210
class AsiMutations(object):
209-
'''List of mutations given an ambiguous pattern'''
211+
"""List of mutations given an ambiguous pattern"""
210212

211213
def __init__(self, pos, label, args):
212-
'''Initialize set of mutations from a potentially ambiguous residue
213-
'''
214+
"""Initialize set of mutations from a potentially ambiguous residue
215+
"""
214216
if pos and label:
215217
pass
216218
self.mutations = MutationSet.from_string(''.join(args))
@@ -226,7 +228,7 @@ def __call__(self, env):
226228

227229

228230
class ASI2(DRMParser):
229-
'''ASI2 Syntax definition'''
231+
"""ASI2 Syntax definition"""
230232

231233
def parser(self, rule):
232234

@@ -241,7 +243,7 @@ def parser(self, rule):
241243

242244
and_ = Literal('AND').suppress()
243245
or_ = Literal('OR').suppress()
244-
#min_ = Literal('MIN')
246+
# min_ = Literal('MIN')
245247

246248
notmorethan = Literal('NOTMORETHAN')
247249
l_par = Literal('(').suppress()
@@ -256,8 +258,8 @@ def parser(self, rule):
256258
not_.setParseAction(Negate)
257259

258260
residue = mutation | not_
259-
# integer + l_par + not_ + Regex(r'[A-Z]+') + r_par
260-
# roll this next rule into the mutation object
261+
# integer + l_par + not_ + Regex(r'[A-Z]+') + r_par
262+
# roll this next rule into the mutation object
261263

262264
# Syntax of ASI expressions
263265
excludestatement = except_ + residue
@@ -286,11 +288,11 @@ def parser(self, rule):
286288
scoreitem = booleancondition + mapper + Optional(Literal('-')) + integer
287289
scoreitem.setParseAction(ScoreExpr)
288290
scorelist = max_ + l_par + delimitedList(scoreitem) + r_par |\
289-
delimitedList(scoreitem)
291+
delimitedList(scoreitem)
290292
scorelist.setParseAction(ScoreList)
291293

292294
scorecondition = Literal('SCORE FROM').suppress() +\
293-
l_par + delimitedList(scorelist) + r_par
295+
l_par + delimitedList(scorelist) + r_par
294296

295297
scorecondition.setParseAction(AsiScoreCond)
296298

pyvdrm/drm.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
'''Asi2 specification
2-
'''
1+
"""Asi2 specification
2+
"""
33

44
from abc import ABCMeta, abstractmethod
55

6+
67
class AsiParseError(Exception):
78
pass
89

910

1011
class DRMParser(metaclass=ABCMeta):
11-
'''abstract class for DRM rule parsers/evaluators'''
12+
"""abstract class for DRM rule parsers/evaluators"""
1213

1314
def __init__(self, rule):
14-
'''drug resistance mutation callers are initialized with rule strings,
15+
"""drug resistance mutation callers are initialized with rule strings,
1516
the initialized parser has a callable decision tree object
16-
'''
17+
"""
1718
self.rule = rule
1819
self.dtree, *rest = self.parser(rule)
1920

2021
@abstractmethod
2122
def parser(self, rule_string):
22-
'''The parser returns a decision tree based on the rule string'''
23+
"""The parser returns a decision tree based on the rule string"""
2324
pass
2425

2526
def __call__(self, mutations):
@@ -33,13 +34,13 @@ def __repr__(self):
3334

3435

3536
class AsiExpr(object):
36-
'''A callable ASI2 expression'''
37+
"""A callable ASI2 expression"""
3738

3839
children = []
3940
label = None
4041

41-
def __init__(self, label, pos, tokens):
42-
'''By default we assume the head of the arg list is the operation'''
42+
def __init__(self, _label, _pos, tokens):
43+
"""By default we assume the head of the arg list is the operation"""
4344

4445
self.typecheck(tokens.asList())
4546
self.children = tokens
@@ -48,22 +49,23 @@ def __init__(self, label, pos, tokens):
4849
self.label = str(type(self))
4950

5051
def typecheck(self, tokens):
51-
'''Override typecheck method to define runtime errors'''
52+
"""Override typecheck method to define runtime errors"""
5253
pass
5354

5455
def __call__(self, args):
55-
'''Evaluate child tokens with args'''
56+
"""Evaluate child tokens with args"""
5657
return self.children(args)
5758

5859
def __repr__(self):
59-
'''Pretty print syntax tree'''
60+
"""Pretty print syntax tree"""
6061
return str(type(self))
6162

6263

6364
class AsiBinaryExpr(AsiExpr):
64-
'''Subclass with syntactic sugar for boolean ops'''
65+
"""Subclass with syntactic sugar for boolean ops"""
6566

6667
def __init__(self, label, pos, tokens):
68+
super().__init__(label, pos, tokens)
6769
self.children = tokens[0]
6870

6971
def typecheck(self, tokens):
@@ -76,7 +78,7 @@ def __repr__(self):
7678

7779

7880
class AsiUnaryExpr(AsiExpr):
79-
'''Subclass for atoms and unary ops'''
81+
"""Subclass for atoms and unary ops"""
8082

8183
def typecheck(self, tokens):
8284
if isinstance(tokens[0], list):

0 commit comments

Comments
 (0)