Home Artificial Intelligence A Data Scientist’s Guide To Improving Python Code Quality Background Linters Code Formatters

A Data Scientist’s Guide To Improving Python Code Quality Background Linters Code Formatters

0
A Data Scientist’s Guide To Improving Python Code Quality
Background
Linters
Code Formatters

Tools and packages to put in writing production worthy Python code

Towards Data Science
Photo by Christopher Gower on Unsplash

Nowadays, Data Scientists have gotten increasingly involved within the production side of deploying a machine learning model. This implies we want to find a way to put in writing production standard Python code like our fellow software engineers. In this text, I would like to go over among the key tools and packages that may aid in creating production-worthy code on your next model.

Overview

Linters are a tool that catches small bugs, formatting errors, and odd design patterns that may result in runtime problems and unexpected outputs.

In Python, now we have PEP8 which fortunately gives us a worldwide style guide to how our code should look. Quite a few linters exist in Python that adhere to PEP8, nonetheless my preference is flake8.

Flake8

Flake8 is definitely a mix of the Pyflakes, pycodestyle and McCabe linting packages. It checks for errors, code smells and enforces PEP8 standards.

To put in flake8 pip install flake8 and you should use it by flake8 . It truly is that easy!

For instance, let’s say now we have the function add_numbers in a file flake8_example.py:

def add_numbers(a,b):
result = a+ b
return result

print(add_numbers(5, 10))

To call flake8 on this file, we execute flake8 flake8_example.py and the output looks like this:

Flake8 has picked up several styling errors that we must always correct to be in keeping with PEP8.

See here for more details about flake8 and how you can customise it on your needs.

Overview

Linters often just let you know what’s incorrect along with your code but don’t actively fix it for you. Formatters do fix your code and help expedite your workflow, ensure your code adheres to style guides, and makes it more readable for other people.

isort

LEAVE A REPLY

Please enter your comment!
Please enter your name here