Skip to content

Instantly share code, notes, and snippets.

View dpk's full-sized avatar

Daphne Preston-Kendal dpk

View GitHub Profile
@dpk
dpk / enviro.py
Last active December 17, 2015 07:19
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
dpk / bfc
Last active February 18, 2022 15:20
Brainfuck compiler in 182 bytes. (172 without shebang; 163 without shebang or automatic feeding to C compiler)
#!/bin/sh
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 irc.freenode.net 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'
EOF
@dpk
dpk / gist:4757681
Last active January 29, 2016 13:40
RFC 3986 URI BNF translated to a regular expression. (Uses Ruby/Oniguruma/Onigmo named capture syntax)
(?xi)
\A
# 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
dpk / gist:4660307
Last active December 11, 2015 21:09
A magnitude-comparison monkey-patch for Ruby's case. Suggested at http://swhack.com/logs/2013-01-28#T23-17-31
# usage:
#
# case compare(2,3)
# when :>
# puts "greater"
# when :<
# puts "less"
# when :==
# puts "same"
# end
@dpk
dpk / gist:4346535
Created December 20, 2012 16:36
French Republican calendar: decimal time
#!/usr/bin/env ruby
def dectime
t = Time.now
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
[h,m,s]
end
@dpk
dpk / gist:4223533
Created December 6, 2012 10:28
Apple's CFHash algorithm for strings in Ruby
# Why? Why not!
def cfhash string
result = string.length
max = 2 ** 32
string.each_codepoint do |cp|
result = ((result * 257) % max) + cp
end
return (result + (result << (string.length & 31))) % max
end
@dpk
dpk / TODO.md
Created November 14, 2012 21:20
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
dpk / munsell.rb
Created November 4, 2012 12:46
translate Munsell colours to RGB triplets
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 http://www.color.org/srgb.pdf
SRGBMAT = Matrix[[ 3.2406, -1.5372, -0.4986],
@dpk
dpk / gist:3969332
Created October 28, 2012 18:11
Klop's fixed-point combinator. Needs a lazy-evaluated Scheme.
(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)
1
(* n (c (- n 1))))))