Skip to content

Instantly share code, notes, and snippets.

View oinopion's full-sized avatar

Tomek Paczkowski oinopion

View GitHub Profile
@oinopion
oinopion / utils.py
Created February 29, 2012 13:02
Render dict from Django model
def render_dict(pattern, data):
"""Render Django model to dict (helps in rendering JSON)"""
out = {}
for item in pattern:
if isinstance(item, basestring):
attr = manager_getattr(data, item)
out[item] = attr
elif isinstance(item, dict):
for subitem, subpattern in item.items():
attr = manager_getattr(data, subitem)
(cascade) % ./manage.py syncdb
Creating tables ...
Creating table ticket_a
Creating table ticket_b
Creating table ticket_c
Installing custom SQL ...
Installing indexes ...
No fixtures found.
(cascade) % ./manage.py shell
Python 2.7.2 (default, Jan 17 2012, 23:42:54)
@oinopion
oinopion / registry.py
Created February 2, 2012 00:02
Simple Dependency Injection with decorators
# encoding: utf-8
"""
Dependency injection made simple
@requires('stores')
class Action(object):
self.stores = None
@provides('stores')
class InMemoryStores(Stores):
# encoding: utf-8
def fib_gen():
a, b = 1, 1
while True:
yield a
a, b = b, a+b
def get_values(iterator, n):
l = []
for i in range(n):
# encoding: utf-8
def fib_gen():
a, b = 1, 1
while True:
yield a
a, b = b, a+b
def get_values(iterator, n):
l = []
for i in range(n):
@oinopion
oinopion / gunicorn.sh
Created December 15, 2011 11:39
Simple wrapper (used mostly in supervisord)
#!/bin/bash
# This script is a wrapper around gunicorn
# It provides clean interface for starting stopping app server
PROJECT_NAME=photon
HOME_DIR=/home/photon
RELEASE_DIR=$HOME_DIR/releases/current
GUNICORN_BIN=$HOME_DIR/bin/gunicorn_django
GUNICORN_PID=$HOME_DIR/pids/gunicorn.pid
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}