Skip to content

Instantly share code, notes, and snippets.

@kgaughan
kgaughan / demo.py
Last active April 29, 2019 06:27
Streaming back responses from a WSGI application using the chunked transfer encoding.
#!/usr/bin/env python
import time
body = r"""\
<!DOCTYPE html>
<html>
<head>
<title>Streaming response demo</title>
@kgaughan
kgaughan / gist:5867299
Created June 26, 2013 13:19
Fetching charity numbers listing from the Revenue Commissioners (Irish government revenue service)
import itertools
import contextlib
import httplib
import urllib2
import xlrd
def parse(fh, sheet=0, mappings=(), start=0, stop=None, step=1):
sheet = xlrd.open_workbook(file_contents=fh).sheet_by_index(0)
@kgaughan
kgaughan / gist:5832700
Created June 21, 2013 17:05
My old ARM assembly language Brainfuck interpreter
REM>Brainfuck
REM by Urban Mueller
REM RiscOS Assembler version by Keith Gaughan
REM v2.01 28-Aug-1999, 03:19am
ON ERROR PRINT REPORT$;" at ";ERL:END
DIM code% 2048
sp=13:link=14:pc=15
file$="<Obey$Dir>.Brainfuck"
:
@kgaughan
kgaughan / threadpoolss.py
Created June 10, 2013 16:13
All the thread pool mixins for SocketServer are, well, not that good, so I knocked together my own. This one can cleanly shut down the pool
"""
Thread pool extensions to SocketServer.
"""
import Queue
import SocketServer
import sys
import threading
@kgaughan
kgaughan / gist:5748234
Last active December 18, 2015 07:39
Rewriting one path to another with mod_rewrite
RewriteEngine On
RewriteBase /
RewriteRule ^view/(.*)$ component/extension/$1 [QSA,L]
@kgaughan
kgaughan / gist:5574899
Created May 14, 2013 09:57
Arbitrary object importation.
# 'pkg_name' is the name of a package/module. May be dotted
# 'obj_name' is the name of an object within said package.
# Why there isn't a straightforward function in the standard library for dealing with this already boggles my mind.
obj = __import__(pkg_name, fromlist=[object_name.split('.', 1)[0]])
for segment in object_name.split('.'):
obj = getattr(obj, segment)
d1 = {}
d1 = {}
# get intersection of key sets.
common = set(d1) & set(d2)
# Check if all common keys map to the same value.
are_same = all(d1[k] == d2[k] for k in common)
# Get those common keys where the values differ.
differences = [k for k in common if d1[k] != d2[k]]
@kgaughan
kgaughan / gist:5501917
Created May 2, 2013 12:37
Using urllib2 with Basic auth (because I can never remember the silly dance that has to be done).
import urllib2
mgr = urllib2.HTTPPasswordMgr()
mgr.add_password('realm', 'http://example.com/base/', 'username', 'password')
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(mgr)))
urllib2.urlopen('http://example.com/base/something/')
@kgaughan
kgaughan / gist:5495464
Created May 1, 2013 14:11
BUILD ALL THE PYTHONS!
#!/bin/sh
find . -maxdepth 1 -mindepth 1 -type d -exec rm -rf {} \;
for i in *.tar.bz2; do
tar xjf $i && (
echo '==>' Attempting build of ${i%.tar.bz2}
cd ${i%.tar.bz2} && \
./configure --prefix=/opt/python-ci && \
make && \
make install && echo WIN || echo FAIL
)
@kgaughan
kgaughan / gist:5488684
Created April 30, 2013 13:28
Given an IP address (either IPv4 or IPv6) and an array of IP ranges in CIDR notation, check if the given address is in any of those ranges.
<?php
function check_if_in_ranges($ip, array $ranges) {
$chunked_ip = to_chunked_ip($ip);
foreach ($ranges as $range) {
$parts = explode('/', $range, 2);
$chunked_range = to_chunked_ip($parts[0]);
if (count($chunked_range) != count($chunked_ip)) {
continue;
}