Skip to content

Instantly share code, notes, and snippets.

Avatar

Richard Cornish richardcornish

View GitHub Profile
View gist:0533b67d85752a66f4e75a0357eda28a
git remote add upstream https://github.com/<user>/<repo>.git
git fetch upstream
git checkout master
git merge upstream/master
git push
@richardcornish
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 jquery.site.js
// 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
richardcornish / forms.py
Last active Jan 19, 2016
Breaking Django's form wizard (GenderFormBasic) out across views by manually hacking HiddenInput and RadioSelect
View forms.py
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()
@richardcornish
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 manage.py migrate
python manage.py runserver
@richardcornish
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
// https://github.com/h5bp/Front-end-Developer-Interview-Questions#jscode
// Usage:
// 1 = add(1)();
// 3 = add(1, 2)();
// 6 = add(1, 2)(3);
// 10 = add(1, 2)(3, 4);
// 0 = add()();
var add = function () {
View managers.py
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
http://www.movable-type.co.uk/scripts/latlong.html
@richardcornish
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
@richardcornish
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
# PHP
@richardcornish
richardcornish / GitHub pages branch
Last active Aug 29, 2015
Creates a gh-pages branch based on a gh-pages directory, and serves its contents as a <username>.github.io website
View GitHub pages branch
# First time
mkdir gh-pages
touch gh-pages/index.html
git add gh-pages
git commit -m "First commit of GitHub Pages branch"
git push origin master
# Subsequent times
# Edit, save, commit, push files inside gh-pages directory
git subtree push --prefix gh-pages origin gh-pages