5 Tools and Techniques for Better Python Project Setups | by Aram Haroyan | Apr, 2022

Photograph by Octavian Dan on Unsplash

Typically once we are including new options to our utility it could possibly have an effect on the present code or break its performance.

To keep away from this case we do write unit assessments and run them each time to make sure that the brand new performance that we’ve added isn’t dangerous to the appliance.

So about assessments, there are libraries and frameworks for writing higher assessments and operating them simply reminiscent of Pytest and tox.

Pytest might help us not simply run assessments but in addition configure easy methods to run them, which information to run, and many others…Pytest has a configuration file pytest.ini the place you’ll be able to describe its configuration for instance which model must be Pytest or that are take a look at information like the next.

# pytet.ini
[pytest]
minversion = 6.0
addopts = -ra -q — cov=src — cov-report=html
python_files = test_*.py

Additionally, it has integrations certainly one of them is pytest-cov which integrates coverage with Pytest, protection might help us by offering details about which a part of the app is roofed by a take a look at and which isn’t, it additionally gives a protection report in varied codecs XML, JSON, HTML and reveals what number of p.c of code is roofed by assessments.

Screenshot from https://coverage.readthedocs.io/en/6.3.2/

Tox is a generic virtualenv administration and take a look at command-line software.
Checking that your package deal installs accurately with totally different Python variations and interpreters
Working your assessments in every of the environments, configuring your take a look at software of alternative
Appearing as a frontend to Steady Integration servers, vastly lowering boilerplate and merging CI and shell-based testing.
Tox additionally has its configuration file.

[tox]
isolated_build = True

envlist =
py38

[testenv]
usedevelop = true
deps =
-r src/requirements_dev.txt

These sorts of instruments might help you to make sure that any adjustments carried out is not going to have an effect on options carried out earlier than.

Typically there may be unused variables within the code or the written code fashion can’t correspond to PEP8 guidelines. Right here static code analyzers come to assist us.
So listed below are a few static code analyzers with their configuration instance.
Pylint, Flake8, MyPy.
These instruments might help us to keep away from issues like lifeless code, none relevant code for instance breaking PEP-8 guidelines, unused variables in code, and many others…
Pylint config: create .pylintrc file

[MESSAGES CONTROL]
disable=
missing-docstring,
too-few-public-methods
[REPORTS]
output-format=colorized
files-output=no
reviews=no
analysis=10.0 - ((float(5 * error + warning + refactor + conference) / assertion) * 10)

Flake8 config: create .flake8 file

[flake8]
ignore = E203, E266, E501, W503, F403, F401, E402
max-line-length = 120
max-complexity = 18
choose = B,C,E,F,W,T4,B9
exclude =
.git,
assessments

These sorts of instruments might help you to outline a code fashion for the entire utility and builders will maintain it!

Think about a scenario the place somebody pushed a code with out operating all of the assessments and static code analyzers, which can have an effect on current options.
So to keep away from this type of scenario pre-commit got here to assist us.
Pre-commit is a framework that creates git hooks to verify your code is written the is akin to your outlined code fashion.
It scans your supply code and runs all of the checkers you’ll outline in your pre-commit config file: .pre-commit-config.yaml

repos:
- repo: 'https://gitlab.com/pycqa/flake8'
rev: 3.8.2
hooks:
- id: flake8
title: Fashion Information Enforcement (flake8)
args:
- '--max-line-length=120'
- repo: 'https://github.com/pre-commit/mirrors-mypy'
rev: v0.720
hooks:
- id: mypy
title: Non-obligatory Static Typing for Python (mypy)

These sorts of instruments might help you to keep away from dangerous code in your git repositories.

Each utility may be hacked and there’s a threat of an information breach or supply code breach, information encryption, utility breakdown, or another dangerous issues.
There are a bunch of instruments for vulnerability scan, however we are going to take a look at Sonarqube. Sonarqube is an open-source highly effective software for code high quality and safety scanning, and one of many main instruments on this business.
Extra within the official documentation.
You possibly can arrange an area Sonarqube server utilizing a Docker picture and outline sonar-project.properties

# should be distinctive in a given SonarQube occasionsonar.projectKey=python_app_blueprint# --- elective properties ---# defaults to mission key#sonar.projectName=My mission# defaults to 'not supplied'#sonar.projectVersion=1.0# Path is relative to the sonar-project.properties file. Defaults to .#sonar.sources=.
# Encoding of the supply code. Default is default system encoding#sonar.sourceEncoding=UTF-8

You in all probability have examine SRP — single duty precept and variable naming, so they aren’t solely about programming in python, java, c#, or another language, they’re general-purpose rules and work nearly in every single place even in folder creation.

So it’s higher to separate your utility into totally different folders and provides them corresponding names, and retailer your information individually.
For instance:

├── LICENSE
├── Makefile
├── mission.toml
├── pytest.ini
├── README.md
├── setup.cfg
├── setup.py
├── sonar-project.properties
├── .ci
│ ├── some_pipeline.yaml
│ ├── scripts
├── src
│ ├── app
│ ├── requirements_dev.txt
│ └── necessities.txt
├── assessments
│ ├── integration
│ │ └── __inti__.py
│ └── unit
│ └── __init__.py
└── tox.ini

More Posts