1.3. Code Style#

1.3.1. Remove unused lines with autoflake#

Do you always remove your unused imports and variables in Python?

If not, this can be problematic:

  • Your code will be more difficult to read

  • Reduced performance because the Python interpreter has to spend less time importing and evaluating unnecessary code

  • Potential issues can arise such as conflicts with other variables

If you want to clean and optimize your code, try autoflake

autoflake is a tool that automatically removes unused imports and variables for you.

You can also customize to fine-tune how autoflake processes your code.

!pip install autoflake
!autoflake --in-place <example.py>

1.3.2. Ensure Documentation with interrogate#

One aspect of code quality and maintainability is documenting your code.

In Python, there are docstrings to do this job.

How about to check if all of your methods and classes use docstrings?

Try interrogate.

interrogate is a Python tool for checking and enforcing docstrings.

You can even print out a coverage report.

Say goodbye to poorly-documented code!

!pip install interrogate
!interrogate . -v

1.3.3. Sort your imports automatically with isort#

When your Python app gets bigger, you will have more and more import statements.

This can look nasty when you don’t order them.

Don’t sort them manually.

Instead, use isort.

isort is a Python library that sorts your imports alphabetically and separates them into sections.

You only need to type one line into your CLI.

!pip install isort
!isort .

1.3.4. Format your code automatically with black#

If you want to avoid manual code formatting, use black for Python.

black is a library providing automatic formatting of Python code.

It makes it easier for you to focus on writing high-quality code.

Instead of wasting time formatting it by hand.

!pip install black
!black .

1.3.5. Lint your YAML files with yamllint#

Are you tired of manually checking YAML files for errors and formatting issues?

Use yamllint.

yamllint is a Python library to lint YAML files, ensuring that they are well-formed.

Since yaml is a popular choice for Config files, yamllint is a nice tool to catch errors before they cause problems.

!pip install yamllint
!yamllint .

1.3.6. Blazingly fast linting with ruff#

Are you still using flake8 or pylint?

Instead, use ruff, with 10x-100x faster performance.

ruff is a blazingly fast linter for Python, written in Rust.

It is orders of magnitudes faster than flake8 and pylint while integrating more functionality like import sorting or removing unused code.

# some_file.py
import requests
import json

def divide(a, b):
    try:
        result = a / b
    except ZeroDivisionError as e:
        result = 'infinity'
    return result
!pip install ruff
!ruff some_file.py

1.3.7. Format Python Code in Documentation Files#

Do you want to apply black on your documentation files?

Try blacken-docs!

blacken-docs apply the popular code formatting tool black in your documentation.

It supports Code snippets in:

  • Markdown

  • LaTeX

  • reStructuredText

Don’t write unformatted code in your documentation anymore!

!pip install blacken-docs
!blacken-docs TEST.md

1.3.8. Correct Misspellings in Your Codebase with codespell#

Typos in your codebase are nasty.

With codespell, you can correct them automatically.

codespell corrects your misspelled words in your source code and other files.

But it makes sure to not touch niche terms to reduce false positives.

Clean up your codebase.

!pip install codespell
!codespell # Running codespell in all files of current directory

1.3.9. Lint Your Dockerfile with hadolint#

Do you want to lint your Dockerfiles?

Try hadolint.

hadolint enforces Dockerfile best practices by parsing the Dockerfile into an AST and performing rules.

It’s available as a Docker container image.

!docker run -rm -i hadolint/hadolint < Dockerfile

1.3.10. Detect Typos in your codebase with typos#

To find typos in your code base, use typos.

typos is a fast code spell checker written in Rust for high performance.

It has a low false positive rate, so you can easily run it in your CI pipeline or as a pre-commit hook.

!brew install typos-cli
!typos