Skip to content

Instantly share code, notes, and snippets.

@whilefalse
whilefalse / Pymongo Dereference a List
Created August 21, 2009 09:04
Pymongo has some really cool auto dereferencing stuff build in. However, web.py was playing with my session object and saving back the dereferenced version. This allows me to dereference a list of DBRefs manually.
def deref_list(db, list):
return map(lambda(ref): db.dereference(ref), list)
def current_host(request):
url = 'http://%s' % request.META['SERVER_NAME']
if request.META['SERVER_PORT'] != 80:
url = url + ':%s' % request.META['SERVER_PORT']
return url
@whilefalse
whilefalse / Python Union-Find
Created August 24, 2009 12:19
Simple implementation of union-find for arbitrary objects in Python
def union(parent,child):
"""Performs a union by attaching the root node of the smaller set to the
root node of the larger set"""
if parent.set_size < child.set_size:
child, parent = parent, child
parent_head = find(parent)
child_head = find(child)
if parent_head == child_head:
@whilefalse
whilefalse / gist:208377
Created October 12, 2009 13:13
A simple git pre-commit nosetest runner, with configurable test directories. Move to .git/hooks/pre-commit and make executable to enable.
#!/bin/bash
echo -e "======================================================================================\n"
echo " >>> Running pre-commit hook..."
dirs=`git-config --get tests.directories`
if [ -z "$dirs" ]
then
echo -e " >>> You have no tests.directories setting. Tests will not be run.\n >>> To enable tests on pre-commit run:\n\tgit config --add tests.directories <space separated list of test dirs>\n"
echo -e "======================================================================================\n"
class toggler(object):
def __init__(self, getter, setter):
self.getter = getter
self.setter = setter
def __enter__(self, *args, **kwargs):
self.setter(not self.getter())
__exit__ = __enter__
@whilefalse
whilefalse / gist:1019087
Created June 10, 2011 15:42
Bundle install git post-merge hook
#!/bin/bash
if [ ! -z `git diff --name-only HEAD@{1}..HEAD Gemfile` ]
then
echo -e "\033[0;31m*** Gemfile change detected, running bundle install ***\033[0m"
bundle install
fi
@whilefalse
whilefalse / gist:1170890
Created August 25, 2011 15:14
LazyMock - responds to eveything.
class LazyMock
def method_missing(*args)
LazyMock.new
end
def respond_to?(*args)
true
end
end
@whilefalse
whilefalse / apod-grabber.rb
Created May 9, 2012 12:59
Apod Image Grabber
%w{nokogiri open-uri fileutils date}.each(&method(:require))
base = 'http://apod.nasa.gov/apod/'
FileUtils.mv(open(base + Nokogiri::HTML(open(base)).css('img')[0].attributes['src'].value), "apod-#{Date.today.strftime('%Y-%m-%d')}.jpg")