Dein Paket stilvoll halten

Python-Pakete entwickeln

James Fulton

Climate informatics researcher

Einführung in flake8

  • Der Standard-Python-Stil steht in PEP8
  • Ein Styleguide legt fest, wie Code formatiert sein soll
  • pytest findet Bugs
  • flake8 findet Stilfehler
Python-Pakete entwickeln

flake8 ausführen

Statischer Code-Checker – liest Code, führt ihn aber nicht aus

flake8 features.py
features.py:2:1: F401 'math' imported but unused
..
<filename>:<line number>:<charcter number>:<error code> <desciption>
Python-Pakete entwickeln

Ausgaben für sauberen Code nutzen

 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-Pakete entwickeln

Ausgaben für sauberen Code nutzen

 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-Pakete entwickeln

Regeln absichtlich brechen

quadratic.py

4. ...
5. quadratic_1 = 6 * x**2 + 2 * x + 4;
6. quadratic_2 = 12 * x**2 + 2 * x + 8
7. ...
Python-Pakete entwickeln

Regeln absichtlich brechen

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-Pakete entwickeln

Regeln absichtlich brechen

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-Pakete entwickeln

Regeln absichtlich brechen

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-Pakete entwickeln

flake8-Einstellungen

Stilverstöße ohne Kommentare ignorieren

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-Pakete entwickeln

Paket-Einstellungen mit setup.cfg wählen

Erstelle eine setup.cfg für Einstellungen

Paket-Verzeichnisbaum

.
|-- example_package
|   |-- __init__.py
|   `-- example_package.py
|-- tests
|   |-- __init__.py
|   `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
`-- setup.py
Python-Pakete entwickeln

Paket-Einstellungen mit setup.cfg wählen

Erstelle eine setup.cfg für Einstellungen

[flake8]


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

Paket-Verzeichnisbaum

.
|-- example_package
|   |-- __init__.py
|   `-- example_package.py
|-- tests
|   |-- __init__.py
|   `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
|-- setup.py
`-- setup.cfg
Python-Pakete entwickeln

Das gesamte Paket

$ flake8

Paket-Verzeichnisbaum

.
|-- example_package
|   |-- __init__.py
|   `-- example_package.py
|-- tests
|   |-- __init__.py
|   `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
|-- setup.py
`-- setup.cfg
Python-Pakete entwickeln

So wenig filtern wie möglich

Am wenigsten filtern

  1. # noqa : <code>
  2. # noqa
  3. setup.py → per-file-ignores
  4. setup.cfg → exclude, ignore

Am meisten filtern

Python-Pakete entwickeln

Lass uns üben!

Python-Pakete entwickeln

Preparing Video For Download...