Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Maheshwari Thakur

Indentation is an important feature of Python syntax. Code blocks in function, class or loops are required to follow same indent level for statements in it. The tabnanny module in Python's standard library is able to detect any violation in this stipulation.

This module is primarily intended to be used in command line mode with –m switch. However, it can also be imported in an interpreter session.

Command line usage:

python –m tabnanny –q example.py

For verbose output use –v switch

python –m tabnanny –v example.py

Following functions are defined in tabnanny module for checking indentation programmatically.

check():

This function checks for ambiguously indented lines in a given file. You can also pass a directory as parameter. All file in it will be recursively checked.

Example:

import tabnanny
tabnanny.check('example.py')

tabnanny.verbose: This flag indicates whether to print verbose messages. This is incremented by the -v option if called as a script.

tabnanny.filename_only: This flag indicates whether to print only the filenames of files containing whitespace related problems. This is set to true by the -q option if called as a script.

process_tokens():

This function is used by check() to process tokens generated by the tokenize module. This function raises NannyNag exception if an ambiguous indent is detected. It is captured and handled in check().

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.