Skip to content

Instantly share code, notes, and snippets.

Avatar
🌏
The main difference between dinosaurs and us is we're building our own meteor.

David Winterbottom codeinthehole

🌏
The main difference between dinosaurs and us is we're building our own meteor.
View GitHub Profile
@codeinthehole
codeinthehole / fix-json-frontmatter.py
Created Mar 3, 2022
Correctly wrap JSON front-matter in markdown files
View fix-json-frontmatter.py
#!/usr/bin/env python
#
# Script to wrap JSON front-matter in markdown files with `---` delimiters.
#
# This allows Prettier to be used on the markdown file (and it won't try and format the JSON front
# matter).
#
# I needed this to convert old Hugo markdown files that had JSON front-matter.
import os
import sys
@codeinthehole
codeinthehole / github-team-report.sh
Created Feb 2, 2022
Open Github pull request list page filtered to closed PRs from the last week from a given team
View github-team-report.sh
#!/bin/bash
#
# Script that opens the Github pull request search page filtered to show closed pull
# requests from the last week, from members of a specified set of users.
#
# This can be useful for team leads when writing progress reports.
# Config
# ------
@codeinthehole
codeinthehole / python-testing.md
Last active May 20, 2022
Python testing reference
View python-testing.md

Python testing reference

This document is a reference for common testing patterns in a Django/Python project using Pytest.

Contents:

@codeinthehole
codeinthehole / test_timezones.py
Last active Mar 9, 2021
A Python unit test that demonstrates the problem with Django's `make_aware` function
View test_timezones.py
import datetime
import pytz
from django.utils import timezone
from dateutil import tz
# This test passes.
def test_pytz_vs_dateutil_timezones():
timezone_name = "Europe/London"
# Start with a naive dt.
View .pythonstartup.py
# Python start-up file
# --------------------
# Ensure a PYTHONSTARTUP environment variable points to the location of this file.
# See https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP
# Always have pp available
from pprint import pprint as pp
# Pre-emptively import datetime as I use it a lot.
import datetime
@codeinthehole
codeinthehole / analyse_codebase.sh
Last active Feb 23, 2022
Analyse the changes in a repo over the last year
View analyse_codebase.sh
#!/usr/bin/env bash
#
# Fetch diff stats for the current repo from the last year
# Get a commit SHA from a year ago
OLD_SHA=$(git log --since="365 days ago" --until="364 days ago" -1 --pretty=format:"%H")
NEW_SHA=$(git rev-parse HEAD)
# Number of lines then and now
OLD_LINES=$(git diff --stat `git hash-object -t tree /dev/null`..$OLD_SHA | awk '/files changed/ {print $4}')
@codeinthehole
codeinthehole / find_dead_template_tags.sh
Last active May 20, 2021
Bash script to find unused custom template tags and filters
View find_dead_template_tags.sh
#!/usr/bin/env bash
#
# Helper script to look for unused template tags and filters.
#
# Run this in the root of your project and it will print out template tags and filters that
# aren't used anywhere.
#
# Requires ripgrep (rg).
set -e
@codeinthehole
codeinthehole / test_commits.py
Created Oct 11, 2019
Sample test module for validating the commits messages on a PR branch
View test_commits.py
import re
import subprocess
import pytest
@pytest.mark.parametrize(
"subject, error_msg",
[
("WIP: working on something", "is a WIP commit"),
View .vimrc
" ============= " VIMRC file for David Winterbottom (@codeinthehole) " ===========
" Inspiration {{{
" -----------
" Videos:
" - http://www.youtube.com/watch?v=aHm36-na4-4
"
" Articles:
"
" - http://alexpounds.com/blog/2014/06/06/the-vimrc-antiques-roadshow
@codeinthehole
codeinthehole / client.py
Last active Oct 29, 2021
Sample Python client for working with the Octopus Energy REST API
View client.py
# Requires the requests library (install with 'pip install requests')
import requests
class APIClient(object):
BASE_URL = "https://api.octopus.energy/v1"
class DataUnavailable(Exception):
"""
Catch-all exception indicating we can't get data back from the API