Skip to content

Instantly share code, notes, and snippets.

@cuppster
cuppster / comics.py
Created January 30, 2013 05:50
Mini-framework for scraping web pages with python using PyQuery, decorators and generators.
class NewComics:
def __init__(self):
self.pages = None
@scrape.route('/newreleases')
def weekly(self, url, d):
# save the total number of pages
if self.pages is None:
@cuppster
cuppster / leo-backbone.js
Created January 27, 2013 01:08
Backbone.js client for Leonardo API
// ## global namespace
//
var Leo = {}
// ## Leonardo Options
//
Leo.options = (function() {
return {
api : 'http://dev.leonar.do/api',
@cuppster
cuppster / quantize.py
Created November 2, 2012 23:08
Quantize An Image To A Custom Palette
import sys
import numpy as np
import scipy.ndimage as nd
from scipy.cluster.vq import vq
from scipy.misc import imsave
def crayola_9():
"""
Palette of the first 8 crayola colors + white
"""
@cuppster
cuppster / 1.cs
Created September 3, 2012 18:39
Promises for C# using Generics
/*
modified from original source: https://bitbucket.org/mattkotsenas/c-promises/overview
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Promises
@cuppster
cuppster / 1.js
Created August 29, 2012 21:06
Updating Views with Events, Models and Rivets
// ## Header View
//
var HeaderView = Backbone.View.extend({
// model backing the view
model: new (Backbone.Model.extend({
defaults: {
hasUpdate: false,
}
})),
@cuppster
cuppster / 1.js
Created August 23, 2012 15:45
Anonymous backbone.js view models
// ## Site Navigation View
//
var SiteNavView = Backbone.View.extend({
model: new (Backbone.Model.extend({
defaults: {
hasNew: undefined,
}
})),
@cuppster
cuppster / 1.js
Created August 21, 2012 23:34
Shared backbone.js Collections
/**
* Shared Collections for backbone.js
*/
// shared collection
// useful when the user may enter the site in multiple routes/views,
// but use the same collection
var SharedCollection = function(collection) {
var obj = this;
@cuppster
cuppster / 1.py
Created August 15, 2012 23:28
Simple String Generator in Python
import itertools
def string_patterns(pattern, *args):
'''generator for string patterns'''
for data in itertools.product(*args):
s = pattern.format(*data)
yield s
# generate some urls!
@cuppster
cuppster / 1.js
Created August 15, 2012 22:13
Routed Commands for Backbone.js
// the application
//
var AppView = Backbone.View.extend({
el: $('body'),
initialize: function() {
var view = this;
// command bar
@cuppster
cuppster / node-express-cors-middleware.js
Created April 9, 2012 16:02
express.js middleware to support CORS pre-flight requests
app.use(express.methodOverride());
// ## CORS middleware
//
// see: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');