PythonTidy does not lint or validate Python source code – it rewrites it to follow PEP-8 (and other) configured rules.
PythonTidy is very useful if you need to
- clean up old codebases and make them to embrace PEP-8
- work with people who do not yet follow strict programming disciplines (it helps to convert them if you can make them use a text editor which highlights the errors on background)
Adhering PEP-8 is also must when you are using a text editor which highlights linting errors in your source code while typing. If your source code is full of unneeded style errors you’ll miss the actual linting errors among them.
PythonTidy was created by Chuck Rhode.
1. PythonTidy plug-in for Sublime Text 2
The basic PythonTidy works from the command line. However, in this blog I show how to use PythonTidy plugin, by Andreas Zeidler, in Sublime Text 2 code editor with one key press integration. Sublime Text 2 has very powerful Python linting support through SublimeLinterl package, highlighting style and linting errors when you type.
First install PythonTidy using git (note: Sublime Text 2 own package control installer does not work due to usage of Git submodules, you need to do this from command line).
Below is a screenshot with two style errors (no space after comma, bad number of newlines between module main level code blocks).
Now you simply press CONTRL+CMD+ALT+T: your source is fixed:
2. Configuring PythonTidy for Sublime Text 2
PythonTidy uses ~/.pythontidy.xml configuration file. A default setting value which most people probably want to change is the line length – PythonTidy shortens your line to 76 characters by default as recommended by PEP-8, and this is really quite short for the modern widescreens, wasting a lot of space. (I know there are some crazy people using tmux + vim + some other terminal stuff from golden early 80s and splitting their screen to 12 mini squares, but most of us don’t do that…)
<config> <parm name="COL_LIMIT" value="int(1000)" /> <parm name="SHEBANG" value="" /> <parm name="PARENTHESIZE_TUPLE_DISPLAY" value="bool(False)" /> <parm name="ADD_BLANK_LINES_AROUND_COMMENTS" value="bool(False)" /> <parm name="ADD_BLANK_LINE_AFTER_DOC_STRING" value="bool(False)" /> <parm name="MAX_SEPS_FUNC_DEF" value="int(99)" /> <parm name="MAX_SEPS_FUNC_REF" value="int(99)" /> <parm name="WRAP_DOC_STRINGS" value="bool(False)" /> <parm name="NORMALIZE_DOC_STRINGS" value="bool(False)" /> </config>
Note: Looks like you cannot add XML comments in config for now.
3. Other text editors
Please post as a comment if you know any other text editors with PythonTidy or background PEP-8′ing support. I am especially interest how one could intergrate this with Eclipse + PyDev.