Skip to content

Instantly share code, notes, and snippets.

View davidwtbuxton's full-sized avatar

David Buxton davidwtbuxton

View GitHub Profile
@davidwtbuxton
davidwtbuxton / angular.md
Created July 12, 2016 15:23
For future reference

If you want to make us angry you can do

<ng-class="{'ng-hidden': condition}">

instead of ng-hidden="condition"

or even better

@davidwtbuxton
davidwtbuxton / views.py
Created March 31, 2016 06:47
Serving Google Cloud Storage files from App Engine without having to read them
import mimetypes
from django.http import HttpResponse
from google.appengine.api import app_identity
from google.appengine.ext import blobstore
def serve_cloud_storage_file(request, object_name):
"""Serve a file from cloud storage by setting a header on the response.
The blobstore will serve it for you.
@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: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 / 419 spam
Last active December 28, 2015 05:09
Recent 419 spam. Particularly like the unambiguous "three (3) of the Terrorists".
From: "Captain Michael Kelvington"<[email protected]>
Subject: DEAR FRIEND
Date: Thu, 17 Oct 2013 23:39:09 +0200
Dear Friend
Forgive my indignation if this message comes to you as a surprise and may
offend your personality for contacting you without your prior consent and
Writing through this channel. I got your contact from the professional data
base found in the internet Yahoo tourist search; I was searching for a foreign
@davidwtbuxton
davidwtbuxton / bug.md
Last active December 10, 2015 23:28
Description of bug in BBC's feeds.

11 January 2013. Double-encoded markup in BBC Atom/RSS feeds.

BBC Atom feeds are double-encoding XHTML markup. But content of an <content type="xhtml">...</content> element must be the actual markup, not escaped markup.

This bug results in reader software showing the markup rather than the formatted content.

See Atom processing model: http://tools.ietf.org/html/rfc4287#section-4.1.3.3

E.g. http://www.bbc.co.uk/blogs/radio4/atom/ contains an entry like this (edited for clarity). Note the <p> tag is encoded as &lt;p&gt;.

@davidwtbuxton
davidwtbuxton / gist:4493982
Created January 9, 2013 15:26
Sketch of querying and updating rows in DB2 from an Excel 97-2004 format file.
# http://www.reddit.com/r/learnpython/comments/167cay/using_python_with_excel_and_sql/
# pip install xlwt ibm_db
import xlrd
import logging
import ibm_db_dbi
logging.basicConfig(level=logging.DEBUG)
@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()