Python Zero to Heros

Online Absolute Beginner Python Tutorials 

Every Sunday 2pm (UK time/ BST)

Get this slide deck:

slides.com/cheukting_ho/python-linting

Recap

Python objects - int, float, str, list, dict, bool

Control flows - if-else, for loop, while loop

Functions, modeuls, classes and decorators

strings operations and regex with re

pytest with fixtures and mock

property-based testing

 

Any Questions?

What is Linting

lint, or a linter, is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs.[1] The term originates from a Unix utility that examined C language source code.[2]

- wikipedia

 

Why Linting

  1. to avoid errors (fix typos before running tests)
  2. to keep a consistance code format (increase readability)
  3. to automatically achieve no.1 and 2 (save time)
  4. Save time in code reviews

 

Liters/ auto-formatter

in Python

  • Flake 8
  • Autoflake
  • Black
  • Isort

To used with them: tox, pre-commit

Flake8

Flake8 is a wrapper around these tools:

PyFlakes
pycodestyle
Ned Batchelder's McCabe script

 

To install

python -m pip install flask8

Flake8

Invoking Flake8

flake8 --select E123

Configuring Flake8

Flake8 supports storing its configuration in the following places:

  • Your top-level user directory
  • In your project in one of setup.cfg, tox.ini, or .flake8.
[flake8]
ignore = D203
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist
max-complexity = 10

Autoflake

autoflake removes unused imports and unused variables from Python code. It makes use of pyflakes to do this.

 

By default, autoflake only removes unused imports for modules that are part of the standard library. (Other modules may have side effects that make them unsafe to remove automatically.) Removal of unused variables is also disabled by default.

 

To install

python -m pip install autoflake

Autoflake

Excluding specific lines

 

add a # noqa comment at the end of the line

from .endpoints import user, utils  # noqa

Black

The Uncompromising Code Formatter

Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.

Blackened code looks the same regardless of the project you're reading. Formatting becomes transparent after a while and you can focus on the content instead.

Black makes code review faster by producing the smallest diffs possible.

Black

To install

 

 

Usage

 

 

Formatting

Black reformats entire files in place. It is not configurable. It doesn't take previous formatting into account. It doesn't reformat blocks that start with # fmt: off and end with # fmt: on. # fmt: on/off have to be on the same level of indentation.

python -m pip install black
black {source_file_or_directory}

Isort

isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections. It provides a command line utility, Python library and plugins for various editors to quickly sort all your imports.

 

To install

python -m pip install isort

Isort

Isort

Usage

 

 

 

Skip ordering

isort mypythonfile.py mypythonfile2.py
import module  # isort:skip

Tox

Like Travis but for testing in various Python enviroments

Tox

To install

 

 

To setup: tox.ini

 

 

 

 

To run

python -m pip install tox
tox
[tox]
envlist = py36

[testenv]
deps = pytest
commands = pytest

Pre-commit

Run commit hooks on every commit to automatically point out issues in code such as missing semicolons, trailing whitespace, and debug statements.

 

To install

 

 

python -m pip install pre-commit

Pre-commit

To setup: .pre-commit-config.yaml

repos:
-   repo: https://github.com/ambv/black
    rev: stable
    hooks:
    - id: black
      language_version: python3.6
-   repo: https://gitlab.com/pycqa/flake8
    rev: 3.7.9
    hooks:
    - id: flake8

Install the git hook scripts

pre-commit install

Next week:
TDD

Sunday 2pm (UK time/ BST)

There are also Mid Meet Py every Wednesday 1pm

Testing month in June