Skip to content

Instantly share code, notes, and snippets.


Richard Cornish richardcornish

View GitHub Profile
richardcornish / Local web server
Created Oct 15, 2014
Runs a local web server from an arbitrary directory
View Local web server
# Python 2.x
python -m SimpleHTTPServer 8000
# Python 3
python -m http.server
# Ruby
ruby -run -e httpd . -p 8000
richardcornish / JavaScript madness
Last active Aug 29, 2015
An incomplete list of bizarre failings of the JavaScript programming language
View JavaScript madness
- Prototypal inheritance forces an unstated empty object onto your object with key "prototype"
- functions can act as object constructors but with the same syntax
- empty array members break loops
- Infinity/-Infinity
- +0/-0
- null is an object
- testing NaN === NaN is *always false*
- typeof always returns "object" for all different reference types
- the type of NaN (not a number) is literally "number" but as a string
- void looks like a Java class but its only job is to return undefined
from decimal import Decimal
from django.db import models, connection
class LocationManager(models.Manager):
def find_nearby(self, latitude, longitude, radius, is_miles=True):
Uses Spherical Law of Cosines to determine places within a radial distance
richardcornish / Adder adder
Last active Aug 29, 2015
Uses variable-length arguments and closures to create an adding adding function
View Adder adder
// H5BP Front-end Developer Interview Questions
// Usage:
// 1 = add(1)();
// 3 = add(1, 2)();
// 6 = add(1, 2)(3);
// 10 = add(1, 2)(3, 4);
// 0 = add()();
var add = function () {
richardcornish / Django install
Last active Aug 24, 2016
The absolute bare minimum to run Django (requires Python, SQLite, pip, and virtualenv)
View Django install
virtualenv myproject
source myproject/bin/activate
pip install django
cd myproject/
django-admin startproject myproject
cd myproject/
python migrate
python runserver
richardcornish /
Last active Jan 19, 2016
Breaking Django's form wizard (GenderFormBasic) out across views by manually hacking HiddenInput and RadioSelect
from __future__ import absolute_import
from django import forms
from django.contrib.auth import get_user_model
from django.utils.safestring import mark_safe
from utils.mixins import NoLabelSuffixMixin
from .utils import HiddenInput, RadioSelect
User = get_user_model()
// One global variable for all your JavaScript
var MY_SITE = window.MY_SITE || {};
// Requires jQuery
// Global jQuery object is aliased to avoid polluting global namespace with "$"
MY_SITE = (function ($) {
'use strict';
var private_function;
richardcornish / gist:ec833827a0612f1c311a02ec4b299612
Last active Jul 17, 2017
Create new Python package for PyPi
View gist:ec833827a0612f1c311a02ec4b299612
# Create environment
mkvirtualenv -p python3 mydjangopackage
pip install django sphinx sphinx-autobuild sphinx-rtd-theme
# Create demo environment
mkvirtualenv -p python3 mydjangopackagedemo
pip install django gunicorn psycopg2 whitenoise dj-database-url
# Create docs
mkdir docs
View gist:0533b67d85752a66f4e75a0357eda28a
git remote add upstream<user>/<repo>.git
git fetch upstream
git checkout master
git merge upstream/master
git push