# Section 8: Continuous Integration: clean code, formatters, linters and VS Code integrations
## case
- Jenkins: CI/CD工具,自動化建構、測試、部署流程
- Bitbucket : 提供程式碼託管,存放、版本控制程式碼(支持Git)
- git commit trigger
- unittest -> coding style
## coding style
### 1.格式化與代碼風格檢查:
- pycodestyle:是否符合PEP 8標準。
```python=
script.py:2:2: E225 missing whitespace around operator
script.py:3:5: E303 too many blank lines (2)
```
- black:是否符合PEP 8標準+一些規則,並將程式碼格式化為一致風格。
- 尾隨逗號(Trailing Comma)
```python=
fruits = [
"apple",
"banana",
"cherry",
]
```
### 2.文檔風格檢查:
- pydocstyle:檢查文檔風格是否符合 PEP 257 規範。
- pydoclint:也檢查拼寫和其他文檔一致性。
```python=
def add_numbers(a, b):
"""
This function adds two number.
Args:
a (int): The first number.
b (int): The second number.
Returns:
int: The sum of a and b.
"""
return a + b
```
### 3.靜態型別檢查:
- mypy:型別檢查+依賴型別註解
```python=
def add(x: int, y: int) -> int:
return x + y
result: int = add(2, "3") # ❌ 錯誤:y 應該是 int,但傳入 str
```
- pylink: 未使用變數、import 檢查
```python=
def add_numbers(x, y):
return x + y
print(add_numbers(1, "2"))
```
- unused import
```python=
import os
import math
def calculate_square_root(x):
return math.sqrt(x)
```
output:
```
W0611: Unused import os (unused-import)
```
### 4.導入排序檢查:
- isort:用於自動排序(標準、第三方、自定義)
```python=
import os
import sys
import numpy as np
import pandas as pd
from mymodule import myfunc
```
### 5.腳本檢查:
- shellcheck:檢查Shell腳本中的語法和風格錯誤
- https://www.shellcheck.net/
https://code.visualstudio.com/docs/python/formatting
https://docs.pydantic.dev/latest/#why-use-pydantic