Skip to content

Instantly share code, notes, and snippets.

require.extensions[".json"] = function (module, filename) {
module.exports = JSON.parse(require("fs").readFileSync(filename, "utf8"))
}
@sspencer
sspencer / count.py
Created March 21, 2011 18:28
Count items without conditional
t = ["one", "two", "one", "four", "one", "two", "three"]
hist = {}
for x in t:
hist[x] = hist.get(x, 0) + 1
@sspencer
sspencer / substitute.py
Created April 5, 2011 20:31
Simple template replacement function
import re
def make_replacer(d):
"bind dictionary to replacement function"
return lambda m: d.get(m.group(1), m.group(0))
def subs(template, obj):
"substitute values from obj into ${vars} in template"
repl = make_replacer(obj)
return re.sub("""\$\{(\w+)\}""", repl, template)
@sspencer
sspencer / multiproxy-working.js
Created April 13, 2011 21:54
Working Multi Proxy
var http = require('http');
// Working multiproxy - can call setTimeout multiple times.
function sendRequest(hostname) {
var client = http.createClient(80, hostname);
client.setTimeout(5000);
var req = client.request('GET', '/');
client.addListener('timeout', function (error) {
@sspencer
sspencer / multiproxy.js
Created April 13, 2011 20:57
send multiple requests
var http = require("http");
// Broken multiproxy - req.socket returns undefined for second hostname when
// it's equal to first hostname.
function sendRequest(hostname) {
var options = {
host: hostname,
port: 80,
path: "/"
};
@sspencer
sspencer / rgb.c
Created July 20, 2011 17:39
RGB Conversion
int rgb = ((r&0x0ff)<<16)|((g&0x0ff)<<8)|(b&0x0ff);
int red = (rgb>>16)&0x0ff;
int green=(rgb>>8) &0x0ff;
int blue= (rgb) &0x0ff;
@sspencer
sspencer / AFNetworkingRequest.m
Created November 18, 2011 23:58
AFNetworking Request
// using ARC, so no release / autorelease
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://localhost"]];
[client setAuthorizationHeaderWithUsername:@"steve" password:@"mypass"];
NSURLRequest *request = [client requestWithMethod:@"GET" path:@"/auth.php" parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation
setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success: %@", operation.responseString);
@sspencer
sspencer / StarOfDavid.py
Created January 26, 2012 07:43
Make each line in star of david add up to 26
import random
import sys
"""
Given the Star of David, arrange the numbers 1-12 (there are 12 intersections) so that
each line (there are 6 lines and 4 intersections per line) adds up to 26.
Intersections on array are numbered TDLR, from a0 to a11.
"""
@sspencer
sspencer / Star6.c
Created January 26, 2012 08:55
Find Star of David 26 Solutions
// Given the Star of David, arrange the numbers 1-12 (there are 12 intersections) so that
// each line (there are 6 lines and 4 intersections per line) adds up to 26.
//
// Intersections on array are numbered TDLR, from a0 to a11.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// prototypes
@sspencer
sspencer / keep_current_file_open.sh
Created February 9, 2012 23:08 — forked from wearhere/keep_current_file_open.sh
Keep your current source file open in Xcode after a run completes (a.k.a don't die in main.m)
#! /bin/sh
# On alternate invocations, this script
# saves the path of the source file currently open in Xcode
# and restores the file at that path in Xcode.
#
# By setting Xcode (in Behaviors) to run this script when "Run Starts"
# and when "Run Completes", you can prevent it from switching to main.m
# when a run finishes.
# See http://stackoverflow.com/questions/7682277/xcode-4-2-jumps-to-main-m-every-time-after-stopping-simulator