簡介
接著上篇(python項目最佳實踐(上))
Continue to put the pre-submission process、編碼檢測、自動格式化、Static type checking, etc. are performed once.
面向對象:python初、中級、There are certain other language foundations ready to transferpython的人員
本文目標:Learn how to standardize、Normalization starts with onepython項目
Run checks before committing code
版本控制工具:git
How to install it is not explained here,請參考git官網.
在初始化一個新的gitThere will be one after the warehouse .gitignore 文件,This file is used to ignore temporary and binary file commits to the repository,
對於python項目,githubThere is a template above,鏈接:.gitignore
問題:Why run a check before committing?
Before you commit the code to the remote repository after you have developed a feature,It's easy to miss something,For example, the formatting style of the code is wrong,通不過 CI 自動化,At this time, automatic code inspection can save you unnecessary trouble.
pre-commit 就可以完美解決該問題,它會在每一個gitRun the auto-detection tool before committing,雖然是用python編寫的,But can be used for various programming language projects.
安裝 pre-commit
如何使用 pre-commit
可以看到,pre-commit Some hooks are already created for us
# 讓我們 手動運行檢查:
運行後,It has found the problem,Prompt that there is no newline at the end of the file,不符合編碼規范.
其實在運行pre-commit後,It has automatically found the problem and solved it,There is no problem when running it.
讓git Automatic detection using a script
讓我們測試下 git 提交,Just change something,進行提交測試
更新 pre-commit
使用 Flake8 linter 進行代碼檢測
linter 是什麼?
在idePrompt for code errors and syntax error detection.在 linter 的幫助下,IDE It can let you know the code errors all the time.
python 的 linters 有很多,常用的有 flake8 和 pylint.
pylint is a very strict one linter,It doesn't allow any small mistakes in your code,Your code should be written strictly according to the required specification,Otherwise it will be marked.Although this way can make your code more secure and standardized,But it will also cause your development time to increase.
It is used in most open source projects flake8,It's relatively loose,但是足夠使用了.
linter 是什麼?
在idePrompt for code errors and syntax error detection.You can prompt your code for errors before your code runs.在 linter 的幫助下,IDE It can let you know the code errors all the time.
python 的 linters 有很多,常用的有 flake8 和 pylint,
pylint is a very strict one linter,It doesn't allow any small mistakes in your code,Your code should be written strictly according to the required specification,Otherwise it will be marked.Although this way can make your code more secure and standardized,But this will increase your development time.
It is used in most open source projects flake8,It's relatively loose,但是足夠使用了.
Python風格指南 - PEP8.
每一個python Developers should and must knowPEP8,以及 Zen of Python.
安裝 Flake8
如果你是用的是vscode,那麼你只需要在 ctrl+shift+p 輸入 linter 選擇 python: linter, 然後選擇 flake8
VScode Your virtual environment is automatically selected,並安裝 flake8
也可以使用poetry 將 flake8 添加到 開發環境
poetry add flake --dev
手動執行 flake8 檢測
將 flake8 添加到 git 鉤子中
將 flake8 添加到 git Automatic code runs before.
代開 上面 pre-commit 添加的 .pre-commit-config.yaml, 添加如下內容, This will automatically run the next time the code is automatically committed.
使用 Black 格式化代碼
So why do you need to format your code?
在多人開發的項目中,We cannot guarantee that every developer will have the same coding style,Even if everyone followspep8the required code,也無法做到萬無一失,After all, people make mistakes,Sometimes you are taking over a feature developed by another colleague,You may encounter code that is completely different from your own coding style,You may look a little labored and awkward,This is where the advantages of the program come into play,借助 Black Automatically format code according to the format specified by the team,Make coding styles consistent across team members.
安裝 Black
在 pyproject.tom 配置文件中配置 Black
執行Black
We are performing pre-commit detection
同樣 Black 加入到 git 鉤子中
使用 Mypy 進行靜態類型檢測
Why use static type checking?
靜態代碼分析工具,This ensures that the code is type-safe;It can make the interpreter perform better code detection,The probability of code errors is greatly reduced,And reduce the difficulty for team developers to get started with code, Makes the code more readable for humans.關於 Mypy這裡不做過多講解,We will now only discuss the general flow of a project.具體的MypyPlease refer to the official documentation for explanation,
官方文檔地址 Mypy
安裝mypy
使用 mypy
將 mypy 添加到 git 鉤子
將以下內容添加到 .pre-commit-config.yaml
使用 isort 對 import 進行自動排序
PEP8Specifies that imports should be sorted in the following order:標准庫、第三方庫、本地庫.此外,我們希望 import Beautiful and human friendly.
剛好 isort This can be done well, 下面是 isort 的官方示例:
錯誤示例(不優雅的 包導入)
Correct and reasonable example
把 isort 添加到 git hooks
最後:Quick configuration example
Quickly start all the processes of a new project.
1.使用 poetry安裝依賴
2.配置 pyproject.tom 文件
3.setup.cfg 文件
4. .pre-commit-config.yaml 文件
If you read the two tutorials before and after,Congratulations, you should already know how to gracefully start a new onepython項目,If you don't know the exact process,那麼,實踐是最好的老師.It’s okay if you can’t remember the exact process,We finally provide quick steps for setting up the entire project,It only takes a few minutes to quickly turn onpython新項目.It's up to you next,Let's get started~
[參考鏈接]
Structure your project
語義化版本
Toml語言
Poetry官網
Pyenv
pyenv-win
廖雪峰-單元測試
pytest-cov
Best_Practice_For_Python最佳實踐指南
Python中PEP8、flake8、autopep8、black
我為什麼不喜歡 black
Mypy
PEP8
聲明:This article was originally published by Nanjing Linkosla,轉載請注明.
聯系我們:電話 400-828-1855.