Skip to content

Instantly share code, notes, and snippets.

View glenrobertson's full-sized avatar
💭
Taking the specifications from the customer to the Software Engineers

Glen Robertson glenrobertson

💭
Taking the specifications from the customer to the Software Engineers
View GitHub Profile
import eventlet, settings, datetime
from crimes.models import Crime
from django.db.models import Min,Max
pool = eventlet.greenpool.GreenPool(settings.DB_CONNECTIONS)
min_max_id = Crime.objects.all().aggregate(Min('id'),Max('id'))
min_id = min_max_id['id__min']
max_id = min_max_id['id__max']
# only show the fields specified in the json properties list of the model
if hasattr(item, 'json_properties'):
json_properties = getattr(item, 'json_properties')
properties = {}
for k,v in item.__dict__.iteritems():
if k in json_properties:
properties[k] = v
# the json properties list is not available, so copy all the properites
else:
@glenrobertson
glenrobertson / setup-postgis-template.sh
Created January 10, 2012 20:27
setup postgis template
# Set postgis-1.5 path.
POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib/postgis-1.5
# Creating the template spatial database
createdb -E UTF8 -T template0 template_postgis
# and add PLPGSQL language support.
createlang -d template_postgis plpgsql
# Loading the PostGIS SQL routines.
psql -d template_postgis -f $POSTGIS_SQL_PATH/postgis.sql
# The root directory of this project; also the directory where this file lives
import os
SETTINGS_DIR = os.path.abspath(os.path.dirname(__file__))
# A list of locations of additional static files
STATICFILES_DIRS = [os.path.join(SETTINGS_DIR, 'static')]
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
@glenrobertson
glenrobertson / mktempdir_cm.py
Created March 19, 2012 23:10
Context manager for making temporary directory
from contextlib import contextmanager
import os
import shutil
@contextmanager
def mktempdir(folder):
os.mkdir(folder)
yield
shutil.rmtree(folder)
@glenrobertson
glenrobertson / xrange_decimal.py
Created March 29, 2012 20:16
xrange for decimals
def xrange_decimal(start, stop, step):
i = start
while i < stop:
yield i
i += step
@glenrobertson
glenrobertson / white_noise_image.py
Created April 3, 2012 00:05
get PIL white noise image
from PIL import Image
def get_white_noise_image(width, height):
pil_map = Image.new("RGBA", (width, height), 255)
random_grid = map(lambda x: (
int(random.random() * 256),
int(random.random() * 256),
int(random.random() * 256)
), [0] * width * height)
pil_map.putdata(random_grid)
@glenrobertson
glenrobertson / fancy_open.py
Created April 19, 2012 21:29
open file depending on file extension: supports .bz2, .zip, .tar, .gz
def fancy_open(filename, mode='r'):
if args.file.endswith('.bz2'):
from bz2 import BZ2File
input_file = BZ2File(filename, mode)
elif args.file.endswith('.zip'):
from zipfile import ZipFile
input_file = ZipFile(filename, mode)
elif args.file.endswith('.tar'):
from tarfile import TarFile
@glenrobertson
glenrobertson / supervisord.sh
Created May 16, 2012 21:54
supervisord init.d script
#!/bin/bash
# Supervisord auto-start
#
# description: Auto-starts supervisord
# processname: supervisord
# pidfile: /var/run/supervisord.pid
SUPERVISORD=/usr/local/bin/supervisord
SUPERVISORCTL=/usr/local/bin/supervisorctl
#!/bin/bash
# run with path to postgis share dir
# e.g. ./postgis_template.sh /usr/local/Cellar/postgis/2.0.1/share/postgis
POSTGIS_TEMPLATE_PATH=$1;
# Creating the template spatial database
createdb -E UTF8 -T template0 template_postgis
# Loading the PostGIS SQL routines.