Future division patch available (PEP 238)

Skip Montanaro skip at pobox.com
Mon Aug 13 11:19:25 EDT 2001


    Ian> Clearly there'll be an effective tool to identify likely areas of
    Ian> concern, but I hope it won't generate too many false-positives (if
    Ian> that's the correct way round) and zero of whatever the opposite is.

I posted this once.  Maybe once again will be sufficient... ;-)

    import tokenize
    import sys

    class TokenSorter:
        def __init__(self, f):
            self.tokens = {}
            self.filename = f
            self.line = ""
            self.linenumber = 0
            self.lastprinted = 0

        def tokeneater(self, typ, val, (sr, sc), (er, ec), line):
            if self.line != line:
                self.linenumber += 1
                self.line = line
            if (tokenize.tok_name[typ] == "OP" and
                val == "/" and
                self.lastprinted != self.linenumber):
                print "%s(%d): %s" % (self.filename, self.linenumber,
                                       line.rstrip())
                self.lastprinted = self.linenumber

    def main():
        for fn in sys.argv[1:]:
            try:
                f = open(fn)
            except IOError:
                pass
            else:
                ts = TokenSorter(fn)
                try:
                    tokenize.tokenize(f.readline, ts.tokeneater)
                except tokenize.TokenError:
                    pass

    if __name__ == "__main__":
        main()

Run it like

    python finddiv.py *.py

Output looks like

    searches.py(23):     print "passed: %d, failed: %d, pass ratio: %.3f" % (p, f, float(p)/(p+f))
    searches.py(27):         print "%.3f queries per second" % ((p+f)/t)
    velocity.py(1): x = 1/2
    velocity.py(4):     return x/t

so you can easily step through the results with Emacs's next-error command.

-- 
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list