3 ways I improved my Python code last year

Chapin Bryce
Pythonic Forensics
Published in
3 min readJan 7, 2022

--

This post will be short and to the point. Here’s three ways that I improved the Python code I developed this year:

  • Listened to SonarLint
  • Leveraged pre-commit
  • Implemented type hinting
Photo by Joshua Aragon on Unsplash

I know some reading this are familiar with these three techniques — though at the start of 2021 I was not and I wanted to share with anyone who started this year in a similar position.

SonarLint

This free extension not only spotted issues in my code, but provided documentations and examples of each identified issue. Since it integrates into VS Code and PyCharm it was a very easy decision to include it into my workflow to increase my code quality. From this, I have learned about when to use an else statement after a for or while loop, using iteration controls within try/except blocks, and have spent a lot of time reducing the cyclomatic complexity of my methods.

Documentation and extension here: https://www.sonarlint.org/

Pre-commit

This tool enables standardization of code quality checks prior to saving and sharing my changes. It is incredibly helpful and has provided a method for performing static code analysis. With a simple configuration, I can use black to format the code, isort to sort the imports, mypy to check for typing issues (more on that in a moment), and many other checks.

Documentation about it: https://pre-commit.com/

A sample config:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 19.3b0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
name: isort (python)
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.910-1'
hooks:
- id: mypy
- repo: https://github.com/pycqa/flake8
rev: '4.0.1'
hooks:
- id: flake8

I look forward to learning more about it and possibly sharing my own pre-commit hooks.

Type hinting

This plays hand-in-hand with pre-commit. After implementing the mypy checks in pre-commit, I took the time to retrofit existing code to use type hints so that mypy can leverage that context and hunt for possible errors. The Python typing documentation is excellent and has helped identify how and when to use various typing classes.

One of my favorites is the TypedDict class. This enables definitions of structured dictionaries with required keys and values. With this, we can define the structure of JSON data that we share between classes. This, by far, has allowed me to more reliably construct interfaces and standardized structures to share data between programs and systems.

Quick example below:

from typing import List, TypedDictclass Weather(TypedDict):
high_temp: float
low_temp: float
summary: str
forecast: List[Weather] = [{
"high_temp": 40.3,
"low_temp": 34.7,
"summary": "Sunny"
}]

Whats next?

I hope this year to learn more about the following topics, though I am sure next year’s post will cover things I am currently unaware of.

  • Incorporating functional programming techniques and reducing unnecessary side-effect usage.
  • Diving into JavaScript and learning more about front end and asynchronous development.
  • Developing stronger skills with DevOps tools such as ansible, terraform, and packer.

What techniques did you pick up this year? What are you hoping to expand on this year?

--

--

DFIR professional, skier, aviation nerd. Co-author of Learning Python for Forensics & Python Forensics Cookbook. Message me for free links to any of my articles