1. Keeping your package stylish
You are well on your way to building stable, working packages, but it's time to think more about style.
2. Introducing flake8
Good code is read many more times than it is written, and so to help the reader, and yourself, we use the standard Python style guide, PEP8. This covers how variables and functions should be named, and generally how your code should be laid out.
Following these conventions makes your code easier for others to understand and use.
When developing code, it can be hard to spot bugs when you are writing it; that's why we use pytest.
Similarly, it can be hard to spot every place where you have deviated in style; for this we use flake8.
3. Running flake8
Flake8 is a static code checker, which means it reads through your code without actually running it. Here we run flake8 from the terminal, passing it one of our modules to evaluate.
It prints a series of style improvement suggestions.
The output includes the file name, then the line number, then the character number of the violation, and a problem code and description.
4. Using the output for quality code
Here is the features-dot-py module. It's a bit messy.
We didn't use the math import, and we haven't spaced out our functions.
In the mean function, we used 3 spaces to indent instead of 4, and in the std function, we created a variable that isn't used.
This module is bug-free and would pass our tests, but it could still be better.
5. Using the output for quality code
If we make the suggested changes and run flake8 again, it prints nothing, meaning our code is stylish.
6. Breaking the rules on purpose
Remember that PEP8 is only a guide, so you might bend these rules occasionally.
For example, PEP8 says that after an equals sign, you should have only one space, but you might find that these two expressions
7. Breaking the rules on purpose
become clearer if you add an additional space. If you made this choice, flake8 would notify you about it every time, which could get annoying.
8. Breaking the rules on purpose
You can stop this by placing a noqa comment on the line. This stands for no quality-assurance. Now flake8 won't evaluate this line of code.
9. Breaking the rules on purpose
If we include a violation code after noqa, then flake8 will evaluate the line but ignore this particular type of violation.
10. flake8 settings
It is possible to ignore specific errors using flake8 without adding comments. For the previous case, we could use flake8's ignore flag and pass it a list of violation codes to ignore.
Or if we only want to search for a specific set of violations, we can select those. Here we search for unused imports and variables.
11. Choosing package settings using setup.cfg
We can save these settings for flake8 by creating a setup-dot-cfg file in the package directory.
12. Choosing package settings using setup.cfg
In this config file, we create a section header for flake8 and then define the settings we want. For example, we'll ignore line spacing in all modules, and we will exclude setup-dot-py from being evaluated. We can also tell flake8 to ignore specific violations in particular files. Here we tell it to ignore extra spaces, but only in the main module of this example package.
When we use flake8 inside our package, these settings will be used without us having to specify them.
13. The whole package
You can run flake8 on the whole package at once by navigating to the top of the package, and running flake8 from the terminal.
14. Use the least filtering possible
Using these config options, or noqa comments, produce different amounts of violation filtering. You should always use the minimum amount of filtering possible.
15. Let's practice!
Now let's practice!