A practical guide for better-looking python code

Photo by Laura Chouette on Unsplash

Introduction

I. Do not push to the master branch

git clone https://github.com/olegpolivin/Fizz-Buzz-CI-CD.git
Empty Fizz-Buzz-Ci-CD repo with a README

Rules for branches

Settings, Branches and Rules
Branch protection rule: no status checks

II. Continuous Integration / Continuous Delivery

Basic example

Basic CI/CD config

Set up a rule for your branch

Branch protection rule with a check
Total 3 (delta 1), reused 0 (delta 0)remote: Resolving deltas: 100% (1/1), completed with 1 local object.remote: error: GH006: Protected branch update failed for refs/heads/main.remote: error: Required status check “build (3.7)” is expected.To https://github.com/olegpolivin/FizzBuzz-CI-CD.git! [remote rejected] main -> main (protected branch hook declined)error: failed to push some refs to ‘https://github.com/olegpolivin/FizzBuzz-CI-CD.git'
git checkout -b dev
git push origin dev
Creating a pull request: dev -> main
Status checks have passed

III. FizzBuzz

FizzBuzz Definition

import scipy
import pandas
import numpy
import matplotlib
from matplotlib import pyplot as plt
####### Here I start the solution to the fizz buzz problem #######
def fizz_buzz(n: int) -> str:
if n % 15 == 0: return 'fizzbuzz'
elif n % 5 == 0: return 'buzz'
elif n % 3 == 0: return 'fizz'
else: return str(n)

Extended GitHub Actions file

CI/CD config with additional checks
Failing status checks

A better version of fizzbuzz.py

"""Function to solve the fizzbuzz problem."""def fizz_buzz(num: int) -> str:
"""This is my great and neat function to solve the famous
Fizz Buzz problem.
:param num: That's the number which we want the answer for
:return: fizz, buzz, fizzbuzz or the number itself
"""
if num % 15 == 0:
return "fizzbuzz"
if num % 5 == 0:
return "buzz"
if num % 3 == 0:
return "fizz"
return str(num)

Catch the problem before committing

IV. Pre-commit hook

Setting up the pre-commit hook

Pre-commit hook

Testing the pre-commit hook

Pre-commit hook checks failed
Pre-commit passed!

V. Testing the code

Setting up the test

├── fizzbuzz.py
├── .github
│ └── workflows
│ └── ci.yml
├── .gitignore
├── .pre-commit-config.yaml
├── README.md
└── test_fizzbuzz.py
"""Perform tests of the fizz_buzz function."""
import pytest
from fizzbuzz import fizz_buzzinputs = [3, 5, 15, 4, 10, 115, 7]outputs = ["fizz", "buzz", "fizzbuzz", "4", "buzz", "buzz", "7"]
@pytest.mark.parametrize("inp,out", zip(inputs, outputs))
def test_fizzbuzz(inp, out):
"""Takes inputs, gets the output of the fizz_buzz function.
Asserts whether equality holds.
"""
assert fizz_buzz(inp) == out
- name: tests
run: pytest

Passing the test

Test passed!

Failing the test

def fizz_buzz(num: int) -> str:
"""This is my great and neat function to solve the famous
Fizz Buzz problem.
:param num: That's the number which we want the answer for
:return: fizz, buzz, fizzbuzz or the number itself
"""
if num % 3 == 0:
return "fizz"
if num % 5 == 0:
return "buzz"
if num % 15== 0:
return "fizzbuzz"
return str(num)
Test failed

Conclusions

Acknowledgements

Data Scientist

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store