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 / userdata.sh
Created February 2, 2016 21:21
Terraform config for an EC2 instance with a replaceable EBS volume
#!/bin/bash
DEVICE=/dev/$(lsblk -n | awk '$NF != "/" {print $1}')
FS_TYPE=$(file -s $DEVICE | awk '{print $2}')
MOUNT_POINT=/data
# If no FS, then this output contains "data"
if [ "$FS_TYPE" = "data" ]
then
echo "Creating file system on $DEVICE"
@codeinthehole
codeinthehole / python-testing.md
Last active April 9, 2024 00:37
Python testing reference

Python testing reference

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

Contents:

@codeinthehole
codeinthehole / .pythonstartup.py
Created December 30, 2020 11:41
Python start-up file
# 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 / user-data.sh
Created August 18, 2014 12:41
Get the value of an EC2 instance's tag
#!/usr/bin/env bash
#
# Get the value of a tag for a running EC2 instance.
#
# This can be useful within bootstrapping scripts ("user-data").
#
# Note the EC3 instance needs to have an IAM role that lets it read tags. The policy
# JSON for this looks like:
#
# {
@codeinthehole
codeinthehole / docker-osx-shared-folders.rst
Last active November 11, 2023 01:22
How to share folders with docker containers on OSX

How to share a folder with a docker container on OSX

Mounting shared folders between OSX and the docker container is tricky due to the intermediate boot2docker VM. You can't use the usual docker -v option as the docker server knows nothing about the OSX filesystem - it can only mount folders from the boot2docker filesystem. Fortunately, you can work around this using SSHFS.

@codeinthehole
codeinthehole / decorators.py
Created February 7, 2013 16:39
Basic auth for Django snippet
import base64
from django.http import HttpResponse
from django.contrib.auth import authenticate
from django.conf import settings
def view_or_basicauth(view, request, *args, **kwargs):
# Check for valid basic auth header
if 'HTTP_AUTHORIZATION' in request.META:
@codeinthehole
codeinthehole / osx_bootstrap.sh
Last active October 10, 2023 12:21
Script to install stuff I want on a new OSX machine
#!/usr/bin/env bash
#
# Bootstrap script for setting up a new OSX machine
#
# This should be idempotent so it can be run multiple times.
#
# Some apps don't have a cask and so still need to be installed by hand. These
# include:
#
# - Twitter (app store)
@codeinthehole
codeinthehole / convert
Created September 28, 2023 10:28
Alfred workflow script for converting numbers to durations
#!/usr/bin/python3
#
# This script can be used as a Alfred Script Filter in a custom workflow for converting numbers to durations.
# To do so, create a new workflow, add a script filter with contents './convert "$@"', ensuring that the
# query is passed as "with input as argv".
#
# It's useful to connect a "Copy to Clipboard" output action to make it easier to paste the duration string somewhere.
import argparse
import json
@codeinthehole
codeinthehole / portforward.sh
Last active August 6, 2023 22:02
Bash helper function for easier port forwarding - should be added to your ~/.bashrc
# Easier port forwarding - usage:
#
# $ portforward project-app1 5432
#
# where 'project-app1' is an alias from ~/.ssh/config and 5432 is the remote
# port that you want to forward to.
function portforward() {
HOST=$1
REMOTE_PORT=$2
# Pick a random port and check it is free
@codeinthehole
codeinthehole / client.py
Last active July 27, 2023 07:40
Sample Python client for working with the Octopus Energy REST API
# 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