Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Help Teach Programming Disciplines with PythonTidy

DZone's Guide to

Help Teach Programming Disciplines with PythonTidy

· DevOps Zone
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

PythonTidy is a utility to reformat Python source code according to Python PEP-8 style guide. It cleans up your source code and makes it adhere the community recommended practices.

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 errorsin 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 throughSublimeLinterl 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…)

Configuration options are explained in PythonTidyWrapper.py. More example settings are available in this Gist. Here is what I threw in the config:

<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.

Also more Sublime Text 2 tips for Python developers.

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:

Published at DZone with permission of Mikko Ohtamaa, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}