Skip to content

Instantly share code, notes, and snippets.

View bruth's full-sized avatar

Byron Ruth bruth

View GitHub Profile
@bruth
bruth / numbers.coffee
Created July 30, 2012 21:25
Prettifying numbers
# utils/numbers
#
# Utilities to formating numbers in a human-readable way.
#
# * toSuffixedNumber - for numbers greater than or equal to 1000,
# the output is a suffixed number
# * toDelimitedNumber - returns a delimited string representation
# of the number
define ->
@bruth
bruth / gist:4155168
Created November 27, 2012 16:16
vimrc
" Kick off pathogen
call pathogen#infect()
" Base
set autochdir " change to cwd
set autoindent " indent on next line
set backspace=2 " allow backspace over indent, eol, start insert
set colorcolumn=80 " color column 80
set nocompatible " explicitly turn off compaitable mode with Vi
set cursorbind " move cursor to corresponding line and column
class LdapBackend(ModelBackend):
"""Authenticate a user against LDAP.
Requires python-ldap to be installed.
Requires the following things to be in settings.py:
DEBUG - boolean
Uses logging module for debugging messages.
@bruth
bruth / gist:4444123
Created January 3, 2013 15:07
Conditional Vim colorscheme based on time of day
" Set color scheme according to current time of day.
let hr = str2nr(strftime('%H'))
if hr <= 6 || hr > 18
let cs = 'Tomorrow-Night'
else
let cs = 'Tomorrow'
endif
execute 'colorscheme '.cs
@bruth
bruth / flatten.py
Created January 4, 2013 21:30
Recursively flatten a list of lists and tuples
def flatten(l, output=None):
if output is None:
output = []
for x in l:
if isinstance(x, (list, tuple)):
flatten(x, output)
else:
output.append(x)
return output
@bruth
bruth / django_autoload_registry.py
Created January 13, 2013 15:56
Registry class that follows a common pattern in Django for autoloading specific modules for a given purpose, e.g. `models.py` and `admin.py`. One such use case enables registering instances or classes and making them available in the admin to associate with some data.
import inspect
from django.conf import settings
from django.utils.importlib import import_module
from django.core.exceptions import ImproperlyConfigured
class AlreadyRegistered(Exception):
pass
@bruth
bruth / jsondiff.js
Last active December 12, 2015 04:38
JavaScript implementation for constructing objects diffs compatible with JSON PATCH syntax.
var jsondiff = (function() {
// Patch helper functions
function getParent(paths, path) {
return paths[path.substr(0, path.match(/\//g).length)];
}
// Checks if `obj` is an array or object
function isContainer(obj) {
return _.isArray(obj) || _.isObject(obj);
@bruth
bruth / jquery-ajax-queue.js
Last active May 3, 2018 06:08
jQuery override to Ajax method to support queuing requests. A deferred object is returned to act as a proxy when the request is actually sent. By default, GET requests are not queued unless the `queue` option is `true`.
(function(root, factory) {
if (typeof define == 'function' && define.amd) {
define(['jquery'], function(jQuery) {
factory(jQuery);
});
} else {
factory(root.jQuery);
}
})(this, function(jQuery) {
view = DataView(...)
context = DataContext(...)
queryset = view.apply(context.apply())
iterator = queryset.raw()
for row in iterator:
pass
@bruth
bruth / jquery.scrape.js
Last active December 17, 2015 07:08
DOM-to-string jQuery plugin
(function($) {
$.scrape = function(options) {
options = options || {};
var el, page = [], clone;
// start with doctype
page.push('<!doctype html>');
// build the html tag since this cannot be captured as a child