Logo

dev-resources.site

for different kinds of informations.

Checking your python code format on Azure Pipelines

Published at
3/23/2022
Categories
formatting
tutorial
azurepipelines
python
Author
10xlearner
Author
10 person written this
10xlearner
open
Checking your python code format on Azure Pipelines

Hello ! I’m Xavier Jouvenot and in this small post, I am going to explain how to automatically check if your python code is well formatted with Azure Pipelines.

Self promotion: Here are a few social networks where you can follow me and check my work as a programmer and a writer πŸ˜‰

personal website, Twitter, Dev.to, CodeNewbie, Medium, GitHub

Problematic

Today, we are going to focus on how we can automatically check if the python code you are versioning is well formatted, and more specifically, how you can use Azure Pipelines to do so. To automate this process will make your team more efficient and the code more consistent since everyone will have to be on board and format their code each time they want to contribute.

If you are not convinced that you should format you code, I really encourage you to go look at the previous article I did on Formatting and Automation, where I go in detail on why you should definitively format your code, and do it in a automatic way, to make your code more readable and make your team more productive since they won’t have to spend time arguing about Spaces VS Tabs.

Solution

The short answer, for the people who don’t want to read through the entire article (I know you do that! I do it too πŸ˜†) is to insert the following steps in your Azure Pipelines process:

- script: |
    pip3 install black
displayName: Installs the latest version of black
- script: |
    black --check .
displayName: Checks if the python scripts are formatted properly with black

Enter fullscreen mode Exit fullscreen mode

If you have my previous article about "Formatting Python – Why and How !", you may already know about about the tool named black. For those who don’t know, this is a code formatter, described by its authors as follow: > Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.

In the steps above, I start by installing this tool with the python package manager pip. Then, I run black at the root of the folder, meaning that it is going to find all the Python files in repository and, since we are using the flag --check, verify if they are well formatted. If one Python file is not formatted correctly, then, the black command will return an error, and the corresponding step in Azure Pipelines will fail.

If you want, you can specify directly the list of the Python files, instead of letting the tool scan through all the repository recursively. πŸ˜‰

Finally, it is interesting to notice that those steps can be uses on any environment available in Azure Pipelines. I actually made a GitHub repository in which I setup Azure Pipelines to run the steps described before on every environment available. You can take a look here as I will update it when new environment will be available on Azure Pipelines πŸ˜‰


Thank you all for reading this article, And until my next article, have a splendid day πŸ˜‰

Interesting links

formatting Article's
30 articles in total
Favicon
How to Configure VSCode for Auto Formatting and Linting in Python
Favicon
Clean Code: Open Source Linting & Formatting
Favicon
A One-Liner `sed` Command to Format SSH Config File
Favicon
Developing a Custom Gradle Plugin for Formatting and Static Analysis
Favicon
Why Do I Love Code Formatters?
Favicon
My opinion about opinionated Prettier: πŸ‘Ž
Favicon
How to convert XML files to CSV format using Boomi in Docker
Favicon
Format Time Ago Function - Time Differences in TypeScript
Favicon
How to use Prettier as a Github Action
Favicon
My universal code beautification tool
Favicon
What are formatting tags in HTML?
Favicon
Compact `match` in Rust
Favicon
Axis Headaches? Examples for Formatting Tick Labels (Matplotlib)
Favicon
Wednesday Links - Edition 2023-05-17
Favicon
Formatting External Drives On Linux Using Gparted.
Favicon
Make Your Code Shine with Prettier Extension for VS Code
Favicon
Accounting Number Format in Excel – How to Apply it to Selected Cells
Favicon
How to Clear Formatting in Excel – Remove Format From a Cell
Favicon
Best Practice #1 : One Function can be accessed many times with Different Values
Favicon
Hugo.io - Multiline cells in a table
Favicon
AppVeyor and python formatting
Favicon
Checking your python code format on Azure Pipelines
Favicon
Formatting Python – Why and How !
Favicon
Golang automatic code formatting : Code like a Pro
Favicon
Clean Code in C# Part 4 Formatting
Favicon
Formatting numbers in JavaScript
Favicon
How to format relative dates using native JavaScript
Favicon
Formatting dates in JavaScript using the user's locale
Favicon
Set Cell Styles and Formatting in Excel with Java
Favicon
Code formatting for C# projects: dotnet format + GitHub Actions

Featured ones: