Skip to content

Instantly share code, notes, and snippets.

@n9986
n9986 / pip-upgrade.py
Created May 25, 2012 11:06
Upgrade all pip packages
import pip
from subprocess import call
for dist in pip.get_installed_distributions():
call("pip install --upgrade " + dist.project_name, shell=True)
@n9986
n9986 / Common.h
Created May 25, 2012 11:17
Simple way to save memory on Serial.print on Arduino
#ifndef __COMMON__
#define __COMMON__
#include "Arduino.h"
//#define DEBUG_ON
#ifdef DEBUG_ON
#define initLog(speed) Serial.begin(speed)
#define log(buf) Serial.print(buf)
@n9986
n9986 / image_loading_in_webkit.js
Created June 7, 2012 16:24
Image Loading tip for Webkit browsers
/*
Webkit browsers set the height and width property after the image is loaded.
This results in zero being returned on trying to get the height and width.
So to get the right width and height using Javascript/jQuery
(.height() and .width()) consider using a img loaded callback as shown in the
example below.
*/
$('img').load(function() {
@n9986
n9986 / pythonchallenge-1to5.py
Created June 21, 2012 11:49
pythonchallenge.com solutions (1 to 5)
# I kept solving the puzzles until I reached the donation page. I give up. No dollars please.
################################################################################
# For challenge 5
def challenge5():
import pprint
import pickle
f = open('banner.p', 'rb')
@n9986
n9986 / gist:3664438
Created September 7, 2012 08:48
Quick method to check if a number is whole or not
-(BOOL)isWholeNumber:(double)number
{
double integral;
double fractional = modf(number, &integral);
return fractional == 0.00 ? YES : NO;
}
@n9986
n9986 / gist:3673973
Created September 8, 2012 11:37
Get a NSURLRequest object for a resource with path and type
-(NSURLRequest *)getURLRequestForResource:(NSString *)fileName ofType:(NSString *)type
{
NSString *bundleFilePath = [[NSBundle mainBundle] pathForResource:fileName
ofType:type];
NSURL *bundleURL = [NSURL fileURLWithPath:bundleFilePath];
NSURLRequest *bundleFileRequest = [NSURLRequest requestWithURL:bundleURL];
return bundleFileRequest;
}
@n9986
n9986 / gist:3737028
Created September 17, 2012 12:30
Random Integer in Javascript
/**
* Returns a random integer between min and max
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
@n9986
n9986 / spatialhash.py
Created September 17, 2012 12:39
Spatial Hash
from math import floor
from collections import namedtuple
Rect = namedtuple('Rect', ('x1', 'y1', 'x2', 'y2'))
class SpatialHash(object):
def __init__(self, cell_size=10.0):
self.cell_size = float(cell_size)
self.d = {}
@n9986
n9986 / gist:3765265
Created September 22, 2012 05:41
Recursive Multi Location Indexing
import sys
from collections import Sequence,defaultdict
#making code python3-compatible
if sys.version_info[0] == 3:
basestring = str
def buildLocator(tree):
locator = defaultdict(list)
def fillLocator(tree, locator,location):
@n9986
n9986 / gist:3765268
Created September 22, 2012 05:44
Item in tuple finder using numpy
# Works for nicely ordered data
import numpy as np
RECIPES = (
('apple', 'sugar', 'extreme_Force'),
('banana', 'syrup', 'magical_ends'),
('caramel', 'chocolate', 'pancake_MONSTER'),
)
np_recipes = np.array(recipes)
indices = zip(*np.where( np_recipes == 'banana' ) ) #[(1, 0)]