Skip to content

Instantly share code, notes, and snippets.

View jnothman's full-sized avatar

Joel Nothman jnothman

  • Canva
  • Sydney
View GitHub Profile
@jnothman
jnothman / quizzes-only.js
Created March 27, 2012 04:15
Links to show Coursera lecture quizzes only
/*
On the coursera lecture index, execute this Javascript (via greasemonkey script, bookmarklet, etc) to show 'quizzes' links next to each lecture. Clicking it will open the lecture, but instead of showing the video, will:
1. Pause it
2. Show the first quiz
3. Upon clicking "skip" or "continue" on the quiz, proceed to the next
4. Repeat from 3.
5. Continue showing the video
Having watched a downloaded video, you can now easily do the in-lecture quizzes separately.
@jnothman
jnothman / gitvimrc.vim
Created March 28, 2012 12:34
source a git repository-specific .vimrc
function! SourceGitVimrc(dir)
let gitroot = system("cd " . fnameescape(a:dir) . "; git rev-parse --show-toplevel 2>/dev/null")
" Strip trailing newline and escape
let gitroot = substitute(gitroot, "\\n*$","","")
if strlen(gitroot) && filereadable(gitroot . '/.vimrc')
execute "source " . fnameescape(gitroot) . '/.vimrc'
endif
endfunction
@jnothman
jnothman / timedeltatype.py
Created November 12, 2012 05:41
An argparse type factory that produces datetime.timedelta objects
import datetime
import re
class TimeDeltaType(object):
"""
Interprets a string as a timedelta for argument parsing.
With no default unit:
>>> tdtype = TimeDeltaType()
@jnothman
jnothman / renumber-opera-session.py
Created December 16, 2012 23:53
This script renumbers an edited Opera Browser session file. If you remove some tabs/windows from an existing autosave.win (or other .win) file, the numbering becomes non-contiguous. Pipe the edited session file through this script and the numbering will now count from 1 to the required number of windows. However, you must also modify the 'window…
import re, sys
num_re = re.compile(r'(?<=^\[)[0-9]+')
in_n = ''
out_n = 0
def sub_cb(match):
global in_n, out_n
@jnothman
jnothman / extended_scorer.py
Created April 16, 2013 13:55
More functionality in scikit-learn `Scorer`
from __future__ import print_function
from abc import ABCMeta, abstractmethod
from functools import partial
import numpy as np
from sklearn.metrics import precision_recall_fscore_support
from sklearn.base import BaseEstimator
"""
Tool to examine the output of model selection search results from scikit-learn (assuming #1787).
Pandas might be more appropriate, but I haven't worked out how to do group_best there...
For example:
>>> my_search = GridSearchCV(est, param_dict={'a': [...], 'b': [...], 'c': [...]})
>>> my_search.fit(X, y)
>>> rw = ResultsWrangler(my_search.grid_results_, my_search.fold_results_)
>>> grouped = rw.group_best(['a', 'b'])
>>> print(zip(grouped.parameters, grouped.scores))
@jnothman
jnothman / gh-sqmrg.sh
Last active December 17, 2015 06:28
Merge a github branch (generally pull request), squashing its commits onto master.
#!/bin/bash
branch="$1"
if [ -z "$branch" -o -n "$2" ]
then
echo Usage: $0 gh_user:branch_name
exit 2
fi
from collections import defaultdict
from six import iteritems
def group_params(items, key_name=lambda x: True):
"""bin by sub-dicts where values are compared by id if not hashable
>>> a = ('boo', 6)
>>> b = ('boo', 6)
>>> id(a) == id(b)
from sklearn import pipeline, grid_search, linear_model, feature_selection, decomposition, datasets, preprocessing
iris = datasets.load_iris()
clf = pipeline.Pipeline([
('scale', preprocessing.StandardScaler()),
('sel', feature_selection.SelectKBest()),
('clf', linear_model.ElasticNet(warm_start=False)),
])
@jnothman
jnothman / assert_warns.py
Last active December 17, 2015 18:09
An extension of numpy.testing.assert_warns that handles checking the warning message/filename and handles multiple warnings using a cascade of critera.
"""An extension of numpy.testing.assert_warns
Handles checking of message and multiple warnings.
"""
from __future__ import absolute_import, print_function
import sys
import re
import warnings