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 / start_data_science_env.py
Last active August 29, 2015 14:10
Start data science environment container
from __future__ import print_function
import os, time, argparse
from subprocess import call, check_call, check_output
def stop_cmd(name):
return ['docker', 'stop', name], False
@calvingiles
calvingiles / stats_coroutines.py
Created September 9, 2014 10:22
Online mean and variance with generators
def online_mean():
"""
Return an initialised generator for online mean (next() has been called).
"""
omean = _online_mean()
next(omean)
return omean
def _online_mean():
@calvingiles
calvingiles / yield_from_example.py
Created September 9, 2014 09:11
Example of yield as filter
"""
An example of using the `yield from` syntax in python 3 and a generator expression than mimics this.
"""
def yield_only_odd(i):
'''
Yield i if and only if i is odd.
This is a generator that yields zero or one times.
'''
# IPython magic to check for PEP8 compliance.
# Author: Juan Luis Cano <juanlu001@gmail.com>
"""IPython magic to check for PEP8 compliance.
To use it, type
```%load_ext pep8magic```
and then
@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 = [
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 / 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))