Skip to content

Instantly share code, notes, and snippets.

View davidwtbuxton's full-sized avatar

David Buxton davidwtbuxton

View GitHub Profile
@davidwtbuxton
davidwtbuxton / grouper.py
Last active August 10, 2017 23:27
Like itertools.grouper but the last group doesn't have padding
import itertools
def grouper(iterable, n):
"""Collect data into chunks.
This is different to the grouper recipe in itertools in that the final
chunk may be shorter.
>>> grouper('ABCDEF', 3)
#!/usr/bin/env python2
import csv
import hashlib
import sys
# Calculate an MD5 checksum for every row in the first column of a CSV.
# Example:
# $ python digests.py < src.csv > dest.csv
@davidwtbuxton
davidwtbuxton / gist:ca843407f025e1b5b4585aa415885321
Last active October 12, 2016 18:22
Benchmarks for bencodepy.encode and buxtor.bencode.
# Speed comparison of bencodepy and buxtor.bencode
# bencodepy is consistently 0.3 - 0.5 seconds faster. Why?
# bencodepy: https://github.com/eweast/BencodePy/blob/master/bencodepy/encode.py
# 4914005 function calls (3618005 primitive calls) in 2.830 seconds
#
#
# buxtor.bencode: https://github.com/davidwtbuxton/buxtor/blob/master/buxtor/bencode.py
# 5592005 function calls (4548005 primitive calls) in 3.183 seconds
@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"<test@jlcity.gov.cn>
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)