Skip to content

Instantly share code, notes, and snippets.

@jwheare
jwheare / gist:68965
Created February 23, 2009 14:27
Big fucking hack that you shouldn't use ever.
function list (vars, context) {
if (typeof(context) === 'undefined') {
context = window;
}
return function (item, i) {
context[vars[i]] = item;
};
}
[1,2,3].each(list(['foo','bar','baz']))
@jwheare
jwheare / array_to_csv_line.php
Created June 23, 2009 17:45
php array to csv line
<?php
// Encode an array to a CSV delimited row using PHP's memory output buffer
// http://php.net/manual/en/wrappers.php.php
function array_to_csv_line($data_array) {
$temp_mem = fopen("php://temp", 'r+');
fputcsv($temp_mem, $data_array);
rewind($temp_mem);
$data_csv = trim(stream_get_contents($temp_mem));
return $data_csv;
"""
Given a list of scores, returns the index of one of those items at random,
biased towards the items with the highest scores.
e.g. random_index_with_bias([84, 62, 61, 67, 45])
To show it works, run it 1,000,000 times:
>>> scores = {}
>>> for i in range(1000000):
@jwheare
jwheare / fetch.py
Created April 24, 2010 19:56
Fetch all a user's Last.fm scrobbles by paging through their recent tracks
#!/usr/bin/env python
"""
Fetch all a user's Last.fm scrobbles by paging through their recent tracks
Usage: ./fetch.py <username> [<start_page> [<end_page>]]
Be aware: You may end up with duplicated data if the user is scrobbling
when you fetch for tracks. Make sure you check for dupes when you process
the XML later
"""
@jwheare
jwheare / tweetgeocode.js
Created April 29, 2010 14:38
Get a lat long from a tweet, by first checking twitter geotags, then foursquare links
var d = document,
w = window,
k = d.getSelection,
x = d.selection,
ws = w.getSelection,
s = (ws ? ws() : (k) ? k() : (x ? x.createRange().text : 0)),
l = d.location,
e = encodeURIComponent;
var tweetId = /\d+$/.exec(l)[0];
@jwheare
jwheare / sepia.php
Created February 1, 2011 11:52
Convert an image to faux sepia tone.
<?php
@$data = file_get_contents($_GET['image']);
if (!$data) {
header("HTTP/1.1 400 Bad Request");
exit;
}
@$img = imagecreatefromstring($data);
if (!$img) {
header("HTTP/1.1 400 Bad Request");
@jwheare
jwheare / clicolor.sh
Last active September 25, 2015 01:27
Useful shortcuts/colorisation for git
# Not really git related, this is for ls colors, put in e.g. ~/.bash_login
export CLICOLOR="true"
export LSCOLORS="DxfxexdxCxegedabagacad"
@jwheare
jwheare / ihatecircles.user.js
Created November 27, 2011 17:55
Turn off circles on This Is My Jam.
// ==UserScript==
// User script to turn off the "I love circles!!!11!" option on This Is My Jam by default.
// Click raw to install in Chrome
// Should work with Greasemonkey and the like in other browsers
// @match http://thisismyjam.com/jam/meta
// @match http://www.thisismyjam.com/jam/meta
// ==/UserScript==
document.getElementById('circle').click()
@jwheare
jwheare / gpu.sh
Created January 13, 2012 18:29
git pull shuffle
gpu() {
stashlist="$(git stash list)"
if [ "$stashlist" ]; then
echo "Already stashed changes"
echo $stashlist
return 1;
fi
git stash && git pull && git stash pop;
}