Skip to content

Instantly share code, notes, and snippets.


Daphne Preston-Kendal dpk

View GitHub Profile
dpk /
Last active Dec 17, 2015
utility functions to make `if __name__ is "__main__":` less painful in Python
# Copyright David Kendal, 2013
# You may redistribute this code, modified or unmodified, provided that this article is preserved in its entirety.
# Like all software, and information generally, this work is without warranty.
import inspect
# "if isscript():" is "if __name__ is '__main__':"
def isscript(frames=1):
callers = inspect.getouterframes(inspect.currentframe())
return callers[frames][0].f_globals['__name__'] == '__main__'
dpk / bfc
Last active Dec 16, 2015
Brainfuck compiler in 182 bytes. (172 without shebang; 163 without shebang or automatic feeding to C compiler)
View bfc
tr -Cd '][.,<>+-'|sed 's/\./putchar(*p);/g;s/,/*p=getchar();/g;s/[+-]/&&*p;/g;s/[<>]/&&p;/g;s/\[/while(*p){/g;y/]<>/}-+/;s/^/main(){int a[30000];int *p=a;/;s/$/}/'|cc -xc -
exec tcpclient 6667 sh <<EOF
echo "NICK topicsed" >&7
echo "USER topicsed +iw swtopic :topicsed" >&7
echo "JOIN #swhack" >&7
cat <&6 | sed -n '/TOPIC #swhack :.*/ s/.*:\(.*\).*/\1/p'
dpk / gist:4757681
Last active Jan 29, 2016
RFC 3986 URI BNF translated to a regular expression. (Uses Ruby/Oniguruma/Onigmo named capture syntax)
View gist:4757681
# unreserved: [a-z0-9\-._~]
# sub-delims: [!$&'()*+,;=]
# pct-encoded: %[0-9a-f]{2}
# dec-octet: (?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
# pchar: (?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})
# h16: [0-9a-f]{1,4}
# ls32: (?:[0-9a-f]{1,4}:[0-9a-f]{4}|
# (?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
dpk / gist:4660307
Last active Dec 11, 2015
A magnitude-comparison monkey-patch for Ruby's case. Suggested at
View gist:4660307
# usage:
# case compare(2,3)
# when :>
# puts "greater"
# when :<
# puts "less"
# when :==
# puts "same"
# end
dpk / gist:4346535
Created Dec 20, 2012
French Republican calendar: decimal time
View gist:4346535
#!/usr/bin/env ruby
def dectime
t =
daysec = t.hour * 3600 + t.min * 60 + t.sec
h = (daysec / 8640.0).floor
m = ((daysec / 86.4) % 100).floor
s = ((daysec / 0.864) % 100).floor
dpk / gist:4223533
Created Dec 6, 2012
Apple's CFHash algorithm for strings in Ruby
View gist:4223533
# Why? Why not!
def cfhash string
result = string.length
max = 2 ** 32
string.each_codepoint do |cp|
result = ((result * 257) % max) + cp
return (result + (result << (string.length & 31))) % max
dpk /
Created Nov 14, 2012
Fauxlot, a sketch for a duxlot/phenny-like bot

To do

  • command interface

    most commands should be web services; all commands should use the same interface. like a Rack application, but for commands!

    for instance, i could define a WebServiceCommand class that would just take the url of an oblique-ish service and execute on it;

dpk / munsell.rb
Created Nov 4, 2012
translate Munsell colours to RGB triplets
View munsell.rb
require 'matrix'
require 'interpolator'
class Munsell
HUES = %w{R YR Y GY G BG B PB P RP}.map(&:to_sym)
INVHUES = %w{RY YG GB BP PR}.map(&:to_sym)
# from
SRGBMAT = Matrix[[ 3.2406, -1.5372, -0.4986],
dpk / gist:3969332
Created Oct 28, 2012
Klop's fixed-point combinator. Needs a lazy-evaluated Scheme.
View gist:3969332
(define L (lambda (a) (lambda (b) (lambda (c) (lambda (d) (lambda (e) (lambda (f) (lambda (g) (lambda (h) (lambda (i) (lambda (j) (lambda (k) (lambda (l) (lambda (m) (lambda (n) (lambda (o) (lambda (q) (lambda (p) (lambda (s) (lambda (t) (lambda (u) (lambda (v) (lambda (w) (lambda (x) (lambda (y) (lambda (z) (lambda (r) (r ((((((((((((((((((((((((((t h) i) s) i) s) a) f) i) x) e) d) p) o) i) n) t) c) o) m) b) i) n) a) t) o) r)))))))))))))))))))))))))))))
(define Yk (((((((((((((((((((((((((L L) L) L) L) L) L) L) L) L) L) L) L) L) L) L) L) L) L) L) L) L) L) L) L) L))
(define (factorial c)
(lambda (n)
(if (= n 0)
(* n (c (- n 1))))))