Skip to content

Instantly share code, notes, and snippets.

View davidwtbuxton's full-sized avatar

David Buxton davidwtbuxton

View GitHub Profile
@davidwtbuxton
davidwtbuxton / gist:4153560
Created November 27, 2012 10:39
Parsing XML
# Python 3
# http://www.reddit.com/r/learnpython/comments/13ugqf/getting_information_from_xml_python_3/
from xml.etree import ElementTree as ET
from urllib import request
URL = 'http://weather.yahooapis.com/forecastrss?w=3369&u=c'
# XML name-space for yweather elements. Note you have to use the correct
# name-space when selecting elements, which is what all the '{%s}' stuff
@davidwtbuxton
davidwtbuxton / gist:4135069
Created November 23, 2012 10:44
Demonstrates logic bug in scaling function
# Test for scaling function. Python 2.7.
# http://www.nerdydork.com/django-custom-upload-handler-to-compress-image-files.html
def scale_dimensions(width, height, longest_side):
if width > longest_side:
ratio = longest_side * 1. / width
return (int(width * ratio), int(height * ratio))
elif height > longest_side:
ratio = longest_side * 1. / height
return (int(width * ratio), int(height * ratio))
return (width, height)
@davidwtbuxton
davidwtbuxton / gist:3745307
Created September 18, 2012 19:33
Example of handling a file-upload with Bottle web framework
from __future__ import unicode_literals
from bottle import Bottle, request, response
from mypkg import analyse_data
# http://www.reddit.com/r/learnpython/comments/1037g5/whats_the_best_lightweight_web_framework_for/
# http://bottlepy.org/docs/dev/tutorial.html
app = Bottle()
import os
from collections import defaultdict
# http://www.reddit.com/r/learnpython/comments/yn52v/noob_advice_please_best_way_to_approach_this/
def normalized_filenames(dirname, func):
"""Builds a map from the normalized filename to the actual filename for
all the filenames in the directory and its sub-directories.
"""
# Use lists to hold values because more than 1 file can have the same key.
d = defaultdict(list)
@davidwtbuxton
davidwtbuxton / gist:2844639
Created May 31, 2012 16:39
Flask with more than one template folder
import jinja2
import flask
app = flask.Flask(__name__)
custom_loader = jinja2.ChoiceLoader([
# Re-use the default template dir first
app.jinja_loader,
# And add a new shared template dir
@davidwtbuxton
davidwtbuxton / gist:2788556
Created May 25, 2012 14:52
Decorated views and exceptions in Bottle
import bottle
import functools
def decorate(func):
"""Prints the arguments."""
@functools.wraps(func)
def wrapper(*args, **kwargs):
print args, kwargs
return func(*args, **kwargs)
@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()
@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: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 / 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):