Skip to content

Instantly share code, notes, and snippets.

View omaraboumrad's full-sized avatar

Omar Abou Mrad omaraboumrad

View GitHub Profile
@omaraboumrad
omaraboumrad / test.py
Created November 12, 2012 23:11
lists files based on requested path (wsgiref playground)
import os
from wsgiref import simple_server
def format_html(entries):
html = """<html><body><ul>%s</ul></body></html"""
return html % entries
def format_entries(requested_dir, children):
result = []
a = '<li><a href="%s">%s</a></li>'
@omaraboumrad
omaraboumrad / processor.py
Created December 20, 2012 08:20
Django context processor
# in processor.py
def site_processor(request):
return {'Foo': 'Bar'}
# in settings.py
from django.conf import global_settings as DEFAULT_SETTINGS
TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
'myapp.processor.site_processor',
)
@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):