Various flake8 fixes including under Python 3
Python 3 changes are untested, except for a few ones where I added the module to tools/py3-compatible.modules
Python 3 changes are untested, except for a few ones where I added the module to tools/py3-compatible.modules