讓你的套件更有型

開發 Python 套件

James Fulton

Climate informatics researcher

認識 flake8

  • 標準 Python 風格見 PEP8
  • 風格指南規範程式碼排版
  • pytest 找出程式錯誤
  • flake8 找出風格問題
開發 Python 套件

執行 flake8

靜態程式碼檢查器——讀程式不執行

flake8 features.py
features.py:2:1: F401 'math' imported but unused
..
<filename>:<line number>:<charcter number>:<error code> <desciption>
開發 Python 套件

運用輸出來提升程式品質

 1. import numpy as np
 2. import math
 3.
 4. def mean(x):
 5.    """Calculate the mean"""
 6.    return np.mean(x)
 7. def std(x):
 8.     """Calculate the standard deviation"""
 9.     mean_x = mean(x)
10.     std = mean((x-mean(x))**2)
11.     return std
12. 
flake8 features.py
2:1: F401 'math' imported but unused

4:1: E302 expected 2 blank lines, found 1
7:1: E302 expected 2 blank lines, found 0
5:4: E111 indentation is not a multiple of four 6:4: E111 indentation is not a multiple of four
9:5: F841 local variable 'mean_x' is assigned to but never used
開發 Python 套件

運用輸出來提升程式品質

 1. import numpy as np
 2.
 3.
 4. def mean(x):
 5.     """Calculate the mean"""
 6.     return np.mean(x)
 7. 
 8. 
 9. def std(x):
10.     """Calculate the standard deviation"""
11.     mean_x = mean(x)
12.     std = mean((x - mean_x)**2)
13.     return std
14. 
flake8 features.py


開發 Python 套件

故意違反規則

quadratic.py

4. ...
5. quadratic_1 = 6 * x**2 + 2 * x + 4;
6. quadratic_2 = 12 * x**2 + 2 * x + 8
7. ...
開發 Python 套件

故意違反規則

quadratic.py

4. ...
5. quadratic_1 =  6 * x**2 + 2 * x + 4;
6. quadratic_2 = 12 * x**2 + 2 * x + 8
7. ...
flake8 quadratic.py
quadratic.py:5:14: E222 multiple spaces after operator
quadratic.py:5:35: E703 statement ends with a semicolon
開發 Python 套件

故意違反規則

quadratic.py

4. ...
5. quadratic_1 =  6 * x**2 + 2 * x + 4; # noqa
6. quadratic_2 = 12 * x**2 + 2 * x + 8
7. ...
flake8 quadratic.py

開發 Python 套件

故意違反規則

quadratic.py

4. ...
5. quadratic_1 =  6 * x**2 + 2 * x + 4; # noqa: E222
6. quadratic_2 = 12 * x**2 + 2 * x + 8
7. ...
flake8 quadratic.py
quadratic.py:5:35: E703 statement ends with a semicolon
開發 Python 套件

flake8 設定

不寫註解也能忽略風格違規

flake8 --ignore E222 quadratic.py
quadratic.py:5:35: E703 statement ends with a semicolon
flake8 --select F401,F841 features.py
2:1: F401 'math' imported but unused
9:5: F841 local variable 'mean_x' is assigned 
     to but never used
開發 Python 套件

用 setup.cfg 設定套件選項

建立 setup.cfg 儲存設定

套件檔案樹

.
|-- example_package
|   |-- __init__.py
|   `-- example_package.py
|-- tests
|   |-- __init__.py
|   `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
`-- setup.py
開發 Python 套件

用 setup.cfg 設定套件選項

建立 setup.cfg 儲存設定

[flake8]


ignore = E302
exclude = setup.py
per-file-ignores = example_package/example_package.py: E222

套件檔案樹

.
|-- example_package
|   |-- __init__.py
|   `-- example_package.py
|-- tests
|   |-- __init__.py
|   `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
|-- setup.py
`-- setup.cfg
開發 Python 套件

完整套件結構

$ flake8

套件檔案樹

.
|-- example_package
|   |-- __init__.py
|   `-- example_package.py
|-- tests
|   |-- __init__.py
|   `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
|-- setup.py
`-- setup.cfg
開發 Python 套件

盡量少用過濾規則

最少過濾

  1. # noqa : <code>
  2. # noqa
  3. setup.pyper-file-ignores
  4. setup.cfgexcludeignore

最多過濾

開發 Python 套件

一起來練習吧!

開發 Python 套件

Preparing Video For Download...