And, you will have Black command line interface (CLI) to play with. BLACK PYTHON FORMATTER INSTALLLet’s say you want to play and test Black in your local, before you make it as a standard practice with your team, you can pip install it in your virtual environment. BLACK PYTHON FORMATTER CODEOnce the developer is satisfied with the changes, he can push the code to the repository.īlack can also be used to format your local project files in addition to an entire repository. If we attached Black to it, every time a developer commits to our repository, the pre-commit hook automatically runs Black on it, and it lists the files in which the changes are made. The pre-commit hook lets you determine set of activities for Git to perform before the developer commits the code. Out of all hooks available, the pre-commit hook feature suited our needs. Using Git for version control in our projects, we wanted to use hooks, which is a useful concept in Git, to ease the formatting process. To make the process even easier and simpler, providing time-savings (being a repetitive process that needs to be run for every code commit), we decided to automate the entire process. They just run Black on the file or on any folder with multiple files, and it will take care of the formatting. Therefore, we decided to use Black for our projects.Ĭurrently, by using Black each time a developer has made changes to their code and wants to commit, he or she does not have to go through every line of code formatting it. Black was the clear winner with its better design in reformatting the entire file. Black, however, produced consistent results even when we formatted previously formatted files multiple times. It would obviously be undesirable to have the second formatted file be different from the first time it was formatted. When YAPF was applied twice on the same file (reformatting applied on an already formatted file), it would give inconsistent results. After eliminating AutoPEP8 from our options, it was time to decide between Black and YAPF. For instance, AutoPEP8 doesn’t format the code if it already follows PEP8 coding standards whereas, Black decides whether to normalize string quotes/prefixes and can be used to stop managing uniformity of coding styles manually. Although both follow PEP8 coding standards, Black follows better practices. While discussing the pros and cons of each, we started by comparing Black and AutoPEP8. We began by listing out the three formatters we considered- AutoPEP8, YAPF and Black. For this reason, we started thinking about the concept of using code formatters. Also, it improves productivity to format code each time it is committed to the repository. BLACK PYTHON FORMATTER HOW TOEven though we have a style guide, like PEP 8, to tell us how to address the formatting, it was hard for our developers to remember to format the code every time they made a commit. We wanted a solution that kept our Python code consistent in the entire repository. In our organization, my team and I faced the same situation. As there are different pieces of code worked on by different developers, it becomes very hard to keep the readability of the code consistent. As a project grows bigger and extends over time, the number of contributors to the project increases. Code formatting is a never-ending topic of discussion for developers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |