Home > Backend Development > Python Tutorial > One simple step to make your Python code cleaner

One simple step to make your Python code cleaner

王林
Release: 2023-04-12 22:19:17
forward
1234 people have browsed it

Easier said than done. We all know that code readability is very important, but we always write as we please, without considering type hints, import sorting, and PEP8 specifications. Today I’m going to share a little trick that can make your Python code cleaner in just one simple step.

This is pre-commit:

One simple step to make your Python code cleaner

allows you to automatically check whether your code meets the specifications you want before submitting it.

Before using, install it with pip:

pip install pre-commit
Copy after login

Then create two files in the root directory of the project: .pre-commit-config.yaml and pyproject.toml.

.pre-commit-config.yaml The content of the file is as follows:

exclude: _pb2.py$
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
args: [--skip-string-normalization]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-docstring-first
- id: check-json
- id: check-merge-conflict
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace
- id: requirements-txt-fixer
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.9.0
hooks:
- id: python-check-mock-methods
- id: python-use-type-annotations
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v0.910"
hooks:
- id: mypy
args:
[
--ignore-missing-imports,
--warn-no-return,
--warn-redundant-casts,
--disallow-incomplete-defs,
]
additional_dependencies: [types-all]
- repo: https://github.com/PyCQA/isort
rev: 5.9.3
hooks:
- id: isort
args: [--profile, black, --filter-files]
Copy after login

It contains black, mypy, check-docstring-first, isort and other tools. The id is the corresponding tool. You can This configuration file is basically enough.

In the .pre-commit-config.yaml file we can specify which hooks will be used, and in pyproject.toml we can specify parameters for these individual hooks.

The content of the pyproject.toml file is as follows:

[tool.black]
line-length = 88
target-version = ["py38"]

[tool.isort]
profile = "black"
multi_line_output = 3
Copy after login

The relevant configurations of black and isort are configured here.

Then execute the pre-commit install command in the root directory of the project to install the pre-commit plug-in.

Then every time you update the code and submit the code, these hooks will be triggered and the following operations will be automatically performed:

Sort import

PEP8 format code

Check your yaml and json files for correctness

Type checking (if you used type hints)

One simple step to make your Python code cleaner

Finally

You can copy these two files to your own project root directory, and then execute pre-commit install. This way, every time you submit the code, it will be clean code. Isn't it very convenient?

The above is the detailed content of One simple step to make your Python code cleaner. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template