Skip to content

Instantly share code, notes, and snippets.

View codeinthehole's full-sized 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 March 3, 2022 14:45
Correctly wrap JSON front-matter in markdown files
#!/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 / analyse_codebase.sh
Last active February 23, 2022 11:06
Analyse the changes in a repo over the last year
#!/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 05:45
Bash script to find unused custom template tags and filters
#!/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 / .vimrc
Last active April 13, 2021 13:47
~/.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 / test_timezones.py
Last active March 9, 2021 21:31
A Python unit test that demonstrates the problem with Django's `make_aware` function
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.
@codeinthehole
codeinthehole / account_tweets.py
Created November 7, 2017 22:50
Python script for printing out an account's tweets
"""
Script for printing out tweets from a given account
Sample usage:
python account_tweets.py qikipedia
"""
import twitter
import sys
@codeinthehole
codeinthehole / uwsgi_stats
Last active May 15, 2019 22:00
Zabbix plugin script for extracting uWSGI metrics from a log file
#!/usr/bin/env python
"""
Extract metrics from a uWSGI log file
This should be saved as /etc/zabbix/pluginx/uwsgi_stats and called from
/etc/zabbix/zabbix_agentd.conf.d/uwsgi.conf, which should have contents:
UserParameter=uwsgi.stats[*],/etc/zabbix/plugins/uwsgi_stats $1 $2
To gather these metrics in Zabbix, create a new item which calls this plugin and
@codeinthehole
codeinthehole / comparison.py
Last active March 20, 2019 06:13
Comparison of three ways of assigning excluded fields to a model when creating via a form
# Question - how best to handle model fields that are excluded from a form's
# fields.
# ------------------------------------------------------------
# Option 1: assign fields to instance before passing into form
# ------------------------------------------------------------
# views.py
instance = Vote(review=review, user=request.user)
form = VoteForm(data=request.POST, instance=instance)
@codeinthehole
codeinthehole / .bashrc
Last active February 12, 2019 09:07
Bash functions for private Docker registries
# Add these functions to your ~/.bashrc in order to be able to query private
# Docker registries from the commandline. You'll need the JQ library
# (http://stedolan.github.io/jq/) to be installed. Alternatively, you can just
# pipe to " python -mjson.tool" to get pretty JSON formatting
# TODO Enter the correct details here
DOCKER_REGISTRY_HOST=docker.yourcompany.com
DOCKER_REGISTRY_AUTH=someuser:somepassword
function _docker_fetch() {