Skip to content

Instantly share code, notes, and snippets.

//Location: 1 happy: 65401 sad: 33
//Location: 2 happy: 406 sad: 139
//Location: 3 happy: 65386 sad: 7
putHappinessScore({location: 3, happiness: 65386, unhappiness: 7});
@swinton
swinton / MercatorMap.java
Created October 13, 2011 13:22
MercatorMap.java | Utility class to convert between geo-locations and Cartesian screen coordinates, via tillnagel.com
/**
* Utility class to convert between geo-locations and Cartesian screen coordinates.
* Can be used with a bounding box defining the map section.
*
* (c) 2011 Till Nagel, tillnagel.com
*/
public class MercatorMap {
public static final float DEFAULT_TOP_LATITUDE = 80;
public static final float DEFAULT_BOTTOM_LATITUDE = -80;
@swinton
swinton / bookmarklet.js
Created November 2, 2011 14:02
bookmarklet.js
javascript:function%20iprl5(){var%20d=document,z=d.createElement('scr'+'ipt'),b=d.body,l=d.location;try{if(!b)throw(0);d.title='(Saving...)%20'+d.title;z.setAttribute('src',l.protocol+'//www.instapaper.com/j/naC26SvnD0ky?u='+encodeURIComponent(l.href)+'&t='+(new%20Date().getTime()));b.appendChild(z);}catch(e){alert('Please%20wait%20until%20the%20page%20has%20loaded.');}}iprl5();void(0)
# Klout scores
klouts <- read.csv("klout-scores.csv", header=TRUE)
# Radiuses for rendering bubbles
radius <- sqrt( klouts$klout_score / pi )
# Competitor matrix
competitors <- read.csv("competitors.csv", header=TRUE)
# Co-ords for plotting competitors
@swinton
swinton / .bash_profile
Created January 3, 2012 13:54
Bash profile
# See: http://www.ibm.com/developerworks/linux/library/l-tip-prompt/
export PS1="\[\e[35;1m\]\w \[\e[36;1m\]\u\[\e[32;1m\]$ \[\e[0m\]"
@swinton
swinton / upstart_virtualenv_test.conf
Created March 5, 2012 15:44
Example of an upstart job running a python script under a virtualenv
########################################
##### upstart_virtualenv_test.conf #####
##### install in /etc/init #####
########################################
description "Testing virtualenv and upstart setup"
env PYTHON_HOME=/home/steve/.virtualenvs/upstart-test
start on runlevel [2345]
@swinton
swinton / distance_on_unit_sphere.py
Created March 22, 2012 14:28
Computing the distance between two locations on Earth from coordinates
#!/usr/bin/env python
"""
Computing the distance between two locations on Earth from coordinates
The following code returns the distance between to locations based on each point's longitude and latitude. The distance returned is relative to Earth's radius. To get the distance in miles, multiply by 3960. To get the distance in kilometers, multiply by 6373.
Latitude is measured in degrees north of the equator; southern locations have negative latitude. Similarly, longitude is measured in degrees east of the Prime Meridian. A location 10° west of the Prime Meridian, for example, could be expressed as either 350° east or as -10° east.
via http://www.johndcook.com/python_longitude_latitude.html
@swinton
swinton / join_csv.py
Created March 27, 2012 10:46
Joins a bunch of CSVs, sends the single joined CSV to STDOUT
#!/usr/bin/env python
"""
Joins a bunch of CSVs, sends the single joined CSV to STDOUT
"""
import codecs, csv, sys
try:
import cStringIO as StringIO
@swinton
swinton / mapreduce.js
Created March 29, 2012 14:58
MongoDB Map Reduce Example
var source = "STRESS";
var target = "stress_locations";
var map = function () {
if (this.tweet.user.location.trim()) {
emit(this.tweet.user.location, 1);
}
};
var reduce = function(key, values) {
@swinton
swinton / htmlescape.py
Created June 21, 2012 20:13
Convert special characters in text to HTML entities.
#!/usr/bin/env python
"""
Convert special characters in text to HTML entities.
NOW WITH ADDED UNICODE SUPPORT!
Usage:
$ echo "<b>foo <i>bar</i></b>" | htmlescape.py
"""