yapf (Code formatter) & pyflakes(lint Tool) with Python3


Code formatter and lint tool is so useful to keep quality of your Python code.

  • yapf : A formatter for Python files
  • pyflakes : A simple program which checks Python source files for errors

In this article, I would like to share how to use the tools.

🎳 Installation

pip install yapf pyflakes

πŸŽƒ Configuration

Add scripts/lint.sh:

#!/bin/bash
set -euC
cd "$(dirname "$0")/../"

for t in yapf pyflakes; do
if ! which "$t" > /dev/null 2>&1; then
echo "Please install $t ." >&2
exit 1
fi
done

status=0

# Check a change after yapf formatting
yapf --diff --parallel --recursive project_folder | tee yapf.diff
[ ! -s yapf.diff ] || status=1

# Check what you can not fix automatically with yapf
pyflakes project_folder || status=1

# heck that __init__.py is created in each directory.
for d in $(find project_folder -type d | grep -v '__pycache__\|\.\|project_folder/res'); do
if [ ! -f "$d/__init__.py" ]; then
echo "$d/__init__.py γŒγ‚γ‚ŠγΎγ›γ‚“" >&2
status=1
fi
done

exit "$status"

πŸš• Execution

Execute the following command in CI tool:

scripts/lint.sh

πŸ–₯ Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!