Lab 7 – Adding a Few Helpers

Welcome back. For this lab we learned about some pretty interesting and helpful additions we can make to repositories. We added a guide to contributing as well as some tools to make sure all the code that enters the repository is formatted the same and is free of lint. I even added a pre-commit hook that will auto check before I make a commit. Pretty neat stuff!

First thing to do was a little house cleaning. After checking out a new topic branch I split up the readme and made a contribution guide. The readme now holds all the usage information while the contribution file holds all of the setup and soon to be guidelines. Made a commit and then onto the next task.

Formatting

Since my repository is built with python I decided to use Black. After installing Black using pip all I had to was run the command along with a path or file to check.

Alright seems to have worked. I browsed through my code and noticed a couple of differences but I didn’t think it changed too much. So after updating the contribution file and testing the code to make sure it all worked I made another commit and noticed this message.

Apparently 60% of two of my main files had been modified. I went and checked the commit diff and found culprit. I had used double quotes around all my strings and I always put my if conditions (in brackets). Black changed all those double quotes to single quotes and took always all the brackets used around conditional checks.

Lint Roller

Next up is to add a Linter to the project. After doing a little research it seemed that flake8 was the more popular option over pyLint. Again, it was a simple install with pip and then I just had to run it from the command line. flake8 .

Ok, lines too long and another issue spam my screen. Invalid escape sequence is coming from my parse reg expression so I just added that to be excluded in the .flake8 config file. I also added a max line length of 100. After running the command again there were still a couple of lines that were over 100 characters so I had to split them up into separate lines;

                print(
                    (
                        f'{self.style._securedLink}[{line["status"]}] '
                        f'{line["url"]}{self.style._plainText}'
                    )
                )

I ran another round of black and then flake8 and both passed so I made yet another commit.

Automation

Almost finished. Now that I have a formatter and a linter, it’s time to automate the process to make writing code a little easier. With the following snippet in the /.vscode folder in a settings.json you can specify a few things so that anyone who clones this repo and uses Visual Studio code will benefit from these features.

{
  "python.formatting.provider": "black",
  "python.linting.flake8Enabled": true,
  "editor.formatOnSave": true
}

I also did the option part and added a pre-commit git hook that will run the checks before any commit. This allows for users who do not use Visual Studio Code to still have their code meet the standard put out. After again updating the contributions and testing the code I made another commit.

Last step was to clean up my history with a rebase, commit –ammed, a push and Bob’s your uncle. To see all the changes me and my handy helper made checkout out this commit compare.

Design a site like this with WordPress.com
Get started