Skip to content

Instantly share code, notes, and snippets.

@mtigas
mtigas / multibyte_count.py
Created February 4, 2011 00:28
Python: Proper multibyte character counting (for Twitter)
# Example of making sure that multibyte characters are counted
# properly with regard to APIs such as Twitter.
#
# See http://dev.twitter.com/pages/counting_characters
#
# Uses example from above link:
# cafe (with diacritical):
# 0x63 0x61 0x66 0xC3 0xA9 (composed character)
# 0x63 0x61 0x66 0x65 0xCC 0x81 (e + combining diacritical)
#!/bin/bash
#
# usat_import
# Fork by Mike Tigas
# Based on work by Ryan Nagle, Chris Groskopf and Brian Boyer
#
# Updated to allow import of multiple states into the same database.
# Unlike the original script, this *only* generates super-wide tables.
#
# Shapes are stored in WGS84 (EPSG:4326) for portability.
@mtigas
mtigas / pypy.rb
Created April 18, 2011 18:15
pypy homebrew recipe, using nightly on OSX 64bit
require 'formula'
require 'hardware'
class Pypy < Formula
if MacOS.prefer_64_bit?
url 'http://buildbot.pypy.org/nightly/trunk/pypy-c-jit-42951-88b090e851cc-osx64.tar.bz2'
md5 'f2fc482e8de56171f05ad83d92846acf'
version '1.5.0a'
else
url 'http://pypy.org/download/pypy-1.4.1-osx.tar.bz2'
@mtigas
mtigas / donottrack_middleware.py
Created April 25, 2011 22:03
Django middleware to aid in implementing support for "Do Not Track" HTTP headers.
"""
A Django middleware, to aid in implementing the HTTP header-based
"Do Not Track" mechanism described in [1] and a few of the myriad variations.
Known browsers utilizing this option include Firefox 4+, Internet Explorer 9+,
and Safari under Mac OS X 10.7.
This does nothing by itself. It remains the application's responsibility to
ensure that the `tracking_opt_out` attribute is used to toggle first party
tracking and disable or notify third parties of the user's preference.
#!/usr/bin/env python
import sys
def getint(data, offset, intsize):
"""Retrieve an integer (big-endian) and new offset from the current offset"""
value = 0
while intsize > 0:
value = (value<<8) + ord(data[offset])
offset = offset + 1
intsize = intsize - 1
@mtigas
mtigas / premature_optimization_middleware.py
Created April 28, 2011 20:50
Django SpacelessHtmlResponse
from django.utils.html import strip_spaces_between_tags
class SpacelessHtmlResponse(object):
"""
Equivalent to running {% spaceless %} template tag against a full HTML response.
"""
def process_response(self, request, response):
if response.get("Content-Type", "").startswith("text/html"):
response.content = strip_spaces_between_tags(response.content.strip())
return response
@mtigas
mtigas / gist:959392
Created May 6, 2011 17:34
Aliases to help test a local or LAN server with "real world" internet connection speeds.
# Add to .bash_profile:
# Adds `slow_net` and `slow_net_off` commands to your terminal.
# * limits to 150KBps ~~ 1.2 Megabit internet
# * adds 200ms latency
alias slow_net="sudo ipfw pipe 1 config bw 150KByte/s delay 200ms && sudo ipfw add 1 pipe 1 src-port 80"
alias slow_net_off="sudo ipfw delete 1"
# Check out the base Django trunk into "django" directory.
svn co http://code.djangoproject.com/svn/django/trunk/ django
# ... update it to 1.2.5
svn switch http://code.djangoproject.com/svn/django/tags/releases/1.2.5/ django
# ... update to 1.3
svn switch http://code.djangoproject.com/svn/django/tags/releases/1.3/ django
@mtigas
mtigas / gist:976939
Created May 17, 2011 17:39
Census Fields (May 2011 release)
Under 5 years,under_5_yrs,dpsf0010002
5 to 9 years,5_to_9_yrs,dpsf0010003
10 to 14 yrs,10_to_14_yrs,dpsf0010004
15 to 19 yrs,15_to_19_yrs,dpsf0010005
20 to 24 yrs,20_to_24_yrs,dpsf0010006
25 to 29 yrs,25_to_29_yrs,dpsf0010007
30 to 34 yrs,30_to_34_yrs,dpsf0010008
35 to 39 yrs,35_to_39_yrs,dpsf0010009
40 to 44 yrs,40_to_44_yrs,dpsf0010010
45 to 49 yrs,45_to_49_yrs,dpsf0010011
class A(object):
def __init__(self):
print "A init"
class B(A):
def __init__(self):
print "B init"
super(B, self).__init__()
class C(B):