Skip to content

Instantly share code, notes, and snippets.

@rctay
rctay / gist:9036785
Created February 16, 2014 16:25
Python: linked lists
class Node(object):
prev = None
next = None
value = None
def __init__(self, prev, value):
self.value = value
self.appendTo(prev)
def unlink(self):
@rctay
rctay / gist:9045618
Created February 17, 2014 06:18
ocaml prologue for HackerRank (reading ints, |>, etc)
let (|>) v f = f v;;
let string_of_char_list (ls : char list) : string =
let s = String.make (List.length ls) '0' in
List.fold_left (fun i c -> s.[i] <- c; i+1) 0 ls;
s;;
let append_string (xs : string list) (ls : char list) : string list =
let s = string_of_char_list ls in
if String.length s = 0 then xs else (xs @ [s]);;
@rctay
rctay / README.md
Last active August 29, 2015 14:00
paging through git log by commits

assumption: your format.pretty begins with %h, eg. format:%h %s (%an, %ad).

@rctay
rctay / browserify-deps-ex.js
Last active August 29, 2015 14:03
demo to filter out 'source' field in deps output
var browserify = require('browserify');
var JSONStream = require('JSONStream');
var through = require('through2');
var b = browserify({basedir: __dirname, entries: ['./main.js']})
b.deps({})
.pipe(through({objectMode: true}, function(chunk, enc, cb) {
// poor man's _.omit()
var o = {};
for (var prop in chunk) {
#!/usr/bin/env node
// Reads JSON from stdin, and runs a JSONPath expression from the command-line on it.
//
// eg.
// $ npm install # install dependencies
// $ echo '{"store": {"book":[{"category":"fiction"}]}}' | node jsonpath.js '$.store.book[0].category'
// fiction
var stdin = process.stdin,
@rctay
rctay / gist:e8c7f6e71b5096f257ea
Last active August 29, 2015 14:08
track local variables in a context
class LocalsTracker(object):
def __enter__(self):
self._locals = locals()
def __exit__(self):
locals_now = locals()
# compute diff between locals_now and self.locals...
def foo():
a = 100
@rctay
rctay / gist:6e583e956c37a310c2f7
Created April 11, 2015 16:34
append EXIF date taken to filename
# produce lines like
#
# mv "./IMG_8869.jpg" "2015-03-29_19-08-01_IMG_8869.jpg"
#
find \( -name 'DSC_*' -or -name 'IMG_*' -or -name 'GOPR*' \) -and -iname '*.jpg'\
| while read x; do\
echo mv \"$x\" \"$(exif --tag='Date and Time (Original)' -m "$x" | sed -e 's/:/-/g' -e 's/ /_/')_${x#./}\"; done >foo
# try one line first
awk NR==1 foo | sh -v
@rctay
rctay / gist:d4c1e97aee49c5e2e946
Created April 14, 2015 15:50
www.sgxdata.pebbleslab.com scrape table to csv eg. for Excel
var rows_all = document.getElementsByTagName("table")[4].children[0].children
var rows = Array.prototype.slice.call(rows_all, 2)
var rows_data = rows.map(function(row) { return Array.prototype.map.call(row.children, function(c, i) { var v = c.innerText; return i===2 ? v.replace("\n"," ") : v }) })
// poor man's csv - doesn't handle values with the delimiter itself!
console.log(rows_data.map(function(c) { return c.join(",") }).join("\n"))
$ git log --graph -50 e8f43ba
* e8f43ba (Merging., Fri Mar 19 23:08:01 2010 -0500, Chris Moffitt)
|\
| * 1a0573a (docs/configuration: change indentation for SATCHMO_SETTINGS block, Thu Mar 18 15:17:04 2010 +0800, Tay Ray Chuan)
| * d275386 (docs/configuration: improve LIVESETTINGS_OPTIONS block, Thu Mar 18 15:04:15 2010 +0800, Tay Ray Chuan)
| * 41be3c0 (docs/configuration: replace tabs with spaces, Thu Mar 18 14:33:14 2010 +0800, Tay Ray Chuan)
| * bf0f490 (docs/configuration: use data markup for L10N_SETTINGS, Thu Mar 18 14:16:48 2010 +0800, Tay Ray Chuan)
| * e488428 (docs/configuration: add reference to category and product urls, Thu Mar 18 13:58:43 2010 +0800, Tay Ray Chuan)
| * 1f0a0a0 (docs/configuration: use inline literals for settings keys, Thu Mar 18 13:53:14 2010 +0800, Tay Ray Chuan)
| * 9831229 (docs/configuration: use inline literals for code, Thu Mar 18 13:40:27 2010 +0800, Tay Ray Chuan)
@rctay
rctay / dulwich-test.sh
Created March 24, 2010 11:38
(MOVED 1122404) [dulwich] running tests
# dulwich - test commands
python setup.py test -s dulwich.tests
python setup.py test -s dulwich.tests.test_repository.RepositoryTests
# with nose - exclude compat
nosetests -e compat