Avoiding code duplication in Python redux code

This is a question for an earlier question. I have some good suggestions for this, so I thought I would try my luck again.

from itertools import takewhile if K is None: illuminacond = lambda x: x.split(',')[0] != '[Controls]' else: illuminacond = lambda x: x.split(',')[0] != '[Controls]' and i < K af=open('a') bf=open('b', 'w') cf=open('c', 'w') i = 0 if K is None: for line in takewhile(illuminacond, af): line_split=line.split(',') pid=line_split[1][0:3] out = line_split[1] + ',' + line_split[2] + ',' + line_split[3][1] + line_split[3][3] + ',' \ + line_split[15] + ',' + line_split[9] + ',' + line_split[10] if pid!='cnv' and pid!='hCV' and pid!='cnv': i = i+1 bf.write(out.strip('"')+'\n') cf.write(line) else: for line in takewhile(illuminacond, af): line_split=line.split(',') pid=line_split[1][0:3] out = line_split[1] + ',' + line_split[2] + ',' + line_split[3][1] + line_split[3][3] + ',' \ + line_split[15] + ',' + line_split[9] + ',' + line_split[10] if pid!='cnv' and pid!='hCV' and pid!='cnv': i = i+1 bf.write(out.strip('"')+'\n') 

Can I code this code? If I have something in common in two loops like this, one obvious possibility is to simply align the common code, but here, eW. It’s annoying that the only difference here is in spelling c .

Short code summary: If K not None, then iterate over K strings a and write both b and c . Otherwise, flip all a and simply write to b .

+4
source share
4 answers

One check, one loop, no classes, optimized for psycco.

 from itertools import takewhile if K is None: illuminacond = lambda x: x.split(',')[0] != '[Controls]' def action(cf, line): cf.write(line) else: illuminacond = lambda x: x.split(',')[0] != '[Controls]' and i < K def action(cf, line): pass af=open('a') bf=open('b', 'w') cf=open('c', 'w') i = 0 for line in takewhile(illuminacond, af): line_split=line.split(',') pid=line_split[1][0:3] out = line_split[1] + ',' + line_split[2] + ',' + line_split[3][1] + line_split[3][3] + ',' \ + line_split[15] + ',' + line_split[9] + ',' + line_split[10] if pid!='cnv' and pid!='hCV' and pid!='cnv': i = i+1 bf.write(out.strip('"')+'\n') action(cf, line) 
+2
source

Why not use only one loop, but including a condition inside this loop? Also, I think you can get rid of redundancy in this lambda.

 from itertools import takewhile k_is_none = K is None def illuminacond(x): global i global K result = x.split(',')[0] != '[Controls]' if not k_is_none: result = result and i < K return result af=open('a') bf=open('b', 'w') cf=open('c', 'w') i = 0 for line in takewhile(illuminacond, af): line_split=line.split(',') pid=line_split[1][0:3] out = line_split[1] + ',' + line_split[2] + ',' + line_split[3][1] + line_split[3][3] + ',' \ + line_split[15] + ',' + line_split[9] + ',' + line_split[10] if pid!='cnv' and pid!='hCV' and pid!='cnv': i = i+1 bf.write(out.strip('"')+'\n') if k_is_none: cf.write(line) 
+3
source

Why not just:

 from itertools import takewhile illuminacond = lambda x: x.split(',')[0] != '[Controls]' and (K is None or i<K) #i'm not so sure about this part, confused me a little :). af=open('a') bf=open('b', 'w') cf=open('c', 'w') for line in takewhile(illuminacond, af): line_split=line.split(',') pid=line_split[1][0:3] out = line_split[1] + ',' + line_split[2] + ',' + line_split[3][1] + line_split[3][3] + ',' \ + line_split[15] + ',' + line_split[9] + ',' + line_split[10] if pid!='cnv' and pid!='hCV' and pid!='cnv': i = i+1 bf.write(out.strip('"')+'\n') if K is None: cf.write(line) 
+1
source

How about this (second class version)?

 from itertools import takewhile class Foo: def __init__(self, K = None): self.bf=open('b', 'w') self.cf=open('c', 'w') self.count = 0 self.K = K def Go(self): for self.line in takewhile(self.Lamda(), open('a')): self.SplitLine() if self.IsValidPid(): self.WriteLineToFiles() def SplitLine(self): self.lineSplit=self.line.split(',') def Lamda(self): if self.K is None: return lambda x: x.split(',')[0] != '[Controls]' else: return lambda x: x.split(',')[0] != '[Controls]' and self.count < self.K def IsValidPid(self): pid=self.lineSplit[1][0:3] return pid!='cnv' and pid!='hCV' and pid!='cnv' def WriteLineToFiles(self): self.count += 1 self.bf.write(self.ParseLine()) if self.K is None: self.cf.write(self.line) def ParseLine(self): return (self.lineSplit[1] + ',' + self.lineSplit[2] + ',' + self.lineSplit[3][1] + self.lineSplit[3][3] + ',' + self.lineSplit[15] + ',' + self.lineSplit[9] + ',' + self.lineSplit[10]).strip('"')+'\n' Foo().Go() 

Original version:

 from itertools import takewhile if K is None: illuminacond = lambda x: x.split(',')[0] != '[Controls]' else: illuminacond = lambda x: x.split(',')[0] != '[Controls]' and i < K def Parse(line): return (line[1] + ',' + line[2] + ',' + line[3][1] + line[3][3] + ',' + line[15] + ',' + line[9] + ',' + line[10]).strip('"')+'\n' def IsValidPid(line_split): pid=line_split[1][0:3] return pid!='cnv' and pid!='hCV' and pid!='cnv' bf=open('b', 'w') cf=open('c', 'w') def WriteLineToFiles(line, line_split): bf.write(Parse(line_split)) if K is None: cf.write(line) i = 0 for line in takewhile(illuminacond, open('a')): line_split=line.split(',') if IsValidPid(line_split): WriteLineToFiles(line, line_split) i += 1 
+1
source

All Articles