Steven Bethard wrote:
Michael Spencer wrote:

def output(seq, linelength = 60):
    if seq:
        iterseq = iter(seq)
        while iterseq:
            print "".join(islice(iterseq,linelength))


Worth noting: "while iterseq" only works because for this case, you have a list iterator, which provides a __len__ method.

Thanks! I had noted that the file iterator didn't behave like this, but I hadn't deduced the reason. Unfortunately, the above construct, while cute, is also not terribly speedy.


 >>> print "\n".join(body[-index:-index-linelength:-1]
 ...      for index in xrange(1, len(body), linelength))

is ugly but much faster with an already-existing string

So, my second attempt is:

from itertools import groupby

def revcomp2(input = sys.stdin, linelength = 60):
    basetable = string.maketrans('ACBDGHKMNSRUTWVYacbdghkmnsrutwvy',
                                 'TGVHCDMKNSYAAWBRTGVHCDMKNSYAAWBR')

    def record(item):
        return item[0] in ">;"

    for header, body in groupby(input, record):
        body = "".join(body)
        if header:
            print body,
        else:
            body = body.translate(basetable, "\n\r")
            print "\n".join(body[-index:-index-linelength:-1]
                for index in xrange(1, len(body), linelength))


Michael

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to