Skip to content

Instantly share code, notes, and snippets.

@shazow
shazow / array_benchmark.js
Created December 11, 2010 03:26
Benchmarking Javascript array creation, write, read.
/*
Oddly, the results are not very intuitive. Seems grid_2d_array is fastest in all benchmarks,
though I was expecting grid_1d_array or at least grid_2d_lazy_array to win
(or maybe even grid_canvas to be surprising, but I was disappointed).
Results with size=[1920,1200] (on Chrome, though Firefox is similar):
--- grid_canvas (2d canvas treated as small 2d array int storage structure) ---
create: 1ms
@shazow
shazow / binary_search.js
Created December 23, 2010 07:03
Prematurely optimized binary search functions in Javascript.
var binary_search = function(a, val, compare_fn) {
// Returns (positive) index of element if found (not necessarily the first),
// otherwise (negative) negated index of insertion point.
var left = 0, right = a.length;
// Tight loop optimization
if(compare_fn) {
while(left < right) {
var middle = (left + right) >> 1;
@shazow
shazow / task.py
Created January 4, 2011 01:08
SQLAlchemy-based task manager (like Celery but standalone)
class Task(BaseModel):
__tablename__ = 'task'
id = Column(types.Integer, primary_key=True)
time_created = Column(types.DateTime, default=datetime.now, nullable=False)
time_updated = Column(types.DateTime, onupdate=datetime.now)
time_wait_until = Column(types.DateTime, default=datetime.now, nullable=False)
seconds_elapsed = Column(types.Float)
@shazow
shazow / benchmark_generators.py
Created January 13, 2011 06:15
Generators versus iterators.
def a(n):
for i in xrange(n):
yield i
def b(n):
r = []
for i in xrange(n):
r.append(i)
return r
@shazow
shazow / __init__.py
Created January 16, 2011 02:36
Slightly-modified Pylons project unit tests root module.
"""Pylons application test package
This package assumes the Pylons environment is already loaded, such as
when this script is imported from the `nosetests --with-pylons=test.ini`
command.
This module initializes the application via ``websetup`` (`paster
setup-app`) and provides the base testing objects.
"""
from unittest import TestCase
@shazow
shazow / gist:789309
Created January 21, 2011 06:08
SQLAlchemy declarative base with __export__ and __import__ useful for exporting and importing fixtures.
"""SQLAlchemy Metadata and Session object"""
from sqlalchemy import MetaData, types
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime, date
__all__ = ['Session', 'metadata', 'BaseModel']
Session = scoped_session(sessionmaker(expire_on_commit=False))
@shazow
shazow / app_globals.py
Created January 23, 2011 22:50
My TurboMail integration with Pylons
"""The application's Globals object"""
from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options
from myproject.lib.email import TurboMailer
class Globals(object):
"""Globals acts as a container for objects available throughout the
life of the application
@shazow
shazow / util.js
Created January 31, 2011 18:22
JavaScript utility functions I wrote and use often.
Function.prototype.bind = function(bind) {
var self = this;
return function () {
var args = Array.prototype.slice.call(arguments);
return self.apply(bind || null, args);
};
};
/** Geometry and vectors **/
@shazow
shazow / gist:847665
Created February 28, 2011 17:26
Pylons environment setup outside of paste. Useful for background tasks (like using turnip)
def setup(config_file='development.ini'):
from socialgrapple.config.environment import load_environment
from paste.deploy import loadapp
from routes.util import URLGenerator
from webtest import TestApp
from beaker.session import SessionObject
environ = {'HTTP_HOST': 'socialgrapple.com'}
wsgiapp = loadapp('config:' + config_file, relative_to='.')
config = load_environment(wsgiapp.config['global_conf'], wsgiapp.config['app_conf'])
@shazow
shazow / twolever.py
Created March 20, 2011 18:22
Print all the people you're following on Twitter, for convenient grepping.
#!/usr/bin/env python
# Print all the people you're following on Twitter.
import webbrowser
import tweepy
import sys
def main():
# Twolever app
consumer_key, consumer_secret = 'd8BVP2w58kwnVxVT3BrrPQ', 'Wuelj9vieJgzpnfqGTxptYBqN9BT3SYHxQnRzeyBbxg'