Pyflakes: HUGE cleanup

It was quite a mess. Double imports, unused imports, imports that overwrite other imports.

Added a couple of 'TODO' comments, mostly for python's logfile system that gets initialized at the
top level of the module. Don't do that. Do that in the main() where you need it.
And don't set it in the tests.
Lots of pep8 cleanup
pep8 fixes. Indentation with 3 spaces is a bad idea.

Note on logging: logger.debug() already does its own variable replacement,
simply with %s (perhaps also with {}). No need to call .format.
Loads of pep8
Merged cleanup branch