Lab 7 Use Static Analysis tooling to manage project
Published: 2021-11-05 21:31:10

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Lab 7 focused on managing project complexity through the use of Static Analysis tooling. My project use Python, so I run with Python [Black](https://pypi.org/project/black/) and [Flake8](https://flake8.pycqa.org/en/latest/index.html). 

### How to apply Black to format Python code
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Black is the uncompromising Python code formatter and it is easy to install and to use. So I can save time and mental energy for more important matters.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Black can be **installed** by running: ` pip install black `
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Easy to **run** with: `black {source_file_or_directory}` or `python -m black {source_file_or_directory}`

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Black supports configurations and will look for configuration starting from the current working directory. By default Black looks for pyproject.toml starting from the common base directory of all files and directories passed on the command line. The pyproject.toml contains separate sections for different tools and Black is using the [tool.black] section.  ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gk21cwo8tq0whcze5ce7.png)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;After run Black, there are 2 files reformateed. One is to use double quotation marks instead of single quotation marks, and another is formatted multiple parameters in a row into multiple rows. After formatting, my code is really easier to read
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6pl9d187m4cdxwfea6xu.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwo744pl87ra1p9wmiq5.png)
### How to apply a linter - Flake8
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Flake8 is a tool officially released by Python to help detect whether Python code is standardized. Compared with pylin, which is popular at present, flake8 has flexible inspection rules, supports the integration of additional plug-ins and has strong expansibility.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Flake 8 can be **installed** by running: `python -m pip install flake8`
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Easy to **run** with: `flake8 path/to/code/to/check.py ` or ` flake8 path/to/code/ `
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;After run Flake8, there are `W605`, `E501`, `E541` issues. 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1. Issue `W605` is about invalid escape sequence. I solved this issue by adding "r" at the beginning of the regular expression.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2. Issue `W501` is about line too long(80>79 characters). I create a ".flake8" file, and add a command `max-line-length = 90 `, then the issue solved.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3. Issue `F541` is about f-string is missing placeholders. I solved this issue by add ad placeholder {args.config}.
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/niued42a529cxwaiawuw.png)
###Intergration
#### How to create one-step solution
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I create a new file named check.sh to provide one-step solution that use a shell script to execute the Black and flake8. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uk65glejfccv7fzpq764.png)
#### How to integrate the 2 extensions into .vscode
1. Create [User and Workspace](https://code.visualstudio.com/docs/getstarted/settings) settings, and add "black" to the .vscode/settings.json.![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7znxk17q8nxsa5pxu1pa.png) 
2. Repeat step 1 and integrate all extensions into the json file.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3o2q6kej3b0p5xjxoc7e.png)
 
###Pre-commit hook
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Git hook scripts](https://pre-commit.com/) are useful for identifying simple issues before submission to code review. 
* Before you can run hooks, you need to have the pre-commit package manager installed: `pip install pre-commit`
* Add a pre-commit configuration. I create a file named .pre-commit-config.yaml and generate a very basic configuration using pre-commit sample-config.
* Install the git hook scripts. I run `pre-commit install` to set up the git hook scripts. Now pre-commit will run automatically on git commit!
* Add Black and Flake8 into the pre-commit file.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odin8tuphzxxirm18bev.png)
###Reflection
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I used to read the project on GitHub. There are many files that I don't understand what their purpose is. After I complete the lab7, I understand most of them， for instance .vscode, .pre-commit-config.yaml, contributting.md, pyproject.toml and so on.