Skip to content

Instantly share code, notes, and snippets.

View dgouldin's full-sized avatar

David Gouldin dgouldin

  • Clara Labs
  • San Francisco, CA
View GitHub Profile

Keybase proof

I hereby claim:

  • I am dgouldin on github.
  • I am dgouldin (https://keybase.io/dgouldin) on keybase.
  • I have a public key whose fingerprint is 0864 6C45 4C16 F447 D49C 33CD 42A6 BBC2 87C3 0C5A

To claim this, I am signing this object:

from collections import OrderedDict
def groupby_unsorted(iterable, key=None):
'''
An implementation of itertools.groupby which eager evaluates groups and
therefore does not require its iterable to be pre-sorted. Groups are
returned in the order they are seen.
'''
key = key or (lambda x: x)
groups = OrderedDict()
def index_of_first(iterable, value):
match_func = value if callable(value) else (lambda x: x == value)
for index, item in enumerate(iterable):
if match_func(item):
return index
raise ValueError('Value not found')
#!/usr/bin/env python
from xml.etree import ElementTree
import requests
import unicodecsv as csv
import sys
url = "http://en.wikipedia.org/wiki/List_of_Game_of_Thrones_characters"
response = requests.get(url)
def get_characters(table):
@dgouldin
dgouldin / server.es6
Last active August 29, 2015 14:19
Gmail IDLE test
require("babel/register");
var Imap = require('imap');
var imap = new Imap({
user: process.env.USERNAME,
password: process.env.PASSWORD,
host: 'imap.gmail.com',
port: 993,
tls: true
});
@dgouldin
dgouldin / noparse.py
Created October 19, 2010 16:43
Django template block tag {% noparse %} renders template syntax characters within the block as normal text.
from django import template
register = template.Library()
token_formats = {
template.TOKEN_TEXT: '%s',
template.TOKEN_VAR: '%s%%s%s' % (template.VARIABLE_TAG_START, template.VARIABLE_TAG_END),
template.TOKEN_BLOCK: '%s%%s%s' % (template.BLOCK_TAG_START, template.BLOCK_TAG_END),
template.TOKEN_COMMENT: '%s%%s%s' % (template.COMMENT_TAG_START, template.COMMENT_TAG_END),
}
@dgouldin
dgouldin / google_pycon_quiz.py
Created March 11, 2011 21:02
My entry to Google's PyCon Quiz: Count and sum all positive palindromic numbers up to a googol
def googol():
def palindromes(end):
for odd_length in (True, False):
left = 0
last_palindrome = 0
while last_palindrome < end:
left += 1
left_str = str(left)
right_str = left_str[::-1]
if odd_length:
@dgouldin
dgouldin / google_quiz.py
Created March 11, 2011 22:50
Google Quiz answer
def googol():
power = 100
count = sum([2*9*10**n for n in range(power/2)])
count_sum = sum([int(digit) for digit in str(count)])
return (count, count_sum)
@dgouldin
dgouldin / fixture_utils.py
Created September 7, 2011 22:47
Some simple django fixture utils
from django.core import serializers
from django.utils import simplejson
def add_to_fixture(qs, filename):
f = open(filename, 'r')
data_python_existing = simplejson.loads(f.read())
f.close()
data_json_unformatted = serializers.serialize('json', qs)
data_python_new = simplejson.loads(data_json_unformatted)
data_python_existing.extend(data_python_new)
@dgouldin
dgouldin / incrdecr.py
Created October 10, 2011 21:19
At attempt at an atomic and bounded cache incr/decr function which handles key existence.
from django.core.cache import cache
def _incr_decr(key, incr=True, min=None, max=None, initial=None):
'''Incr/decr function which handles key creation as needed'''
# FIXME: min/max have rampant race conditions which could be fixed using
# memcache's "cas" feature, but current pylibmc + libmemcached causes a
# segfault when used.
initial = initial or min or 0