Skip to content

Instantly share code, notes, and snippets.

javascript: (function() {
var url = 'http://journeyplanner.tfl.gov.uk/user/XSLT_TRIP_REQUEST2';
var default_location = "MY_DEFAULT_LOCATION";
var input = '%s';
var matches = input.match(/(^\s*(.*)\s*>\s*(.*)\s*$|^\s*(.*)\s*<\s*(.*)\s*$)/);
if( ! matches) {
window.location = url;
<?php
/*
* Postcode validation function
* Based on code by John Gardner: http://www.braemoor.co.uk/software/postcodes.shtml
* Converted to use PCRE rather than the now deprecated POSIX regular expressions by
* David Evans.
*
* Optional second argument is set to the properly capitalised and spaced version of
* the postcode.
@evansd
evansd / fab_completion.sh
Created October 15, 2010 17:24
Tab-completion script for Fabric (Debian/Ubuntu style)
have fab && {
_fab_completion()
{
COMPREPLY=()
local cur tasks
tasks=$(fab --shortlist 2>/dev/null)
_get_comp_words_by_ref cur
COMPREPLY=( $(compgen -W "${tasks}" -- ${cur}) )
@evansd
evansd / gist-embed.php
Created November 7, 2010 21:03
WordPress plugin for embedding gists
<?php
/*
Plugin Name: Gist Embed
Description: Embed Gists from Github with robust caching
Author: David Evans
Version: 0.1
*/
@evansd
evansd / heapqutils.py
Created May 17, 2011 17:18
Some useful iterator functions
# For more explanation and usage examples, see:
# http://www.drhevans.com/blog/posts/331-comparing-large-data-sets-in-python/
from itertools import imap, izip, count, repeat, tee
import heapq
def full_outer_join(*iterables, **kwargs):
"""
Perform a full outer join on a sequence of sorted iterables, where the
key function supplies the join condition.
@evansd
evansd / postinstall.sh
Created June 28, 2011 21:30
postinstall sript from vagrant example box (having it somewhere web-accessible is easiest way to get it on to newly created VMs)
# Apt-install various things necessary for Ruby, guest additions,
# etc., and remove optional things to trim down the machine.
apt-get -y update
apt-get -y remove apparmor
apt-get -y install linux-headers-$(uname -r) build-essential
apt-get -y install zlib1g zlib1g-dev libxml2 libxml2-dev libxslt-dev libssl-dev openssl libreadline5-dev
apt-get clean
# Remove this file to avoid dhclient issues with networking
rm -f /etc/udev/rules.d/70-persistent-net.rules
@evansd
evansd / gist:1639992
Created January 19, 2012 13:09
Global find and replace using ack
function substitute {
if [ -z "$1" -o -z "$2" ]; then
echo "Usage: substitue FROM_STRING TO_STRING [OPTION]..."
echo
echo "Replace all occurances of FROM_STRING (a sed-compatible regular"
echo "expression) with TO_STRING in all files for which ack-grep matches"
echo "FROM_STRING."
echo
echo "Any additional options are passed directly to ack-grep (e.g.,"
echo " --type=html would only run the substitution on html files)."
@evansd
evansd / opensitepackages.sh
Created February 15, 2012 09:52
Open site-packages directory of currently active virtualenv in Nautlius
# Open site-packages directory of currently active virtualenv in Nautlius
# (Relies on virtualenvwrapper)
function opensitepackages {
virtualenvwrapper_verify_workon_home || return 1
virtualenvwrapper_verify_active_environment || return 1
typeset site_packages="`virtualenvwrapper_get_site_packages_dir`"
\gnome-open "$site_packages"/$1
}
@evansd
evansd / on_parent_exit.py
Created April 9, 2012 21:19
Ensure subprocesses exit when their parents die
"""
Utility (Linux only) to ensure subprocesses exit when their parents do by sending
a specified signal when the parent dies.
Usage:
subprocess.Popen(['some-executable'], preexec_fn=on_parent_exit('SIGHUP'))
"""
import signal
@evansd
evansd / defer_signals.py
Last active March 31, 2020 15:39
Context manager to defer signal handling until context exits
# The MIT License (MIT)
#
# Copyright (c) 2013 David Evans
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions: