DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Help Teach Programming Disciplines with PythonTidy

Mikko Ohtamaa user avatar by
Mikko Ohtamaa
·
Jan. 29, 13 · Interview
Like (0)
Save
Tweet
Share
3.42K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Sublime Text

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Factors When Selecting a Database
  • Better Performance and Security by Monitoring Logs, Metrics, and More
  • Deploying Java Serverless Functions as AWS Lambda
  • Bye Bye, Regular Dev [Comic]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: