Future division patch available (PEP 238)

Ian Parker parker at gol.com
Tue Aug 14 16:04:57 CEST 2001


In article <mailman.997716022.20626.python-list at python.org>, Skip
Montanaro <skip at pobox.com> writes
>
>    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.
>

Thanks Skip- what an excellent piece of code. I'd imagine it'd need to
be put in the FAQ and still posted several more times.  

With script plus a little surrounding verbiage, my possibly affected 13
of 19 files scanned came up as:

"19 .py files scanned, 4 affected with 9 instances of the division
operator."

Now that is much more reasonable, but it is still almost 25% of the
modules, and I still, if I understand correctly, need to decide which
ones should be integer division.  

For a large project, that might amount to a significant amount of work:
checking, fixing, testing, regression testing, UAT, change control and
releasing.  

Of course, I was brought up on the idea of integer division giving an
integer result, so perhaps I use it rather freely in my code.  Looking
at some other packages, I see less than 10% of files containing the
operator, but even that still implies substantial work for someone.

Regards

Ian
-- 
Ian Parker



More information about the Python-list mailing list