In this lesson, you will learn,

  • what is Python code quality
  • what are the benefits
  • the types of code quality tools

What is Python Code Quality?

Whether you are writing a new NetBox library, building a custom module in Ansible or building out tasks in Nornir, it’s important to ensure that your Python code meets a level of quality. This leads to the next question: what is the level of quality that is required?

Some of the elements that indicate good code quality are when your code is:

  • readable
  • secure
  • maintainable
  • error-free
  • easy to debug
  • styled consistently.

What Are the Benefits?

What do we really get from ensuring our code is of a top-notch nature and gleaming? Well, it comes down to this: by ensuring we have code quality we can reduce the amount of time it takes to debug, iterate faster on new features and, quite frankly, save ourselves hours of head-scratching and troubleshooting over trivial issues.

For example, have you ever used a built-in name as a variable by mistake and couldn’t understand why your code wasn’t working? I know I have: especially writing code for network operations I’ve certainly fallen foul of assigning the built-in int as a variable name for an interface.

Code Quality Tooling?

Rather than trying to mandate and update our code manually to ensure it meets the required code quality, luckily for us the Python ecosystem provides a range of tools that allow us to automate this process. These tools come under the following categories: linters, formatters and type checkers. Here is a summary of each:

Tool Type Description Alters Code?
Formatters Formats your code to align to the necessary styling guidelines. Yes
Linters Detects potential errors and syntax issues that could cause an issue at runtime. No
Type Checkers Validates to ensure your code aligns to your defined type hints. No

Centralized Configuration /w pyproject.toml

One of the big disadvantages of Python over the years has been the need to have multiple files and configuration files for all the various tools and elements required for a Python project - for example, configuration files not only for the various formatting and linting tools, but also for the various files needed to build and publish your Python package on PyPI.

More recently, a new Python file - pyproject.toml (PEP518) - has been gaining adoption, which acts to solve this issue by centralizing the various settings within a single file.

Therefore, throughout this course we will be adding our configuration settings within pyproject.toml by default, and only creating additional configuration files if the tool does not support pyproject.toml based configuration files.

Want to Learn Network Automation?
Join our membership and get access to:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Tech demos and sessions

Join Now ➜