Code Validation

Code Validation

In Python, code validation typically means checking whether your code:

  • Is correctly formatted
  • Follows naming conventions
  • Has no syntax issues
  • Meets PEP8 style rules
  • 4 spaces per indent
  • Snake_case for variables/functions
  • CamelCase for class names
  • Max line length of 79 or 88 characters
  • Two lines between functions, one line between methods, etc.

You can validate by copy and pasting your code into linters that check your code for any deviation from commonly accepted practices. I use the CodeInstitute one which is rough and ready but gets the job done.

Alternatively there are libraries that help keep your code organised.

Flake8

Flake8 checks for:

  • PEP8 violations
  • Syntax errors
  • Unused imports
  • Logical issues

You can also configure rules in a .flake8 config file. Install flake8 with:

pip install flake8

Then run it from your terminal with:

flake8 your_script.py

black

Black automatically rewrites your code to conform to a consistent style (PEP8 inspired). It doesn't check, it fixes your code with changes.

Install it with:

pip install black

Run it from your terminal with:

black your_script.py

isort

isort organises the imports at the top of your Python files. It automatically sorts and groups imports alphabetically and by type (stdlib, third-party, local). Install it with:

pip install isort

Run it from your terminal with:

isort your_script.py