Developing Python Packages
James Fulton
Climate informatics researcher
pytest
is used to find bugsflake8
is used to find styling mistakesStatic code checker - reads code but doesn't run
flake8 features.py
features.py:2:1: F401 'math' imported but unused
..
<filename>:<line number>:<charcter number>:<error code> <desciption>
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
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
quadratic.py
4. ...
5. quadratic_1 = 6 * x**2 + 2 * x + 4;
6. quadratic_2 = 12 * x**2 + 2 * x + 8
7. ...
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
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
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
Ignoring style violations without using comments
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
Create a setup.cfg
to store settings
Package file tree
.
|-- example_package
| |-- __init__.py
| `-- example_package.py
|-- tests
| |-- __init__.py
| `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
`-- setup.py
Create a setup.cfg
to store settings
[flake8]
ignore = E302
exclude = setup.py
per-file-ignores = example_package/example_package.py: E222
Package file tree
.
|-- example_package
| |-- __init__.py
| `-- example_package.py
|-- tests
| |-- __init__.py
| `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
|-- setup.py
`-- setup.cfg
$ flake8
Package file tree
.
|-- example_package
| |-- __init__.py
| `-- example_package.py
|-- tests
| |-- __init__.py
| `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
|-- setup.py
`-- setup.cfg
Least filtering
# noqa : <code>
# noqa
setup.py
→ per-file-ignores
setup.cfg
→ exclude
, ignore
Most filtering
Developing Python Packages