Skip to content

Instantly share code, notes, and snippets.

@calvingiles
calvingiles / SlicerClass.py
Last active December 21, 2015 14:28
Slicer Class - grab an instance of SlicerClass and slice it. Pass this anonymously as an argument to get natural slices as arguments instead of using start, stop, step arguments. Works with arbitrarily complex slice arguments as they are just passed through. Usage: foo(slice_arg=slicer[2:7]) or foo(slice_arg=slicer[:2,...,3:4:-0.5]) or even foo(…
class SlicerClass(object):
'''
slicerClass allows the creation of a slicer object.
A slicer, when indexed with square brackets, will return a slice object.
This allows a slice to be created and neatly passed as an argument.
'''
def __getitem__(self,val):
return val
def __len__ (self):
return 0
@calvingiles
calvingiles / kmeans_ex_log.r
Created October 7, 2013 19:12
log scaling of k-means example
library(stats)
library(ggplot2)
set.seed(1)
# for our first example, let's create some synthetic easy-to-cluster data
d <- data.frame()
d <- rbind(d, data.frame(x=1 + rnorm(20, 0, 0.1), y=1 + rnorm(20, 0, 0.1), label=as.factor(rep(1, each=20))))
d <- rbind(d, data.frame(x=1 + rnorm(20, 0, 0.1), y=3 + rnorm(20, 0, 0.1), label=as.factor(rep(2, each=20))))
d <- rbind(d, data.frame(x=3 + rnorm(20, 0, 0.1), y=1 + rnorm(20, 0, 0.1), label=as.factor(rep(3, each=20))))
d <- rbind(d, data.frame(x=3 + rnorm(20, 0, 0.1), y=3 + rnorm(20, 0, 0.1), label=as.factor(rep(4, each=20))))
@calvingiles
calvingiles / GCD.py
Last active December 30, 2015 07:19
Return the greatest common denominator of a and b.
def GCD(a, b):
if b:
return GCD(b, a % b)
return a
@calvingiles
calvingiles / ipp.py
Created December 19, 2013 19:30
i++
# Auto-incremented number, starting at 0.
# Relies on function defualts being evaluated only once and lists being mutable.
def ipp(__i=[-1]):
'''This is a dirty hack'''
__i[0] += 1
return __i[0]
@calvingiles
calvingiles / ai_il.py
Created December 19, 2013 19:50
auto-incrementor and infinate list
"""
>>> I = ai(start=0, step=1)
>>> I()
0
>>> I()
1
>>> J = ai(start=0, step=1)
>>> J()
0
>>> I()
@calvingiles
calvingiles / ai_il.py
Created December 19, 2013 19:50
auto-incrementor and infinate list
"""
>>> I = ai(start=0, step=1)
>>> I()
0
>>> I()
1
>>> J = ai(start=0, step=1)
>>> J()
0
>>> I()
@calvingiles
calvingiles / number_formatter.py
Last active August 29, 2015 14:01
arbitrary precision number formatter
import math
def num_fmt(num):
i_offset = 24 # change this if you extend the symbols!!!
prec = 3
fmt = '.{p}g'.format(p=prec)
symbols = ['Y', 'Z', 'E', 'P', 'T', 'G', 'M', 'k', '', 'm', 'u', 'n']
e = math.log10(abs(num))
@calvingiles
calvingiles / specialist_function_factory.py
Created May 21, 2014 14:58
specialist function factory
def specialist_function_factory(func, *s_args, **s_kwargs):
def specialist_func(*args, **kwargs):
all_args = s_args + args
all_kwargs = dict(list(s_kwargs.items()) + list(kwargs.items()))
return func(*all_args, **all_kwargs)
return specialist_func
alpha country numerical
BEL Belgium 056
BLZ Belize 084
BEN Benin 204
BMU Bermuda 060
BTN Bhutan 064
BOL Bolivia (Plurinational State of) 068
BES Bonaire, Saint Eustatius and Saba 535
BIH Bosnia and Herzegovina 070
BWA Botswana 072
@calvingiles
calvingiles / haiku.py
Last active August 29, 2015 14:04 — forked from friggeri/haiku
python function to generate heroku-like names
import random
def get_haiku(n_adj=1, sep='-'):
'''
return short haiku, seperated by sep.
Original from https://gist.github.com/afriggeri/1266756
'''
adjs = [