Online Absolute Beginner Python Tutorials
Every Sunday 2pm (UK time/ BST)
by Cheuk Ting Ho
Get this slide deck:
slides.com/cheukting_ho/python-linting
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
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
To used with them: tox, pre-commit
Flake8 is a wrapper around these tools:
PyFlakes
pycodestyle
Ned Batchelder's McCabe script
To install
python -m pip install flask8
Invoking Flake8
flake8 --select E123
Configuring Flake8
Flake8 supports storing its configuration in the following places:
[flake8]
ignore = D203
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist
max-complexity = 10
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
Excluding specific lines
add a # noqa comment at the end of the line
from .endpoints import user, utils # noqa
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.
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 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
Usage
Skip ordering
isort mypythonfile.py mypythonfile2.py
import module # isort:skip
Like Travis but for testing in various Python enviroments
To install
To setup: tox.ini
To run
python -m pip install tox
tox
[tox]
envlist = py36
[testenv]
deps = pytest
commands = pytest
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
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
Sunday 2pm (UK time/ BST)
There are also Mid Meet Py every Wednesday 1pm
Testing month in June