Skip to content

Instantly share code, notes, and snippets.

View weaver's full-sized avatar

Ben Weaver weaver

View GitHub Profile
@weaver
weaver / grabme.py
Created October 15, 2010 17:53
Download some grab.by images into the current directory.
#!/usr/bin/env python
## Download some grab.by images into the current directory.
## Requires curl command-line utility.
##
## The bounds should be given in base62 (e.g. in the range 0 - ZZZZ).
##
## Example:
##
## mkdir /tmp/grab
@weaver
weaver / make-selector.js
Created October 14, 2010 18:54
Create an absolute jQuery selector for a DOM element.
// Create an absolute jQuery selector for a DOM element.
//
// + el - Element to select.
//
// Returns String selector.
function makeSelector(el) {
var tag, index, stack = [];
for (; el.parentNode; el = el.parentNode) {
tag = el.tagName;
@weaver
weaver / password-test.js
Created August 10, 2010 20:52
Hash passwords for storage. #nodejs
var assert = require('assert'),
sys = require('sys'),
pwd = require('password'),
vows = require('vows');
vows.describe('Password').addBatch({
'The default method': method(pwd),
'The bcrypt method': method(pwd.bcrypt),
'The sha512 method': method(pwd.sha512)
}).export(module);
@weaver
weaver / amap.js
Created August 10, 2010 02:59
Asynchronous map #nodejs
// amap -- asynchronous map
//
// Maps fn over list and passes the result to callback. For example,
//
// function loadEntries(folder, callback) {
// fs.readdir(folder, function(err, files) {
// if (err) throw err;
// amap(files, callback, function(name, index, next) {
// load(path.join(folder, name), next);
// });
@weaver
weaver / form.js
Created August 9, 2010 23:24
Express form validation #nodejs
//// form -- form validation
///
/// Represent the structure of a form and use it to validate form
/// data. Here's a Form that describes a create-account form:
///
/// var SignupForm = form.Form('Sign Up')
/// .text('account', 'Account Name', form.required(/[\w-\.]+/))
/// .email('email', 'Email', form.required())
/// .password('password', 'Password', form.required())
/// .password('confirm', 'Confirm', [form.required(), form.equal('password')]);
@weaver
weaver / viewMiddleware.js
Created August 9, 2010 23:06
Express middleware for adding template bindings #nodejs
// This is rudimentary view-level middleware for Express. When
// ServerResponse.render() is called, any middleware methods are
// invoked in the order they were registered. They are passed the
// request, response, and local template bindings.
//
// For example:
//
// var connect = require('connect'),
// app = require('express').createServer();
//
@weaver
weaver / setup.js
Created August 4, 2010 15:33
Manage library and external dependency folders in your Node.JS project.
//// setup.js -- add packages to require.paths
///
/// Manage library folders and external dependency folders in your
/// project by including setup.js in your project. Call it from the
/// beginning of your project's entry-point to adjust require.paths.
///
/// For example, if you're making an application structured like this:
///
/// README
/// app.js # main program
@weaver
weaver / demo-client.py
Created March 1, 2010 16:20
XMPP client/server that use JSON info queries.
#!/usr/bin/env python
"""demo-client.py -- query the demo server
Example:
## In one terminal:
> python demo-server.py
## In another terminal:
> python demo-client.py '*'
@weaver
weaver / yaml-omap.py
Created February 28, 2010 03:44
Use Python OrderedDict as YAML omap
import yaml
### OrderedDict from <http://pypi.python.org/pypi/ordereddict/1.1>
# Copyright (c) 2009 Raymond Hettinger
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
@weaver
weaver / tornado-webdav.py
Created February 26, 2010 20:06
Run a wsgidav WebDAV server through Tornado
#!/usr/bin/env python
"""tornado-webdav.py -- run a wsgidav server through tornado
This script requires tornado commit id
1ae186a504224e9f6cf5375b56f8e26e4774e2a0. The easiest way to get this
patch is to clone the git repository and install using setup.py:
git clone git://github.com/facebook/tornado.git
cd tornado