Monthly Archives: November 2013

Indentation in Python; Sometimes it’s creepy

Yesterday, I was working on my next patch i.e. Patch Number 3. While doing that, I came across a very unusual thing with my system that it was showing me indentation error. I made it correctly indented in terminal but it still shows indentation error. When i opened it with gedit it was again not properly indented. So after searching a lot, I came to know about a cool  feature that is autopep8. It automatically indents your code.
here is, how you can install it via pip.
$ pip install --upgrade autopep8

It will auto-format the code not only indentation but also spacing styles. So it makes the python script to conform  PEP8 style guide.

$ autopep8 
# to auto-format your code
After solving this somehow,  i made a new pull request and create a new commit. But then i realized that there were few things that were i need to change. Also, the problem of indentation error was not resolved.
So first thing i had to do was to delete my git commit. I learned few commands that i would like to share with you.
To know which commit is the head pointing to:
$ git reset --hard HEAD~1

The HEAD~1 means the commit before head.
If you want to move you head to some earlier commit,then

$ git log

This will list all the commits made by you along with their commit id. You can find the commit-id of the commit you want to move your head to

$ git reset --hard <sha1-commit-id>

If you want to delete the commit that you have already pushed, then:

$ git push origin HEAD --force
After doing this, I again made commit after making changes in the code. But my problem was still there. There were indentation errors and also trailing white-spaces errors. Then pypingou asked me to make changes in the ~/.vimrc file and it really worked for me.
They are:
set list
set listchars=tab:→\ ,trail:·
This will allow you to quickly check your trailing spaces or when it uses tab/spaces. So, finally my problem was solved 🙂