We use pep8 standards while coding. But sometimes carelessly we tend to miss this standard.
This was one of the common problems faced while coding. But now we are enforcing pep8 in our git repository. Without correcting these issues we cannot commit to the repository.
Let's learn how to do this
Step 1: Install pep8 checking package
pip install pycodestyle
Step 2: Create a hook folder in your repo and copy the pre-commit hook file
You can download the pre-commit from https://gist.github.com/afsal-parseltongue/a0dee4c25bd2b4d9616adbec56c0ebad
Step 3: Configure the pre-commit file if needed
This is a pre-commit hook I commonly used in Django projects. If you need to change the package change the text pycodestyle (line 35) to the package you are planning to use.
I am using a max-line length of 120, If you need to configure it change the value.
Also exclude folder is a folder in which pep8 checking is excluded, In Django, we don’t need to run this on the migration folder
Step 4: Configure the git hook path
git config core.hooksPath <path to hook>
For our case
git config core.hooksPath hooks
Now your repo has pep8 enforcing. You can commit only when all the pep8 rules are followed.Incase of any violations you will get error like this
PEP8 style violations have been detected. Please fix them
./master_data/views.py:199:5: E303 too many blank lines (4) ./master_data/views.py:203:59: E231 missing whitespace after ',' ./master_data/views.py:203:64: E251 unexpected spaces around keyword / parameter equals ./master_data/views.py:203:66: E251 unexpected spaces around keyword / parameter equals ./master_data/views.py:207:11: W292 no newline at end of file
Hope you have learned something from this. In case you want any help doing this please connect with us using the email firstname.lastname@example.org