Correctly encode db exceptions, minor cleanup.
Lint python code.