Giữ package của bạn gọn gàng

Phát triển gói Python

James Fulton

Climate informatics researcher

Giới thiệu flake8

  • Chuẩn style Python mô tả trong PEP8
  • Style guide quy định cách trình bày code
  • Dùng pytest để tìm lỗi
  • Dùng flake8 để phát hiện lỗi style
Phát triển gói Python

Chạy flake8

Trình kiểm tra mã tĩnh - đọc mã, không chạy

flake8 features.py
features.py:2:1: F401 'math' imported but unused
..
<filename>:<line number>:<charcter number>:<error code> <desciption>
Phát triển gói Python

Dùng đầu ra để cải thiện chất lượng mã

 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
Phát triển gói Python

Dùng đầu ra để cải thiện chất lượng mã

 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


Phát triển gói Python

Cố ý vi phạm quy tắc

quadratic.py

4. ...
5. quadratic_1 = 6 * x**2 + 2 * x + 4;
6. quadratic_2 = 12 * x**2 + 2 * x + 8
7. ...
Phát triển gói Python

Cố ý vi phạm quy tắc

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
Phát triển gói Python

Cố ý vi phạm quy tắc

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

Phát triển gói Python

Cố ý vi phạm quy tắc

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
Phát triển gói Python

Thiết lập flake8

Bỏ qua vi phạm style mà không dùng comment

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
Phát triển gói Python

Chọn thiết lập package với setup.cfg

Tạo setup.cfg để lưu thiết lập

Cây thư mục package

.
|-- example_package
|   |-- __init__.py
|   `-- example_package.py
|-- tests
|   |-- __init__.py
|   `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
`-- setup.py
Phát triển gói Python

Chọn thiết lập package với setup.cfg

Tạo setup.cfg để lưu thiết lập

[flake8]


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

Cây thư mục package

.
|-- example_package
|   |-- __init__.py
|   `-- example_package.py
|-- tests
|   |-- __init__.py
|   `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
|-- setup.py
`-- setup.cfg
Phát triển gói Python

Toàn bộ package

$ flake8

Cây thư mục package

.
|-- example_package
|   |-- __init__.py
|   `-- example_package.py
|-- tests
|   |-- __init__.py
|   `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
|-- setup.py
`-- setup.cfg
Phát triển gói Python

Chỉ lọc ở mức tối thiểu cần thiết

Ít lọc nhất

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

Lọc nhiều nhất

Phát triển gói Python

Ayo berlatih!

Phát triển gói Python

Preparing Video For Download...