Skip to content

Instantly share code, notes, and snippets.

View Wilfred's full-sized avatar

Wilfred Hughes Wilfred

View GitHub Profile
@Wilfred
Wilfred / cached_instances.py
Created September 17, 2013 17:44
Ensure no class gets instantiated twice with the same arguments.
import copy
def make_hash(obj):
"""Make a hash from an arbitrary nested dictionary, list, tuple or
set.
"""
if isinstance(obj, set) or isinstance(obj, tuple) or isinstance(obj, list):
return hash(tuple([make_hash(e) for e in obj]))
@Wilfred
Wilfred / gist:6010761
Created July 16, 2013 17:28
wget sentry homepage
$ wget "http://www.getsentry.com" [18:13:49]
--2013-07-16 18:19:37-- http://www.getsentry.com/
Resolving www.getsentry.com (www.getsentry.com)... 67.228.250.100
Connecting to www.getsentry.com (www.getsentry.com)|67.228.250.100|:80... failed: Connection timed out.
Retrying.
--2013-07-16 18:21:45-- (try: 2) http://www.getsentry.com/
Connecting to www.getsentry.com (www.getsentry.com)|67.228.250.100|:80... failed: Connection timed out.
Retrying.
@Wilfred
Wilfred / arithmetic.py
Created July 8, 2013 15:41
digg-style pagination with Django templates, e.g. "... 3 4 5 *6* 7 8 9 ..." and Twitter bootstrap
from django import template
"""It's sometimes really handy to be able to do basic arithmetic in
Django templates.
"""
register = template.Library()
@Wilfred
Wilfred / response_test.py
Last active December 19, 2015 07:18
Some additional assertions for convenient Django unit testing
from django.test import TestCase
class ResponseTestCase(TestCase):
"""Some additional assertions for Django responses."""
def assertHttp200(self, response):
status_code = response.status_code
self.assertEqual(
status_code, 200,
"Expected a HTTP 200 response, but got HTTP %d" % status_code)
@Wilfred
Wilfred / admin.py
Created June 27, 2013 13:28
adding custom admin actions for built-in Django models
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class MyUserAdmin(UserAdmin):
actions = ['my_action']
def my_action(self, request, queryset):
# stuff here...
@Wilfred
Wilfred / irc.conf
Created June 25, 2013 11:40
supervisor configuration for ii
[program:irc]
command = /usr/bin/ii -s irc.freenode.org -i /tmp -n bot_user_name
@Wilfred
Wilfred / fakeCheckbox.js
Created June 20, 2013 16:50
Styling <input type="checkbox"> using <span>s and updating the input accordingly according to events
// replace real checkboxes with fake ones using spans that work the same
$('input[type=checkbox]').each(function() {
var $input = $(this);
$input.hide();
var $fakeCheckbox = $('<div class="fake-checkbox"><span class="icon-TICK"></span></div>');
$input.after($fakeCheckbox);
// when our fake checkbox is clicked
$fakeCheckbox.click(function() {
@Wilfred
Wilfred / clickablecolumn.py
Created June 6, 2013 13:44
column with URL to a model for django_tables2
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
import django_tables2 as tables
class ClickableColumn(tables.Column):
"""Renders a link to edit the current object."""
def __init__(self, url_name, *args, **kwargs):
self.url_name = url_name
@Wilfred
Wilfred / dropdown.html
Created May 30, 2013 17:54
strange dropdown behaviour -- try opening the select. Only occurs on Chrom[e|ium] on Linux.
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
#dock {
width: 70px;
background-color: green;
height: 100%;
@Wilfred
Wilfred / tco.el
Last active December 17, 2015 13:19
toying with TCO in elisp
;; tco.el --- tail-call optimisation -*- lexical-binding: t -*-
(require 'dash)
(eval-when-compile (require 'cl))
(setq lexical-binding 't)
(defun tco-add-trampoline (fun-name new-name form)
"Given quoted soure FORM, replace calls to FUN-NAME (a symbol)
with a lambda expression that returns the result of the FUN-NAME call."
(--map