Middleware.py fix

moved import to beginning of file, as it's requested for the inheritance, and
was breaking a autodoc by sphinx.
Minor other corrections for pep8 compliancy