Skip to content

Instantly share code, notes, and snippets.

View dpk's full-sized avatar

Daphne Preston-Kendal dpk

View GitHub Profile
module Rubinius
class CompiledCode
def _dump(depth)
Marshal.dump([@scope, Rubinius::ToolSets::Runtime::CompiledFile::Marshal.new.marshal(self)])
end
def self._load(string)
scope, dump = Marshal.load(string)
cm = Rubinius::ToolSets::Runtime::CompiledFile::Marshal.new.unmarshal(dump)
cm.scope = scope
@dpk
dpk / primesieve.py
Created December 7, 2014 23:47
infinite sequence of primes generator in Python (unbounded Sieve of Eratosthenes)
def primes():
prime_multiple_factors = {}
def add_factor(composite, factor):
if composite not in prime_multiple_factors:
prime_multiple_factors[composite] = set([factor])
else:
prime_multiple_factors[composite].add(factor)
x = 2
while True:
@dpk
dpk / jslen.py
Created February 15, 2015 18:56
JavaScript length of a string (Python)
# jslen -- find the JavaScript length of a string (Python 3)
#
# Note to Python developers: since you know it as of Python 3.3, it'd
# be nice to have a way to find out in O(1) if a string contains any
# wide characters.
# find the preferred (i.e. faster) byte order
if ''.encode('utf-16') == b'\xFE\xFF':
preferred_encoding = 'utf-16le'
else:
"""
McCarthy's amb operator (kind of) implemented in Python
ᚾᚪᚻᛏ᛫ᛁᛋ᛫ᚦᚱᚫᛞᛋᛁᚳᚩᚱ
very unoptimized, much slow, wow
"""
class amb:
def __init__(self, *values):
self.values = tuple(values)
if len(self.values) < 2:
@dpk
dpk / gist:a92d3ea83aa7142e3317
Created June 11, 2015 08:40
Restart frontmost application with AppleScript (revised version of my script from c. 2011)
tell application "System Events" to set _app to the name of the first process whose frontmost is true
tell application _app
quit
set _isRunning to true
repeat while (_isRunning)
delay 1
tell application "System Events"
set _isRunning to ((name of processes) contains _app)
end tell
end repeat
if (('devicePixelRatio' in window && window.devicePixelRatio >= 1.5) ||
('matchMedia' in window && window.matchMedia("(-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3/2), (min-resolution: 1.5dppx)").matches))
{
document.addEventListener('DOMContentLoaded', function() {
var elements = document.getElementsByClassName('retina');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
if (element.tagName !== 'IMG') continue;
var match = element.src.match(/^(.+)\.(.+?)$/);
@dpk
dpk / README.md
Last active September 20, 2015 17:10 — forked from mbostock/.block
Albers Equal-Area Conic
@dpk
dpk / allcaps.pl
Last active September 23, 2015 14:08
A smarter way to all-caps a string.
#!/usr/bin/perl -w
# A smarter string capitaliser. Everyone knows that in words that start with certain prefixes,
# the prefix should be left in lower- or mixed-case when the word is made 'all-caps.'
# For instance, MacDonald becomes MacDONALD rather than MACDONALD, iPod becomes
# iPOD instead of IPOD, etc. This script attempts to be vaguely clever about doing that while
# also avoiding doing the same with intercapsed or camelcased product names like QuarkXPress.
# The cut-off point was set at four characters because the longest surname prefix that ought
# to be left in mixed-case that I could think of was 'Fitz,' but this causes problems with some
# product names, and they can be listed in @exceptions. Included are MacBook, AirPort, WiFi,
# and PostScript. If you have any other word suggestions, mail them to me at the address on
// A javascript port of my Perl string capitaliser. Possibly someone who is better at
// DOM scripting than me could make a script that would automatically apply this to any
// element styled with text-transform: uppercase.
// Again, the algorithm is too simple to be worth insisting on attribution, but if you
// do use this script anywhere, please attribute it to me in a comment block somewhere.
// By David Kendal, http://dpk.org.uk/
// see also the original perl version at http://gist.github.com/567255
function is_exception (word, exceptions) { // possibly there's a routine built-in to JavaScript to do this
word = word.toLowerCase();
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use JSON;
use Getopt::Long;
use URI::Escape qw( uri_escape_utf8 );
# options
my $query;
my $titles;