Skip to content

Instantly share code, notes, and snippets.

View gjcourt's full-sized avatar

George Courtsunis gjcourt

View GitHub Profile
@gjcourt
gjcourt / iPhone Scale.js
Created October 29, 2010 07:19
get the scale on all iDevices
function scale() {
return document.body.clientWidth / window.innerWidth;
}
@gjcourt
gjcourt / javascript html truncate.js
Created January 5, 2011 02:49
javascript translation of Benjamin Thomas' ruby code
/*
truncate text within HTML
does not work with malformed html
*/
function html_truncate(elem, num_words, truncate_string) {
// local state
var original = elem,
current = elem.childNodes[0],
count = 0;
@gjcourt
gjcourt / JSON Object Equality.js
Created February 25, 2011 04:26
test if two JSON objects are equivalent
/**
* not so simple check for object equality
*/
var equal = function(a, b) {
function check(a, b) {
for (var attr in a) {
if (a.hasOwnProperty(attr) && b.hasOwnProperty(attr)) {
if (a[attr] != b[attr]) {
switch (a[attr].constructor) {
case Object:
@gjcourt
gjcourt / detect.js
Created May 20, 2011 19:46
Javascript Object Key Detection
function detect(obj, attr, exact) {
var results = [],
re = new RegExp(attr, 'i');
// helper function for the recursion
function detectHelper(obj, attr, exact, path, results) {
for (var a in obj) {
// sanity check
if (!obj.hasOwnProperty(a))
continue;
@gjcourt
gjcourt / fib.py
Created June 22, 2011 20:04
Recursive and Iterative Fibonacci
# lines words bytes
# 5 17 102
def rfib(n):
if n == 0 or n == 1:
return 1
else:
return rfib(n-1) + rfib(n-2)
# 8 23 113
@gjcourt
gjcourt / datetime_loader.py
Created July 19, 2011 22:41
python datetime loader
def load_datetime(value):
from datetime import datetime
if type(value) is str:
import re
JAVASCRIPT_DATE = re.compile(r'^\d{4}(?:-\d{2}){2}T(?:\d{2}:){2}\d{2}$')
PYTHON_DATE = re.compile(r'^\d{4}(?:-\d{2}){2}\s(?:\d{2}:){2}\d{2}$')
PYTHON_DATE_MICROSECONDS = re.compile(r'^\d{4}(?:-\d{2}){2}\s(?:\d{2}:){2}\d{2}\.\d+$')
STRING_NUMBER = re.compile(r'^\d+(\.\d+)?$')
if JAVASCRIPT_DATE.match(value):
return datetime.strptime(value, '%Y-%m-%dT%H:%M:%S')
@gjcourt
gjcourt / query_parser.py
Created July 27, 2011 20:57
One line query string parser (@dcramer)
dict((k, v[0] if len(v) == 1 else v) for k, v in [(k, [z[1] for z in v]) for k, v in itertools.groupby(sorted([x.split('=') for x in qs.split('&')], key=lambda x: x[0]), key=lambda x: x[0])])
/**
* When element is visible (has non-zero height), execute callback
*/
function whenVisible(elem, cb) {
(function() {
if (elem.height() !== 0) {
cb();
} else {
setTimeout(arguments.callee, 250);
}
this.decay = function(feat, time, callback) {
var steps = 100;
var ts = time / steps;
for (var i = 0; i < steps; ++i) {
var opacity = 1 - ((i / steps)*.5);
(function(i, opacity){setTimeout(function() {
feat.style.fillOpacity = opacity;
feat.style.pointRadius = 4 * opacity;
commentsLayer.drawFeature(feat);
if (i == steps - 1) {
@gjcourt
gjcourt / spellchecker.py
Created August 31, 2011 22:20
Spellcheck
import re
import pprint
import os
path = os.path.join(os.path.dirname(__file__), 'words.txt')
vowel_pattern = re.compile('[aeiou]', re.IGNORECASE)
def run():
"""runs the program"""
input = raw_input('>')