Skip to content

Instantly share code, notes, and snippets.

@perrygeo
perrygeo / jsonp_callback.patch
Created February 13, 2012 23:38
tilestache dynamic callback patch
From 8a0bcc6425e0e1865f09a10faec3a224d91a8ccc Mon Sep 17 00:00:00 2001
From: Matthew Perry <mperry@ecotrust.org>
Date: Mon, 13 Feb 2012 15:28:06 -0800
Subject: [PATCH] Allow dynamic callbacks for jsonp
---
TileStache/__init__.py | 12 +++++++++++-
1 files changed, 11 insertions(+), 1 deletions(-)
diff --git a/TileStache/__init__.py b/TileStache/__init__.py
@perrygeo
perrygeo / gist:1903033
Created February 24, 2012 19:09
Sed scripts to fix blank lines and whitespace; helps code comply with PEP8
# Removes whitespace chars from blank lines
pep8 -r --select=W293 -q --filename=*.py /usr/local/src/madrona/madrona/ | xargs sed -i 's/^[ \r\t]*$//'
# Removes trailing blank lines from files
pep8 -r --select=W391 -q --filename=*.py /usr/local/src/madrona/madrona/ | xargs sed -i -e :a -e '/^\n*$/{$d;N;ba' -e '}'
# Squashes consecutive blanks lines into one
pep8 -r --select=E303 -q --filename=*.py /usr/local/src/madrona/madrona/ | xargs sed -i '/./,/^$/!d'
# confirm
@perrygeo
perrygeo / gist:2227998
Created March 28, 2012 16:30
Bash one-liners
# Find the ten largest disk hogs in the current directory (with human-readable sizes)
for X in $(du -s * 2>/dev/null| sort -nr | cut -f 2); do du -hs $X 2>/dev/null; done | head -n 10
@perrygeo
perrygeo / gist:2279331
Created April 1, 2012 23:10
pep8 tricks
pep8 --statistics -qq --filename=*.py /usr/local/src/madrona/madrona/
pep8 --select=E202,E201 --repeat . > ~/pep8.txt
vim -q ~/pep8.txt
# :map z :cn
# type z to cycle through all instances; puts your cursor right on the spot
@perrygeo
perrygeo / gist:2386206
Created April 14, 2012 17:48
K-D tree nearest neighbor search in n-dimensions
import numpy as np
from scipy.spatial import KDTree
def run_kdtree(ndim=8,npnts=500000):
"""
given a set of random n-dimensional points,
find the closest one to a given point
"""
print "Creating %d %d-dimensional points" % (npnts, ndim)
@perrygeo
perrygeo / reddit_wallpaper.py
Created April 15, 2012 00:56
reddit_wallpaper: Gnome wallpaper switcher
#!/usr/bin/python
#reddit wallpaper rotate #Scialex
import json, gconf, syslog, os, random
from urllib2 import urlopen
SAVE_LOCATION = os.path.join(os.path.expanduser('~'), '.background_getter')
GCONF_KEY = '/desktop/gnome/background/picture_filename'
PICTURE_ENDINGS=['png','jpg','gif']
JSON_PAGE = 'http://www.reddit.com/r/earthporn.json' # '+' seperated
IMGUR_JSON_FORMAT = "http://api.imgur.com/2/image/{0}.json"
@perrygeo
perrygeo / gist:2407506
Created April 17, 2012 17:03
cache arbitrary functions in django
from django.core.cache import cache as _djcache
def cache_function(seconds=6000):
"""
Cache the result of a function call for the specified number of seconds,
using Django's caching mechanism.
Assumes that the function never returns None (as the cache returns None to indicate a miss),
and that the function's result only depends on its parameters.
Note that the ordering of parameters is important.
e.g. myFunction(x = 1, y = 2), myFunction(y = 2, x = 1), and myFunction(1,2) will each be cached separately.
@perrygeo
perrygeo / gist:2410616
Created April 18, 2012 02:14
Find lowest version of python required by codebase
# See http://stackoverflow.com/questions/804538/tool-to-determine-what-lowest-version-of-python-required
# https://github.com/ghewgill/pyqver
wget https://raw.github.com/ghewgill/pyqver/master/pyqver2.py
find /path/to/project/ -name "*.py" | xargs python pyqver2.py | sort > versions.txt
tail versions.txt
# last line will show you the minimum version required
@perrygeo
perrygeo / gist:2425260
Created April 20, 2012 01:53
Use R to summarize GoldenCheetah metrics database
library(RSQLite)
con <- dbConnect("SQLite", dbname = "/Users/perry/Library/GoldenCheetah/MattPerry/metricDB")
d <-dbGetQuery(con, "select * from metrics")
# Correlate heartrate and time riding with "bike score"
cor((d$Xaverage_hr / LTHR) ^2 * (d$Xtime_riding/3600 * 100), d$Xskiba_bike_score)
@perrygeo
perrygeo / gist:2431997
Created April 20, 2012 21:26
Remove dos line endings recursively
# ^M is entered using Ctrl-V Ctrl-M
grep -IUrl "^M" sourcecode/* | xargs dos2unix