Keeping your package stylish

Developing Python Packages

James Fulton

Climate informatics researcher

Introducing flake8

  • Standard Python style is described in PEP8
  • A style guide dictates how code should be laid out
  • pytest is used to find bugs
  • flake8 is used to find styling mistakes
Developing Python Packages

Running flake8

Static 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>
Developing Python Packages

Using the output for quality code

 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
Developing Python Packages

Using the output for quality code

 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


Developing Python Packages

Breaking the rules on purpose

quadratic.py

4. ...
5. quadratic_1 = 6 * x**2 + 2 * x + 4;
6. quadratic_2 = 12 * x**2 + 2 * x + 8
7. ...
Developing Python Packages

Breaking the rules on purpose

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
Developing Python Packages

Breaking the rules on purpose

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

Developing Python Packages

Breaking the rules on purpose

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
Developing Python Packages

flake8 settings

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
Developing Python Packages

Choosing package settings using setup.cfg

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
Developing Python Packages

Choosing package settings using setup.cfg

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
Developing Python Packages

The whole package

$ 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
Developing Python Packages

Use the least filtering possible

Least filtering

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

Most filtering

Developing Python Packages

Let's practice!

Developing Python Packages

Preparing Video For Download...