Skip to content

Instantly share code, notes, and snippets.

@glyphobet
glyphobet / 2dqueries.js
Created May 10, 2011 08:48
Example showing that MongoDB uses native units for regular 2d queries, and radians for spherical 2d queries
> db.dropDatabase();
{ "dropped" : "test", "ok" : 1 }
>
> // These points are one degree apart, which (according to Google Maps) is about 110 km apart
> // at this point on the Earth.
> db.points.insert({location: [-122, 37]});
> db.points.insert({location: [-122, 38]});
> db.points.ensureIndex({location:"2d"});
>
>
@glyphobet
glyphobet / hoover.py
Created July 21, 2011 04:59
Remove emacs poop and cruft lying around from mounting Linux filesystems on Mac OS X over sshfs
#!/usr/bin/env python
import sys
import os
import os.path
for a in sys.argv[1:]:
for root, dirs, files in os.walk(a):
for f in files:
if f.startswith(':2e_') or f.startswith('._') or f.endswith('~') or f == '.DS_Store':
print "rm %r" % os.path.join(root, f)
@glyphobet
glyphobet / tiny.js
Created July 26, 2011 01:02
Firefox bookmarklet to get the current page's short URL, if one is defined. If no short URL is defined, gives you the option of getting one from TinyURL instead.
(function(){
var promptURL=function (url){
window.prompt("Copy short URL to clipboard:",url);
};
var links=document.getElementsByTagName('link');
for(var i=0;i<links.length;i++){
var tag=links[i];
if(tag.getAttribute('rel')=='shorturl' || tag.getAttribute('rel')=='shortlink' || tag.getAttribute('id')=='shorturl' || tag.getAttribute('id')=='shortlink'){
promptURL(tag.getAttribute('href'));
return;
@glyphobet
glyphobet / jython_crasher.py
Created September 22, 2011 10:19
This crashes Jython (2.5.2 final), without a trailing newline at the end of the file. With the newline, it's fine. In CPython, it's fine with or without the trailing newline. Reported to Jython in: http://bugs.jython.org/issue1812
class Foo(object):
def foo(arg):
pass
# comment
@glyphobet
glyphobet / immutabledict.py
Created May 13, 2012 10:52
Immutable dictionary in Python
class ImmutableDict(dict):
def __setitem__(self, key, value):
raise TypeError("%r object does not support item assignment" % type(self).__name__)
def __delitem__(self, key):
raise TypeError("%r object does not support item deletion" % type(self).__name__)
def __getattribute__(self, attribute):
if attribute in ('clear', 'update', 'pop', 'popitem', 'setdefault'):
raise AttributeError("%r object has no attribute %r" % (type(self).__name__, attribute))
@glyphobet
glyphobet / elapsed_time.py
Created June 3, 2012 10:46 — forked from mwhooker/elapsed_time.py
humanize elapsed time
def elapsed_time(start, end):
"""
>>> from datetime import datetime
>>> elapsed_time(datetime(2000, 1, 1), datetime(2000, 1, 1, 20, 15))
'20 hours, and 15 minutes'
>>> elapsed_time(datetime(2000, 1, 1), datetime(2000, 1, 1, 0, 1))
'1 minute'
>>> elapsed_time(datetime(2000, 1, 1), datetime(2000, 1, 2, 0, 1))
'1 day, and 1 minute'
>>> elapsed_time(datetime(2000, 1, 1), datetime(2000, 1, 2, 2, 3, 4))
@glyphobet
glyphobet / pre-commit.sh
Created July 17, 2012 10:53
self-installing git-hook to prevent accidentally committing pdb.set_trace() calls
#!/bin/bash
mybasename=`basename $0`
if [ ! -e .git/hooks/${mybasename} ] ; then
ln -s ../../git-hooks/${mybasename} .git/hooks/${mybasename}
fi
set_traces=`git diff --cached -Gpdb\.set_trace\(\) | grep '^\+'`
if [ ! -z "$set_traces" ] ; then
@glyphobet
glyphobet / ChromeMacOSXFixes.sh
Created September 8, 2012 15:48
Fixes for Chrome and Mac OS X
# Use Mac OS X's print dialog instead of Chrome's idiotic print dialog
defaults write com.google.Chrome NSUserKeyEquivalents -dict-add 'Print Using System Dialog...' '@p' 'Print...' '~@p'
# Prevent Chrome from automatically deciding you want search results in German instead of English
find ~/Library/Application\ Support/Google/Chrome -name Preferences -print0 | xargs -0 perl -pi.bak -e 's{_google_url": "http://www.google.de/",}{_google_url": "http://www.google.com/",}g;'
# Add Quit menu item to Finder
defaults write com.apple.finder QuitMenuItem -bool yes
# Change Preview to default to "Single Page" view
@glyphobet
glyphobet / jquery.putdel.js
Created September 16, 2012 17:39
jQuery.put() and jQuery.del() functions for HTTP PUT and DELETE methods
/* Extend jQuery with functions for PUT and DELETE requests. */
/* Based on http://homework.nwsnet.de/news/9132_put-and-delete-with-jquery */
(function(){
function _ajax_request(url, data, callback, type, method) {
if (jQuery.isFunction(data)) {
callback = data;
data = {};
}
return jQuery.ajax({
@glyphobet
glyphobet / .bashrc-ack.sh
Created September 26, 2012 22:34
find ack
# Find ack on the system (even if it's been installed as ack-grep (linux) or ack-5.12 (MacPorts) and set up aliases
if [ "`compgen -c ack`" ]; then
ACK=`compgen -c ack | head -n 1`
alias ack=${ACK}
alias ackp="${ACK} --pager=less\ -R"
fi
# Note: this is not idempotent