Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
thomasballinger / gist:3836614
Created October 4, 2012 21:36
wsgi example
def myapp(environ):
print environ
query_string = environ['QUERY_STRING']
return '<html><body>hello<body></html>'
def hello_world_app(environ, start_response):
status = '200 OK' # HTTP Status
@thomasballinger
thomasballinger / gist:3859927
Created October 9, 2012 16:36
my annouce function
def announce(self, torrent):
"""Returns data from Tracker specified in torrent"""
announce_query_params = {
'info_hash' : torrent.info_hash,
'peer_id' : self.client_id,
'port' : self.port,
'uploaded' : 0,
'downloaded' : 0,
'left' : torrent.length,
'compact' : 1, # sometimes optional
@thomasballinger
thomasballinger / gist:3947297
Created October 24, 2012 16:52
dynamic att lookup in Python
class Other(object):
def Task(self, x):
print 'called task'
class Foo(object):
def __init__(self):
self.tom = 12
self.stuff = []
@property
@thomasballinger
thomasballinger / gist:4262261
Created December 11, 2012 21:26
Javascript Closures
// Closures are sometimes unintuitive!
console.log('the smart way');
funs = []
for (var a = 0; a < 3; a++){
funs.push(function(){
var newa = a;
return function(){console.log(newa);}
}())
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#define MAX_CANDIDATES 25000
#define TWOTOTHE32 4294967296
int64_t random64bit(){
// we need 64 bits of random data
@thomasballinger
thomasballinger / hacker school name game
Created February 13, 2013 23:17
Paste in a bookmarklet after compressing with something like http://4umi.com/web/bookmarklet/edit.php
javascript: (function () {
if (document.timesClicked == undefined) {
document.timesClicked = 0;
};
document.timesClicked++;
var names = document.getElementsByClassName('name');
for (var i = 0; i < names.length; i = i + 1) {
if (!names[i].savedName) {
names[i].savedName = names[i].innerHTML;
}
var data = $('.message_row, .recipient_row').map(function(i, x){
return {
name : jQuery.makeArray($(x).find('.sender_name').map(function(i, x){return x.innerText})),
message : jQuery.makeArray($(x).find('.message_content').map(function(i, x){return x.innerText})),
time : jQuery.makeArray($(x).find('.message_time').map(function(i, x){return x.innerText})),
stream : jQuery.makeArray($(x).find('.stream_labels').map(function(i, x){return x.innerText})),
subject : jQuery.makeArray($(x).find('.narrows_by_subject').map(function(i, x){return x.innerText}))
};
})
var s = JSON.stringify(jQuery.makeArray(data))
@thomasballinger
thomasballinger / bettersay.py
Last active December 14, 2015 01:58
A say that follows better unix conventions!
#!/usr/bin/env python
from subprocess import Popen
import sys
def say_blocking(msg):
p = Popen(['say', msg])
p.communicate()
while True:
line = sys.stdin.readline()
@thomasballinger
thomasballinger / .gitconfig
Created February 28, 2013 19:12
except of ~/.gitconfig
[alias]
glog = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
co = checkout
unstage = reset HEAD
[core]
pager = less -FRSX
editor = vim
[color]
ui = auto
status = auto
@thomasballinger
thomasballinger / boggle_transitions.py
Created March 1, 2013 19:43
boggle state transitions for an n x n board I acknowledge it's a bad idea in the first place, but should I indent this?
def get_transitions(n):
"""Return a dictionary of array spots to neighbors for an nxn grid"""
return {initial_row*n + initial_col :
{row * n + col
for row in range(max(0, initial_row-1), min(n, initial_row+2))
for col in range(max(0, initial_col-1), min(n, initial_col+2))
if (row, col) != (initial_row, initial_col)}
for initial_row in range(n)
for initial_col in range(n)}