Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting white space in python files?

Tags:

python

I have some tab formatted code in python and I have some space formatted code in python.

Integrating the code in a pain... my editor wants to work in tabs or spaces, but not both.

Is there a command line function in linux or, well, anything which will convert python code formatting one way or the other?

like image 894
jedierikb Avatar asked Dec 03 '10 23:12

jedierikb


3 Answers

reindent.py is a nifty utility script which converts any python .py file into one using 4-space indents and no tabs.

This is useful for "normalizing" code from disparate sources, assuming you're willing to settle on the 4-space standard. (Or, if you want tabs, you could run reindent.py followed by the unix unexpand command.)

PS. Your installation of Python may have reindent.py already installed in a Tools or Examples folder. On Ubuntu it is provided by the python-examples package, and is located at /usr/share/doc/python2.6/examples/Tools/scripts/reindent.py.

like image 200
unutbu Avatar answered Oct 08 '22 05:10

unutbu


'man expand' for some info

it's in coreutils on Debian

like image 33
jcomeau_ictx Avatar answered Oct 08 '22 07:10

jcomeau_ictx


You can use expand and unexpand Unix commands for this.

Generally if I code in vim for instance I have it automatically convert tabs to spaces.

my ~/.vimrc looks something like this:

set expandtab
set tabstop=4
like image 26
SirMo Avatar answered Oct 08 '22 06:10

SirMo