Skip to content

Instantly share code, notes, and snippets.

@exallium
exallium / container.py
Created September 1, 2012 03:42
Useful BaseContainer and BaseContainerElement classes
from inspect import getmembers
class BaseContainerElement(object):
"""
BaseContainerElement is a generic element for a container
Process is where the magic should take place.
"""
def process(self, **kwargs):
pass
@exallium
exallium / contrast.java
Created September 11, 2012 18:40
Function to give contrast to input color.
public static int contrast(int argb) {
double a = 1 - (0.299 * Color.red(argb) + 0.587 * Color.green(argb) + 0.144 * Color.blue(argb))/255;
int d = 0 ? a < 0.5 : 255;
return Color.argb(255, d, d, d);
}
@exallium
exallium / crawler.py
Created September 11, 2012 19:53
A neat way to cripple your network
from lxml.html import fromstring
from httplib import HTTPConnection
class Crawler(object):
max_depth = 2
def __init__(self, href=None, depth=0):
if depth == self.max_depth:
return
@exallium
exallium / gist:4032255
Created November 7, 2012 15:31
One liner to remove JS files compiled from CoffeeScript using the current directory as root for the search.
rm `find . -iname '*.coffee' | sed 's/coffee$/js/g'`
@exallium
exallium / fabfile.py
Created November 7, 2012 16:56
Smart makemessages wrapper to account for gettext statements in CoffeeScript files.
from fabric.api import lcd, local
from importlib import import_module
import sys
def makemessages(settings_module='settings'):
"""
Generates the *.po files for everything.
MAKE SURE YOU RUN collectstatic _BEFORE_ BUILDING THE PO FILES.
@exallium
exallium / m
Created December 6, 2012 20:29
ZSH Functions for Django
# Manage
if [[ ! -a manage.py ]] then
print "Your location is bad and you should feel bad."
return
fi
local dir=`find . -name settings_local.py | perl -ne'/\/([A-Za-z0-9_]*)\/settings_local.py/ && print $1'`
if [[ -z dir ]] then
print "Local settings not found!"
/**
* Generic loadData method. Any parameter that matches a column will
* have data loaded.
* @param resultSet The result set from the database query
* @throws IllegalAccessException The requested field couldn't be accessed
*/
private void loadData(ResultSet resultSet) throws IllegalAccessException {
Field[] fields = getClass().getDeclaredFields();
for (Field f : fields) {
@exallium
exallium / bf.c
Last active December 11, 2015 07:39
Simple brainfuck interpreter
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NUL '\0'
struct Loop {
char *start;
char *end;
struct Loop *next;
};
@exallium
exallium / oauth2tokenfromcredentials.py
Created March 13, 2013 19:40
Discovery (oauth2client) to GData OAuth2Token converter. Blatantly taken from http://blog.bossylobster.com/2012_12_01_archive.html (Thanks!)
class OAuth2TokenFromCredentials(OAuth2Token):
def __init__(self, credentials):
self.credentials = credentials
super(OAuth2TokenFromCredentials, self).__init__(None, None, None, None)
self.UpdateFromCredentials()
def UpdateFromCredentials(self):
self.client_id = self.credentials.client_id
self.client_secret = self.credentials.client_secret
self.user_agent = self.credentials.user_agent
@exallium
exallium / lev.js
Created May 15, 2013 18:02
Iterative Levenshtein Distance algorithm adapted from http://en.wikipedia.org/wiki/Levenshtein_distance
// Iterative with two matrix rows
function lev(left, right) {
// Degenerative Cases
if (left == right) return 0;
if (left.length == 0) return right.length;
if (right.length == 0) return left.length;
var vectorLength = right.length + 1;
var workVector0 = new Array();