As I promised yesterday, here’s a thread on how to automate the styling(PEP8) checks and logical error checks on your Python code if you’re using Git as a version control system.

🧵👇
1/10 Running Linters, Formatters and any other Analysis tools on your code can really help in making your code consistent and much more readable. See my previous thread on improving the Quality of your Python code: https://twitter.com/RaviTezu/status/1381309781361627138?s=20
2/10 Using Git pre-commit hook, we can automate these tools to run against the files which are modified/staged for a commit. If the modified file doesn’t contain the code according to the PEP8 standards or if it is not formatted right, we are going to fail the commit.
3/10 While you can write and configure your own git pre-commit hooks, here’s an easy way to do it:
Pre-Commit: A framework written in Python for managing and maintaining multi-language pre-commit hooks. See https://pre-commit.com  for more details.
4/10 You can install it using pip: "pip install pre-commit” and then create a file with the name “.pre-commit-config.yaml” inside your git repository to configure it.
5/10 Here’s an example configuration(👀attached screenshot) to run “PyLint” and “Black” on the modified files every time you run “git commit -m <blah>”. You can see the list of all supported hooks here: https://pre-commit.com/hooks.html 
6/10 Once you have decided on what all hooks to run and added them to your “.pre-commit-config.yaml” file. All you need to do is run “pre-commit install” as a last step to install the hooks. Note: You can skip this step using an advanced feature of pre-commit.
7/10 Now, when you try to commit, these pre-commits hooks will kick in and run against the modified files and fail the commit if something needs to be changed.
[👀Screenshot] I didn’t have the code in the right format and so Black failed the commit, whereas the PyLint run passed.
8/10 Pre-commit currently supports hooks written in many languages: See https://pre-commit.com/#supported-languages and it also has some very good advanced features like skipping some hooks, running hooks at specific stages and more. See: https://pre-commit.com/#advanced  for more details.
9/10 As mentioned above, pre-commit currently support hooks written in other languages as well.
So you can use pre-commit to run any tool or any customised scripts for other programming languages as well and thereby increasing the code quality.
10/10 That’s all for today. Please like/retweet this thread if you think it can help others. Please follow me for more threads on Programming, Software Engineering and more. Thank you 😀
#Python #GIT #Programming #CodeQuality
You can follow @RaviTezu.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: