Clean up Python source code with PythonTidy, available as Sublime Text 2 plugin too

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

Configuration options are explained in More example settings are available in this Gist. Here is what I threw in the 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)" />

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.



\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+

pgrep and pkill – Linux scripting process management friends

Often when inspecting an UNIX server as a sysadmin you need to find and kill a (hung) process running with a certain command line arguments.

Traditional solution is to search through ps process listing output using grep command:

ps -Af|grep -i "xcmd"
... list of all processes running on the server matching xcmd here ...

Even though chaining commands is something UNIX philosophy encourages there exist specific commands for finding processes and killing them. pgrep searches from processes and pkill kills them. The sister commands use the same syntax.

Here is an example. We have a cron script which will open a SSH tunnel for the background. Then this tunnel is used to communicate with remote server by other processes in the same script. Sometimes, however, SSH tunnel can hang due to network reasons. The tunnel is started with command in this is used in one of /etc/cron.d scripts:

# daemon mode + do not open remote shell
ssh -fN -L

(If you are planning to create more robust solution it is suggested to use lock files instead where the process id is written each time the background process is started. However, here we discuss about the solution which is quick to implement and easy to understand.)

First we note this problem by inspecting the process list by the traditional means:

ps -Af|grep -i ssh

And in the output we see the bad process – we verify it is not working as the tunneled port indeed does not communicate with the remote server:

root      8292     1  0 09:37 ?        00:00:00 ssh -L -fN

Now we can see if pgrep also can find this process by matching full command line (-f switch)

pgrep -f 'ssh'

And now, we simply put a command at the beginning of our cron script to kill the previous tunnel, regardless if it is hung or not, wen running the script. Please note that the side effect is that this will kill all other SSH commands with the same command line on the server: in our case it does not matter, as the tunnel should be only started and maintained by this one cron script.


# Kill existing pipes if they are hung
pkill -f 'ssh'

ssh -L -fN

# script goes here...



\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+

Creating animated GIF screen captures on OSX

Animated GIF images are a good method for demonstrating your application, library or framework UI functionality and features: as one picture tells more than thousand words, one animation tells more than your average megapolis telephone directory.

(The example screen capture courtesy of jQuery Interdependencies)

The benefits of animated GIFs

  • You can embed them almost anywhere and they just work (even on Github README)
  • Autoplay! When your clip is 1-3 seconds you really don’t want to put it behind play button.
  • Animated GIFs compress OK’ish on graphical content where there are big plain color areas
  • Animated GIFs work well on lower frate rates

Here are short instructions how to create animated GIF screen recordings on OSX.

1. Record video using QuickTime Player

Screencast videos can be recorded with OSX’s own QuickTime player – no additional applications needed (since OSX Lion?). QuickTime player works scaring well. Just choose File > New Screen Recording and choose the area which you want to record

2. Converting the screen capture video to animated GIF

The resulting h264 MPEG5 as .mov file needs to be post-processed to animated GIF. I found GIFBrewery (~5 USD) suitable for this purpose. You could do it by hand with free software, but GIFBrewery provides nice “tune settings and see live preview” style functionality which saves a lot of your valuable time.

Note: I had a bug in GIFBrewery where the animation colors got screwed up if I tried to to move starting frame (probably related to h264 key framing?).

3. Crop and shrink the capture area

Try to make the target area as small as possible. Resize your browser window, zoom out fonts, use lower resolution and such tricks.

You can do further cropping in GIFBrewery.

4. Use low FPS

Set animated GIF frames per second to low number like 3 -5 frames per second. FPS is not usually critical factor for screen recordings where your purpose is to demostrate the UI element interaction.

5. Turn off font subpixel antialiasing

OSX may do font subpixel antialiasing by default (some more info). You might want to turn off this for screen recordings, as subpixel antialiasing is tied to (your own) monitor and may create funny looking color artifacts on other displays when your recording is being viewed.

To turn it off in Terminal:

defaults -currentHost write -globalDomain AppleFontSmoothing -int 2

To turn back the default setting:

defaults -currentHost delete -globalDomain AppleFontSmoothing

You need to restart the applications after changing this setting. The easiest way to test the setting on Finder is:

killall Finder # Will autorestart

6. Optimize the GIF

ImageOptim is an OSX application which will optimize all dragged and dropped images in place. For me, it provided 33% saving in the GIF size for GIFBrewery exported files.

7. Going beyond GIFs with custom Javascript animation code

Small GIFs are suitable for most of the UI screen recording purposes. However, if you want to create even more perfect results there is a way: a custom Javascript, PNG and JSON driven animation as demonstrated by the author Sublime Text 2, Jon Skinner.


\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+