Search code examples
pythontornadopylintpychecker

Is there a good way to apply pychecker/pylint to the python code in Tornado templates?


I am using Tornado 2.0 (Python 2.6.5) to build a simple web app.

Naturally, my Tornado templates contain snippets of Python code. For my non-template code, I am using pychecker and pylint to check for errors, etc.

However, obviously pychecker and pylint can't be run over the templates directly, b/c the templates are not python files proper (for non-Tornado users: they are html-like snippets w/ some control sequences and embedded python code).

So, my question is: can anyone suggest a good way to apply pychecker/pylint to the python code in these template files? Presumably this would involve extracting the code from the files.

I can hazard a few guesses as to how to do this, but I am curious if other people perceive this as a problem and what solutions they have pursued. I am still fairly new to web app design/construction so I am interested in answers guided by practical experience.


Solution

  • You need to use view class pattern to avoid cluttering your template with Python code which cannot be analyzed.

    • Create a Python class to process your view, instead of function

    • Have all "template logic" code as class methods. Your template calls them like {{ view.get_full_name }} and def get_full_name(self): return self.item.first_name + " " + self.item.last_name

    • Make instance out of your class

    • make call() as starting point for your processing

    • Pass "self" to your template as a context var

    Some instructions for Django, but generally all Python frameworks (Pyramid, Zope) follow the same pattern:

    Class views in Django

    "$yourframeworkname view class" should yield more tutorials in Google.