Skip to content

Instantly share code, notes, and snippets.

View RutledgePaulV's full-sized avatar

Paul Rutledge RutledgePaulV

View GitHub Profile
@RutledgePaulV
RutledgePaulV / stickyfeet.less
Created March 21, 2014 09:17
This little mixin handles everything necessary to get a 'sticky' footer. Simply pass in the selectors for the DOM elements as described below.
.force-height(@height: 100%) {
height: @height;
min-height: @height;
}
.apply(@selector: ~'', @contents){
@{selector}{
@contents();
}
}
@RutledgePaulV
RutledgePaulV / pretty_print_template_tag.py
Last active July 3, 2016 18:05
This custom template tag uses the Beautiful Soup library to prettify the html output from rendered django templates.
from django.template import Node
class PrettyPrintNode(Node):
def __init__(self, nodelist):
self.nodelist = nodelist
def render(self, context):
from bs4 import BeautifulSoup
html = BeautifulSoup(self.nodelist.render(context))
@RutledgePaulV
RutledgePaulV / BrowserFuncs.js
Last active August 29, 2015 14:04
Sahi Browser Function Manager
/**
* This utility allows us to ignore using Sahi's <browser> </browser> tags for defining functions that should be run on the browser.
* Instead we can simply call these utilities to pass functions back and forth between the two, which is super useful.
*/
var $Browser = (function ($Browser) {
/**
* Adds a function to the window object on the browser side of things.
*
* @param $key - The name that should be assigned to the function on the browser.
@RutledgePaulV
RutledgePaulV / DjangoPlugins.py
Last active August 29, 2015 14:07
Builds on a meta-class plugin architecture in a way that automatically registers plugins across django apps and allows for easy use in the form of a decorator.
from django.utils.module_loading import autodiscover_modules
# creating a decorator that effectively sets the 'Plug' class
# as the meta class and passes along the key and module
def Plugin(key, module):
class Plug(type):
# here because we need to allow kwargs but not pass them to the type constructor
def __new__(cls, name, bases, namespace, **kwargs):
@RutledgePaulV
RutledgePaulV / ObjectUtil.java
Created February 2, 2016 03:33
A generic instantiation method for java that determines the best constructor to use for the provided arguments.
public final class ObjectUtils {
private ObjectUtils() {
}
/**
* Instantiate a class for the provided constructor arguments.
*
* @param clazz The class to instantiate
*
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B): F[B]
}
trait Applicative[F[_]] extends Functor[F] {
def unit[A](a: => A): F[A]
def map2[A,B,C](fa: F[A], fb: F[B])(f: (A, B) => C): F[C]
def map[A,B](fa: F[A])(f: A => B): F[B] =
@RutledgePaulV
RutledgePaulV / make-model.clj
Last active July 3, 2016 19:26
A macro wrapping def-record that also defines a constructor function with default values.
(defmacro make-model
[name args & body]
(let [defaults (if (map? (first body)) (first body) {})
constructor-name (str/lower-case (str "make-" name))]
`(do (defrecord ~name ~args ~@(if (map? (first body)) (rest body) body))
(defn ~(symbol constructor-name)
([] (~(symbol constructor-name) {}))
([values#] (~(symbol (str "map->" name)) (merge ~defaults values#)))))))
@RutledgePaulV
RutledgePaulV / map-symbols.clj
Created July 29, 2016 02:56
Macro to convert variadic provided symbols into a map of symbol-turned-keyword -> symbol-value
(defmacro map-symbols [& symbols]
"Converts variadic provided symbols into a map of symbol-turned-keyword -> symbol-value"
(into {} (map #(hash-map (keyword (name %)) (var-get (resolve %))) symbols)))
(defn aliasing [& keys-and-values]
"Creates a map from pairs of vector/value entries. Each entry in the vector is used as a key for the corresponding value."
(loop [result {} entries (into [] (partition 2 keys-and-values))]
(let [entry (first entries) keys (first entry) value (second entry)]
(if (empty? entries)
result
(recur (merge result (zipmap keys (repeatedly #(identity value)))) (rest entries))))))
@RutledgePaulV
RutledgePaulV / subtrees.clj
Created October 10, 2016 04:52
Given a tree shaped list, decompose it into all subtrees.
(defn subtrees [tree]
(if (or (nil? tree) (empty? tree))
'()
(let [parent (first tree) children (take-while list? (rest tree))]
(if (not-empty children)
(concat
(map #(list parent %) children)
(mapcat subtrees children)
(subtrees (drop (inc (count children)) tree)))
(subtrees (next tree))))))