Skip to content

Instantly share code, notes, and snippets.

@shazow
shazow / gist:513526
Created August 8, 2010 03:10
Helpers for manipulating colors in javascript.
/* Helpers for manipulating colors in javascript. */
/* IE patch. :( */
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
/***/
@shazow
shazow / mytypes.py
Created September 24, 2010 04:13
SQLAlchemy Enum type based on Integer indices, for better storage efficiency over the default Enum type.
"""
SQLAlchemy Enum type based on Integer indices.
"""
from sqlalchemy import types
class Enum(types.TypeDecorator):
impl = types.Integer
def __init__(self, value_map, strict=True, *args, **kw):
"""Emulate Enum type with integer-based indexing.
@shazow
shazow / util.py
Created September 29, 2010 19:28
Python utility functions I wrote and use often. See https://github.com/shazow/unstdlib.py for more.
"""
Handy Python functions written by Andrey Petrov (shazow). I release this code
under public domain. (Attribution is optional but appreciated.)
Approximate changelog (according to https://gist.github.com/603374):
2011-05-05
- I made a proper github repo out of this:
https://github.com/shazow/unstdlib.py
@shazow
shazow / benchmark.py
Created October 2, 2010 22:55
SQLAlchemy types benchmark
from test import model
Session = model.Session
import time
from decorator import decorator
def measure(description):
def wrapper(fn, *args, **kw):
print "{0}...".format(description),
@shazow
shazow / gist:612814
Created October 6, 2010 04:13
Examples where lack of semicolons break Javascript code
// Examples where lack of semicolons break things
// Example 1:
function a() { return "a" }
function b() { return "b" }
var foo = [1,2,3]; // <--- remove this semicolon to break things
-1 == a() || b();
@shazow
shazow / filenose.py
Created October 31, 2010 17:45
Didn't know about PhotoRec, so I wrote my own jpeg data recovery script in April 2008.
#!/bin/env python
# filenose - Find specific types of files from raw data.
from StringIO import StringIO
import sys
import time
from datetime import datetime
MAXSIZE = 1024*1024*4 # 4mb
CHUNK_SIZE = 1024*1024*10 # 10mb
@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
@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 / 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 / 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++;