Python 3 Migration: Integer Division

One of the hardest parts in the migration process is to find incorrect integer divisions in the python 2.7 nupic code. Python 3 behaves differently as nicely described here.

Here are two locations in nupic which I needed to change and let me tell you it takes a long time to find those!

shifted = shifted / dimensions[i] 
to
shifted = shifted // dimensions[i]
left = right = start + runLen / 2
to
left = right = start + runLen // 2

Anyone has a good idea how to spot those issue? Right now, I need to step through code in two debugger windows side by side and compare data types and values…

Can you write a regex to find division in the code? Something simple like this can be used as a filter:

(\s+)?\/(\s+)?

57%20AM

1 Like

I think we need to find all the divisions where both operands are integers. That’s not something a regex can do. But the article mentions a way to change the division behavior in python 2.7. All a user has to do is to insert this line:

from __future__ import division

on top of a module and viola the old python 2.7 code has the same behavior as python 3.

Assuming the current nupic test suit runs through this would be the way to see where it breaks. I haven’t tried it out yet.