Skip to content

Instantly share code, notes, and snippets.

View simonhayward's full-sized avatar

Simon Hayward simonhayward

View GitHub Profile
@simonhayward
simonhayward / main.go
Created August 21, 2020 06:43
simple static server
package main
import (
"log"
"net/http"
)
func logRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
@simonhayward
simonhayward / reservoir-sampling.py
Created August 17, 2020 09:55
reservoir sampling
"""
https://florian.github.io/reservoir-sampling/
- Store the first k elements as the reservoir elements
- For the i-th element, add it to the reservoir with a probability of k/i.
This is done by replacing a randomly selected element in the reservoir
"""
import random
import sys
@simonhayward
simonhayward / keybase.md
Created August 30, 2016 13:13
keybase.md

Keybase proof

I hereby claim:

  • I am simonhayward on github.
  • I am simonhayward (https://keybase.io/simonhayward) on keybase.
  • I have a public key whose fingerprint is C78E AB4C 3CCB 8657 CC05 011F 8AC0 2148 9CC8 9FA0

To claim this, I am signing this object:

@simonhayward
simonhayward / migrations_check.py
Created July 12, 2016 14:59
Check migrations ran
import logging
from django.db import DEFAULT_DB_ALIAS, connections
from django.db.migrations.loader import MigrationLoader
def migrations_have_applied():
"""
Check if there are any migrations that haven't been applied yet
"""
@simonhayward
simonhayward / .bashrc
Last active January 26, 2017 13:40
windows cygwin .bashrc - ssh-add
export SSH_AUTH_SOCK=/home/simon/.ssh-socket
ssh-add -l >/dev/null 2>&1
if [ $? = 2 ]; then
# No ssh-agent running
rm -rf $SSH_AUTH_SOCK
# >| allows output redirection to over-write files if no clobber is set
ssh-agent -a $SSH_AUTH_SOCK >| /tmp/.ssh-script
source /tmp/.ssh-script
echo $SSH_AGENT_PID >| ~/.ssh-agent-pid
@simonhayward
simonhayward / .bashrc
Last active December 21, 2015 09:07
Welcome screen .bashrc
# UTF-8
export LC_ALL=en_GB.UTF-8
export LANG=en_GB.UTF-8
export LANGUAGE=en_GB.UTF-8
export EDITOR=/usr/bin/vim
# tailoring 'less'
#######################################################
alias more='less'
@simonhayward
simonhayward / vimrc
Last active July 22, 2016 14:39
vimrc
" filetype plugin on
" filetype indent on
" Pathogen load
filetype off
call pathogen#infect()
call pathogen#helptags()
filetype plugin indent on
syntax on
@simonhayward
simonhayward / prepare-commit-msg
Last active August 29, 2015 14:04
git-hooks-prepare-commit-msg
#!/bin/sh
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
if [ -n "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "master" ]; then
echo "[$BRANCH_NAME] $(cat $1)" > $1
fi
# This file has been auto-generated by i3-config-wizard(1).
# It will not be overwritten, so edit it as you like.
#
# Should you change your keyboard layout somewhen, delete
# this file and re-run i3-config-wizard(1).
#
# i3 config file (v4)
#
# Please see http://i3wm.org/docs/userguide.html for a complete reference!
from string import ascii_uppercase as uc, ascii_lowercase as lc, maketrans
def rot_func(text, encode=True, rotate=13):
letters = uc + lc
rot = "".join((x[:rotate][::-1] + x[rotate:][::-1])[::-1] for x in (uc,lc))
src, trg = (letters, rot) if encode else (rot, letters)
trans = maketrans(src, trg)
return text.translate(trans)