Skip to content

Instantly share code, notes, and snippets.

dpk dpk

View GitHub Profile
View marshal_proc.rb
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 Dec 7, 2014
infinite sequence of primes generator in Python (unbounded Sieve of Eratosthenes)
View primesieve.py
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 Feb 15, 2015
JavaScript length of a string (Python)
View jslen.py
# 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:
View mccarthy.py
"""
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 Jun 11, 2015
Restart frontmost application with AppleScript (revised version of my script from c. 2011)
View gist:a92d3ea83aa7142e3317
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
View retina.js
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 Sep 20, 2015 — forked from mbostock/.block
Albers Equal-Area Conic
@dpk
dpk / allcaps.pl
Last active Sep 23, 2015
A smarter way to all-caps a string.
View allcaps.pl
#!/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
View gist:597837
// 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();
View gist:619192
#!/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;
You can’t perform that action at this time.