Skip to content

Instantly share code, notes, and snippets.

View davidwtbuxton's full-sized avatar

David Buxton davidwtbuxton

View GitHub Profile
@davidwtbuxton
davidwtbuxton / gist:cc836e21c16d44e87c87
Created February 19, 2015 09:54
Python: using dict.update() is a lot slower than just setting a single key.
$ python -m timeit -s 'd = {}' 'd.update({"key": "value"})'
1000000 loops, best of 3: 0.293 usec per loop
$ python -m timeit -s 'd = {}' 'd["key"] = "value"'
10000000 loops, best of 3: 0.0433 usec per loop
@davidwtbuxton
davidwtbuxton / gist:045eff038c0bee38edff
Last active August 29, 2015 14:15
List all Django urls
# List all URLs in a Django application.
from collections import namedtuple
from django.conf.urls import RegexURLResolver, include
from django.views.generic import View
def walk_patterns(patterns, prefix=()):
"""Yields pairs of (RegexURLPattern, prefixes) where the first is the
pattern instance which has the actual view function, url name, etc. and the
@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
# 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: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()
@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:2219990
Created March 27, 2012 20:25
Memo-ize decorator that makes classes into singleton factories
# Based on
# http://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
class memoized(object):
"""Decorator that caches a function's return value each time it is called.
If called later with the same arguments, the cached value is returned, and
not re-evaluated.
"""
def __init__(self, func):
@davidwtbuxton
davidwtbuxton / gist:2291658
Created April 3, 2012 12:39
De-duplicate UKOOA data
# Python 2.7
from collections import OrderedDict
in_name = 'F87.p190'
out_name = 'my_results.txt'
results = OrderedDict()
@davidwtbuxton
davidwtbuxton / scotch.py
Created May 9, 2012 13:51
Scrape scotch prices, save as CSV
import csv
import requests
from BeautifulSoup import BeautifulSoup
from datetime import datetime
# http://www.reddit.com/r/learnpython/comments/tczgd/help_me_improve_this_code_webscraping_liquor/
# http://pastebin.com/TinHnCSp
# http://www.specsonline.com/cgi-bin/snf?body=/cgi-bin/prodlist&index=Liquors%7C255%7CSCOTCH+MALTS
@davidwtbuxton
davidwtbuxton / gist:2780839
Created May 24, 2012 11:04
Bottle route traversal
# Demonstration of inspecting all the routes, including those on sub-apps,
# from the default app instance.
#
# This should be run directly to print a list of route prefixes and the rules.
# Tested with Python 2.7 and Bottle-dev. Patch here
# https://github.com/davidwtbuxton/bottle/commit/ddd712ef252b06ecd0e957f8ac4e37b65ee79cae
import bottle
subapp = bottle.Bottle()