The tabnanny Module

(New in 2.0) The tabnanny module checks Python source files for ambigous indentation. If a file mixes tabs and spaces in a way that throws off indentation, no matter what tab size you're using, the nanny complains.

In the badtabs.py file used in the following examples, the first line after the if statement uses four spaces followed by a tab. The second uses spaces only.

$ tabnanny.py -v samples/badtabs.py
';samples/badtabs.py': *** Line 3: trouble in tab city! ***
offending line: print "world"

indent not equal e.g. at tab sizes 1, 2, 3, 5, 6, 7, 9

Since the Python interpreter reads a tab as eight spaces, the script will run correctly. It will also display correctly in any editor that assumes that a tab is either eight or four spaces. That's not enough to fool the tab nanny, of course.

Example 11-6 shows how you can also use tabnanny from your own programs.

Example 11-6. Using the tabnanny Module

File: tabnanny-example-1.py

import tabnanny

FILE = "samples/badtabs.py"

file = open(FILE)
for line in file.readlines():
 print repr(line)

# let tabnanny look at it
tabnanny.check(FILE)

'if 1:12'
' 11print "hello"12'
' print "world"12'
samples/badtabs.py 3 ' print "world"'12'

To capture the output, you can redirect sys.stdout to a StringIO object.

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols

Internationalization

Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules



Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net