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__'
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: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 / irccexport.py
Created March 14, 2014 23:21
Quick-and-dirty IRCCloud log export tool
import requests
import json
import sys
from collections import deque
from concurrent.futures import ThreadPoolExecutor as Pool
from datetime import datetime
from os import mkdir
from os.path import isdir
def debug(*args, **kwargs):
@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:3848296
Created October 7, 2012 12:46
Ruby HTML Diff
require 'hpricot'
require 'diff/lcs'
class HTMLDiff
def self.diff a, b
self.new(a, b).compare
end
def initialize a, b
@dpk
dpk / gist:3828768
Created October 3, 2012 18:21
don't use more than 100,000 cons pairs
; <sbp> this is why lisp engineers aren't allowed to pilot submarines
(setq *all-conses* (make-array 100000))
(dotimes (ii 100000) (setf (aref *all-conses* ii) (cons nil nil)))
(setq *conses-index* 0)
(defun cons (hd tl)
(let ((the-cons (aref *all-conses* *conses-index*)))
(setq *conses-index* (1+ *conses-index*))
(setf (car the-cons) hd)