Skip to content

Instantly share code, notes, and snippets.

View davidwtbuxton's full-sized avatar

David Buxton davidwtbuxton

View GitHub Profile
@davidwtbuxton
davidwtbuxton / gist:2043869
Created March 15, 2012 11:57
Comparison of dictionary types
# Comparison of dictionary types
# http://www.reddit.com/r/Python/comments/qxjho/sdict_sorted_dictionary_for_python_sdict_is_a/
from sdict import Dict
from collections import OrderedDict
import itertools
import timeit
def keygen():
"""A generator that produces all possible combinations of letters."""
@davidwtbuxton
davidwtbuxton / gist:2013644
Created March 10, 2012 22:28
Scrape metacritic for scores
#!/usr/bin/env python
# http://www.reddit.com/r/learnpython/comments/qqnqt/metacritic_score_grabber/
import urllib2
import re
url = 'http://www.metacritic.com/game/xbox-360/mass-effect-3'
r = urllib2.urlopen(url)
html = r.read()
# http://www.reddit.com/r/learnpython/comments/qkh43/new_to_python_searching_csv_files/
# http://stackoverflow.com/questions/9564322/loop-through-rows-of-one-csv-file-to-find-corresponding-data-in-another
# http://stackoverflow.com/questions/9577997/search-through-csv-from-specific-row-down
import csv
# Difference constants. Note these are floats, so don't expect perfect decimal
# mathematics.
DELTA_HI = 0.001
@davidwtbuxton
davidwtbuxton / gist:1827083
Created February 14, 2012 14:27
Comparison of LBYL versus EAFP
# Comparison of LBYL versus EAFP
# http://www.reddit.com/r/Python/comments/pou99/python_performance_tips_part_1/
from __future__ import print_function
from hashlib import md5
_lbyl_cache = {}
_eafp_cache = {}
@davidwtbuxton
davidwtbuxton / gist:1683234
Created January 26, 2012 15:19
Noughts and crosses in Python
# Noughts and crosses for Python 2
# http://www.reddit.com/r/learnpython/comments/oxgr0/converting_to_and_from_ternary/
# Winning positions, represented as bit masks
wins = [
'111 000 000', '000 111 000', '000 000 111', # Across
'100 100 100', '010 010 010', '001 001 001', # Down
'100 010 001', '001 010 100', # Diagonal
]
# Store winning boards as integers