Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HOWTO: Use reindent.py for dummies

I am a beginner at Python and have made the mistake of mixing spaces and tabs for indentations. I see people use reindent.py, but I have no idea how to use it. please explain in the simplest way possible without trying to use too fancy words and dumb it down as best as possible as I am a beginner.

Thanks.

like image 850
person239 Avatar asked Nov 20 '14 04:11

person239


People also ask

What is Reindent PY?

If no paths are given on the command line, reindent operates as a filter, reading a single source file from standard input and writing the transformed source to standard output. In this case, the -d , -r and -v flags are ignored. You can pass one or more file and/or directory paths.

How to dedent Python?

It is Ctrl + [ in IDLE. You can change it to your favorite Shift + Tab in Options -> Configure IDLE - Keys.

How to indent in Python shortcut?

Just select the text you want indented and hit Tab . To un-indent, select the text and hit Shift + Tab .

What is 4 spaced indentation?

For example if you start off using four spaces for an indent, then you should always use four spaces. In the example below, four spaces have been used for the first indent, but only two for the second, and you can see that as a result the code doesn't “line up”. phrase = input("Talk to me > ")


2 Answers

To install reindent with the python package manager, you can first run pip install reindent on your system. Then simply call from the terminal

reindent -n file.py

The script will modify file.py.

If you do not want to modify the original file, you can simply run the command without the -n flag, and you get:

reindent file.py

which will return a file called file.py.bak, which is the corrected version.

like image 119
Jason Brooks Avatar answered Oct 04 '22 23:10

Jason Brooks


You run reindent.py like this:

reindent.py <filename>.py

This will create <filename>.py.bak in the current directory.

If you prefer, you can run it like this: reindent.py -n <filename>.py if you don't want a backup file.

like image 34
vikramls Avatar answered Oct 05 '22 00:10

vikramls