Using autopep8 (https://github.com/hhatto/autopep8) on all py files.
Some lines were changed manually (those which may change the logic).

Have not fixed E501 line too longs, these are the only pep8 failures.

Check current pep8 compliance of all py files in current directory:
for f in *.py; do pep8 $f; done

Run autopep8 on py files in current directory:
for f in *.py; do autopep8 -i $f; done
Reverted 4 test failures from autopep8 commit:
66fc98f0b7316331b505c4c4d4929fcbdc668d86

Arising from correcting PEP8 E712, changing == boolean to is boolean.
Possible that these are bugs in the tests?
shortened lines over 140 chars where appropriate
Merge PR #2632