GC3: Grid Computing Competence Center

Blog index

GC3 graudates into S3IT
Posted early Tuesday morning, July 1st, 2014
How to create a module that also load a virtualenvironment
Posted late Friday morning, March 7th, 2014
Openstack workshop at GC3
Posted at noon on Saturday, February 22nd, 2014
Moving LVM volumes used by a Cinder storage
Posted late Friday evening, February 21st, 2014
How to configure swift glusterfs
Posted Monday night, February 10th, 2014
Yet another timeout problem when starting many instances at once
Posted late Friday night, February 8th, 2014
Fixing LDAP Authentication over TLS/SSL
Posted Monday night, January 6th, 2014
Linker command-line options for Intel MKL
Posted Saturday night, January 4th, 2014
A virtue of lazyness
Posted Saturday afternoon, December 21st, 2013
(Almost) readable CFEngine logs
Posted Thursday afternoon, December 19th, 2013
CFEngine error: ExpandAndMapIteratorsFromScalar called with invalid strlen
Posted Wednesday afternoon, December 11th, 2013
'Martian source' log messages and the default IP route
Posted Monday afternoon, November 25th, 2013
GC3 takes over maintenance of the Schroedinger cluster
Posted at noon on Monday, November 4th, 2013
Grid Engine: how to find the set of nodes that ran a job (after it's finished)
Posted early Wednesday morning, October 30th, 2013
Python2 vs Python3
Posted at teatime on Friday, September 13th, 2013
GC3Pie 2.1.1 released
Posted Friday evening, September 6th, 2013
Happy SysAdmin day!
Posted mid-morning Friday, July 26th, 2013
Object-oriented Python training
Posted Thursday afternoon, July 25th, 2013
Elasticluster 1.0.0 released
Posted Thursday night, July 18th, 2013
Short Autotools tutorial
Posted at lunch time on Friday, July 5th, 2013
Patch Emacs' PostScript printing
Posted Tuesday evening, June 11th, 2013
Slides of the Object-oriented Python course now available!
Posted Tuesday evening, June 11th, 2013
Automated deployment of CFEngine keys
Posted at midnight, May 31st, 2013
blog/Resize_an_image
Posted Tuesday evening, May 14th, 2013
Join us at the Compute Cloud Experience Workshop!
Posted early Monday morning, April 29th, 2013
GC3 Beamer theme released
Posted at lunch time on Friday, April 5th, 2013
VM-MAD at the International Supercompting Conference 2013
Posted at lunch time on Tuesday, March 26th, 2013
The GC3 is on GitHub
Posted at lunch time on Monday, March 18th, 2013
How to enable search in IkiWiki
Posted Friday afternoon, March 15th, 2013
GC3Pie Training
Posted Thursday night, March 7th, 2013
Object-oriented Python training
Posted Thursday afternoon, March 7th, 2013
Advance Reservations in GridEngine
Posted late Thursday morning, March 7th, 2013
GridEngine accounting queries with PostgreSQL
Posted Wednesday night, March 6th, 2013
Floating IPs not available on Hobbes
Posted at teatime on Tuesday, February 26th, 2013
Notes on SWIFT
Posted mid-morning Tuesday, February 12th, 2013
An online Python code quality analyzer
Posted at lunch time on Saturday, February 9th, 2013
Seminar on cloud infrastructure
Posted Sunday night, February 3rd, 2013
GC3 announce its cloud infrastructure Hobbes
Posted Wednesday afternoon, January 30th, 2013
GC3Pie 2.0.2 released
Posted Monday afternoon, January 28th, 2013
Continuous Integration with Jenkins
Posted at noon on Saturday, January 26th, 2013
On the importance of testing in a clean environment
Posted mid-morning Monday, January 21st, 2013
Weirdness with ImageMagick's `convert`
Posted at teatime on Tuesday, January 15th, 2013
boto vs libcloud
Posted Tuesday afternoon, January 15th, 2013
Resolve timeout problem when starting many instances at once
Posted at lunch time on Monday, January 7th, 2013
Proceedings of the EGI Community Forum 2012 published
Posted at teatime on Monday, December 17th, 2012
SGE Workaround Installation
Posted at lunch time on Tuesday, December 4th, 2012
How to pass an argument of list type to a CFEngine3 bundle
Posted mid-morning Thursday, November 22nd, 2012
GC3 at the 'Clouds for Future Internet' workshop
Posted mid-morning Wednesday, November 21st, 2012
GC3 attends European Commission Cloud Expert Group
Posted mid-morning Monday, October 29th, 2012
SwiNG - SDCD2012 event
Posted at lunch time on Monday, October 22nd, 2012
Large Scale Computing Infrastructures class starts tomorrow!
Posted late Tuesday afternoon, September 25th, 2012
From bare metal to cloud at GC3
Posted mid-morning Monday, September 24th, 2012
GC3 at the EGI Technical Forum 2012
Posted Thursday night, September 20th, 2012
Training on GC3Pie and Python
Posted late Friday evening, September 7th, 2012
GC3Pie used for research in Computational Quantum Chemistry
Posted late Thursday afternoon, September 6th, 2012
``What's so great about MPI or Boost.MPI?''
Posted mid-morning Thursday, September 6th, 2012
blog/How to generate UML diagram with `pyreverse`
Posted late Thursday morning, August 23rd, 2012
Git's `rebase` command
Posted mid-morning Friday, June 15th, 2012
AppPot 0.27 released!
Posted at noon on Thursday, June 14th, 2012
Urban computing - connecting to your server using `mosh`
Posted mid-morning Wednesday, June 6th, 2012
Whitespace cleanup with Emacs
Posted Tuesday afternoon, June 5th, 2012
Translate pages on this site
Posted Thursday evening, May 31st, 2012
Scientific paper citing GC3Pie
Posted Wednesday evening, May 30th, 2012
GC3 attends Nordugrid 2012 conference
Posted at lunch time on Wednesday, May 30th, 2012
How the front page image was made
Posted late Wednesday evening, May 16th, 2012
GC3 blog launched!
Posted late Tuesday evening, May 15th, 2012
New GC3 Wiki now online!
Posted Tuesday evening, May 15th, 2012
AppPot paper on arXiv
Posted Tuesday evening, May 15th, 2012
GC3 at the EGI Technical Forum 2011
Posted Tuesday evening, May 15th, 2012

More on topic...

At GC3, we do a lot of Python programming. As everyone knows, Python is whitespace-sensitive, as indentation is used to denote statement blocks, which contributes to the great readability of Python. However, whitespace is invisible by its very definition, so it's easy to mix TAB and space characters in a source file, which can mess up the file if you are using a non-default TAB length setting. In addition, git warns about trailing whitespace and other "whitespace errors".

So, how to enforce whitespace discipline?

It turns out that you do not need to, if you're using the Emacs editor.

Give visual distinction to troublesome whitespace

Minor mode whitespace-mode1 will make many whitespace features immediately visible:

screenshot of whitespace-mode on Emacs 23

  • whitespace characters at the end of a line is shown with red background color;
  • empty lines at the beginning or end of a buffer are shown in bright yellow;
  • normal whitespace is shown as a light grey dot (so you can easily count the number of spaces in a run);
  • a light grey $ is shown at the end of each line;
  • lines exceeding a certain (configurable) lenght are shown in a bright pink color on a black background.

I find it convenient to bind whitespace-mode to the F8 key:

    (global-set-key (kbd "<f8>")  #'whitespace-mode)

Clean up whitespace issues

The whitespace-cleanup command can cure most whitespace-related issues:

(whitespace-cleanup)

Cleanup some blank problems in all buffer or at region. [...]

The problems cleaned up are:

  1. empty lines at beginning of buffer.
  2. empty lines at end of buffer. If whitespace-style includes the value empty, remove all empty lines at beginning and/or end of buffer.

  3. 8 or more SPACEs at beginning of line. [...]

  4. SPACEs before TAB. [...]

  5. SPACEs or TABs at end of line. [...] If whitespace-style includes the value trailing, remove all SPACEs or TABs at end of line.

  6. 8 or more SPACEs after TAB. [...]

See whitespace-style',indent-tabs-mode' and `tab-width' for documentation.

Features dealing with SPACE and TAB mixup actually do the right thing based on the setting of indent-tabs-mode: if it's nil, TABs are always converted to SPACEs, otherwise 8 or more SPACEs are converted to TABs.

Automatically cleanup whitespace issues

I find it convenient to run the whitespace-cleanup function automatically when saving a file. This is easy: Emacs runs the hook write-contents-functions just before saving a file, so one only needs to add whitespace-cleanup to that. However, according to the documentation write-contents-functions "automatically becomes buffer-local when set in any fashion", so it cannot be set globally.

The trick is to use another hook to set write-contents-functions; the mode setup hooks work fine here, and allow us to just turn on whitespace cleanup feature only for certain modes (e.g., on for Python, but off for Makefiles).

Another problem, however, is that whitespace-cleanup sets the "buffer modified" flag even unconditionally (even if it really has to do no cleanup), and the Emacs save procedure aborts if the buffer is modified by any of the write-contents-functions. So we must define a wrapper that resets the "buffer modified" flag after running whitespace-cleanup to what it was before running the whitespace cleanup.

Wrapping up, all this is accomplished with the following Emacs Lisp code in your ~/.emacs file:

  1. Define a variant of whitespace-cleanup that resets the "buffer modified" flag to its prior state:

    (defun whitespace-cleanup* ()
      (let* ((modified-before-p (buffer-modified-p)))
        (whitespace-cleanup)
        (if (not modified-before-p)
            (not-modified))))
    
  2. Define a function to append whitespace-cleanup to write-contents-functions:

    (defun whitespace-cleanup-on-save ()
      (add-hook 'write-contents-hooks #'whitespace-cleanup*))
    
  3. Then add it to the setup hooks of the major modes of your choice:

    (add-hook 'c-mode-common-hook   #'whitespace-cleanup-on-save)
    (add-hook 'emacs-lisp-mode-hook #'whitespace-cleanup-on-save)
    (add-hook 'python-mode-hook     #'whitespace-cleanup-on-save)
    (add-hook 'sh-mode-hook         #'whitespace-cleanup-on-save)
    (add-hook 'text-mode-hook       #'whitespace-cleanup-on-save)
    

Further reading

--RM


  1. Bundled with GNU Emacs since version 23; available for download from EmacsWiki for Emacs version 21 onwards. ↩

top