Skip to content

Instantly share code, notes, and snippets.

@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 / __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 / 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 / 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 / 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 / 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 / framecounter.js
Created December 7, 2010 07:08
My ghetto frame counter (for tracking fps), now replaced with mrdoob's Stats.js
function FrameCounter(sample_length) {
this.sample_length = sample_length || 500; // In milliseconds
this.n = 0;
this.last_n = 0;
this.time_created = this.time_sampled = this.time_updated = new Date();
this.last_rate = 0;
}
FrameCounter.prototype = {
tick: function() {
this.n++;
@shazow
shazow / pytron.py
Created December 5, 2010 00:12
My Tron-like game from 2006.
'''
pytron - Tron clone written in Python with PyGame
By Andrey Petrov
Thanks to:
Igor Foox, for debugging (v0.3)
Changelog:
2006-11-16 Version 0.3
@shazow
shazow / sqlalchemy_expando.py
Created November 29, 2010 06:10
AppEngine Expando model implemented in SQLAlchemy
"""
Usage:
class MyTable(ExpandoModel):
__tablename__ = 'my_table'
id = Column(types.Integer, primary_key=True)
>>> t = MyTable(id=1)
>>> Session.add(t)
>>> Session.commit()
@shazow
shazow / .htaccess
Created November 22, 2010 05:40
Redirecting Wordpress blog post links to Posterous blog post links
# Redirect shazow.net/blog -> blog.shazow.net
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
# Keep serving static files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d