- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Detection of ambiguous indentation in python
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.
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.
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.
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().
- Related Articles
- Lines and Indentation in Python
- Why is indentation important in Python?
- Statement, Indentation and Comment in Python
- Fraud Detection in Python
- Ambiguous Genitalia
- Line detection in python with OpenCV?
- Code indentation in Lua Programming
- What is Ambiguous Grammar?
- Text Indentation using CSS
- Text Indentation Working with CSS
- Smile detection using haar cascade in OpenCV using Python
- Differentiate between Ambiguous and Unambiguous Grammar
- Detection of a specific color(blue here) using OpenCV with Python?
- Error detection at its best: Implementing Checksum using Python
- Psychological Approaches to Detection of Deceit