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
@glenrobertson
glenrobertson / index.html
Last active August 29, 2015 14:03
Map with TMS params
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<style type="text/css">
html, body, #map {
margin: 0;
padding: 0;
@glenrobertson
glenrobertson / data_required_if.py
Created June 15, 2015 21:09
Conditional DataRequired wtforms validator
class DataRequiredIf(wtforms.validators.DataRequired):
"""
Similar to DataRequired
Only evaluates if func(form.data) evaluates to True
"""
def __init__(self, func, *args, **kwargs):
super(DataRequiredIf, self).__init__(*args, **kwargs)
self.func = func
def __call__(self, form, field):
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 / 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