Skip to content

Instantly share code, notes, and snippets.

View omaraboumrad's full-sized avatar

Omar Abou Mrad omaraboumrad

View GitHub Profile
@omaraboumrad
omaraboumrad / middleware.py
Created December 20, 2012 08:23
Django middleware
class FooMiddleware(object):
def process_request(self, request):
pass
# in settings.py
MIDDLEWARE_CLASSES = (
# ...
'myapp.foo.middleware.FooMiddleware',
)
@omaraboumrad
omaraboumrad / test.html
Created March 12, 2013 12:00
python tornadoweb websocket sample
<html>
<head>
<title>Foo</title>
<script type="text/javascript">
var ws = new WebSocket("ws://localhost:8888/ws");
ws.onopen = function(){
ws.send("sup?");
};
@omaraboumrad
omaraboumrad / test.py
Created March 19, 2013 11:49
sqlite table creation speed.
# Slow: python test.py
# Fast: python test.py -p /path/to/ramdisk
import os
import sqlite3
# Helper
from optparse import OptionParser
options = OptionParser()
options.add_option("-p", "--prefix", dest="prefix", default='')
@omaraboumrad
omaraboumrad / ramdisk
Created March 19, 2013 15:03
Creating a ramdisk (tmpfs)
sudo mkdir /tmp/ramdisk
sudo chmod 777 /tmp/ramdisk
sudo mount -t tmpfs -o size=256M tmpfs /tmp/ramdisk/
@omaraboumrad
omaraboumrad / node.py
Last active December 22, 2015 04:18
simple bidirectional tree
class NodeList(list):
def __init__(self, root, items):
self.root = root
super(NodeList, self).__init__(items)
def append(self, item):
item._parent = self.root
super(NodeList, self).append(item)
class Node(object):
@omaraboumrad
omaraboumrad / bootstrap.py
Created September 3, 2013 22:44
sily little vagrant bootstrapper
#! /usr/bin/python
import os
def cmd(txt):
print '>>> {} <<<'.format(txt)
return os.system(txt) >> 8
def test(what):
return cmd(what) == 0
@omaraboumrad
omaraboumrad / sprite_sheet.py
Last active December 22, 2015 11:09
Dealing with a spritesheet of 16 images in a 4x4 matrix
import sys
import pygame
class Character(pygame.sprite.Sprite):
def __init__(self, *group):
super(Character, self).__init__(*group)
self.sheet = pygame.image.load('char.png')
self.at = 0
self.elapsed = 0
self.rect = pygame.rect.Rect(50, 100, 50, 50)
@omaraboumrad
omaraboumrad / singledispatch.py
Last active December 23, 2015 08:19
Poor man's singledispatch
# Define
def singledispatch(func):
through_map = {}
def register(through):
def _on(what):
through_map[through] = what
return what
return _on
def dispatch(what):
@omaraboumrad
omaraboumrad / fiddle.js
Created October 19, 2013 15:24
Parallax scrolling
window.onload = init;
function init(){
var canvas = document.createElement('canvas');
document.body.appendChild(canvas);
canvas.width = 400;
canvas.height = 400;
var ctx = canvas.getContext('2d');
@omaraboumrad
omaraboumrad / fiddle.html
Created October 19, 2013 15:34
space glider
<audio autoplay loop>
<source src="https://dl.dropboxusercontent.com/u/18982788/starwars.mp3" />
</audio>