DEV Community

Cover image for Lab 7 Use Static Analysis tooling to manage project
Jason
Jason

Posted on

Lab 7 Use Static Analysis tooling to manage project

       Lab 7 focused on managing project complexity through the use of Static Analysis tooling. My project use Python, so I run with Python Black and Flake8.

How to apply Black to format Python code

      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.
      Black can be installed by running: pip install black
      Easy to run with: black {source_file_or_directory} or python -m black {source_file_or_directory}

      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
      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
Image description

How to apply a linter - Flake8

      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.
      Flake 8 can be installed by running: python -m pip install flake8
      Easy to run with: flake8 path/to/code/to/check.py or flake8 path/to/code/
      After run Flake8, there are W605, E501, E541 issues.
       1. Issue W605 is about invalid escape sequence. I solved this issue by adding "r" at the beginning of the regular expression.
       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.
       3. Issue F541 is about f-string is missing placeholders. I solved this issue by add ad placeholder {args.config}.
Image description

Intergration

How to create one-step solution

      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

How to integrate the 2 extensions into .vscode

  1. Create User and Workspace settings, and add "black" to the .vscode/settings.json.Image description
  2. Repeat step 1 and integrate all extensions into the json file. Image description

Pre-commit hook

      Git hook scripts 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

Reflection

      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.

Top comments (0)