开发者

avoiding code duplication in Python code redux

This is a followup to an earlier question. I got some good suggestions for that, 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 开发者_如何学Python= i+1
            bf.write(out.strip('"')+'\n')

Is it possible to compactify this code? If I have some stuff in common in two loops like this, one obvious possibility is to just factor out the common code, but here, eww. The annoying thing is that the only difference here is the writing to c.

Brief summary of code: If K is not None, then loop over K lines of a and write to both b and c. Otherwise, loop over all of a and just write to b.


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

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)


One check, one loop, no classes, psyco-optimizable.

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)


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)


How about this (second class based 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜