Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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
require.extensions[".json"] = function (module, filename) {
module.exports = JSON.parse(require("fs").readFileSync(filename, "utf8"))
}
@sspencer
sspencer / transparent-gif.js
Created October 31, 2010 22:27
Serve a transparent GIF from NodeJS
// Two ways to serve transparent GIF
var buf = new Buffer([
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00,
0x80, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x2c,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
0x02, 0x44, 0x01, 0x00, 0x3b]);
res.send(buf, { 'Content-Type': 'image/gif' }, 200);
@sspencer
sspencer / gist:399025
Created May 12, 2010 19:22
NodeJS BrowserPlus Service Sketch
// A rough sketch of how a NodeJS service might look
// Take 2 - keep services flat.
service = require('browserplus').service;
log = require('browserplus').log;
service.name = "HelloWorld";
service.version = "1.0.1";
service.doc = "A hello world service for BrowserPlus";
I'm Lloyd Hilaiel from BrowserPlus.
[show the yahoo cow, moo]
This screencast explores the question, *how do we make uploading from the Browser, better?*
[textual question showing on screen]
What does better mean?
I'm Lloyd Hilaiel from BrowserPlus. `[show the yahoo cow, moo]`
This screencast explores the question, *how do we make uploading from the Browser, better?* `[textual question
showing on screen]`
What does better mean?
* Better means *easier* for the user to select the content they wish to upload `[easier floats in lower left
portion of screen]`
* Better also means the selected content should transfer *faster*. `[faster floats in lower
# Ruby Range Check with Default Value
# if args['level'] is from (-1..9) return args['level], otherwise return -1
level = (-1..9).include?(args['level']) ? args['level'] : -1