Skip to content

Instantly share code, notes, and snippets.

@rctay
rctay / gist:e8c7f6e71b5096f257ea
Last active Aug 29, 2015
track local variables in a context
View gist:e8c7f6e71b5096f257ea
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
View jsonpath.js
#!/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 / browserify-deps-ex.js
Last active Aug 29, 2015
demo to filter out 'source' field in deps output
View browserify-deps-ex.js
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) {
@rctay
rctay / README.md
Last active Aug 29, 2015
paging through git log by commits
View README.md

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

View gist:9829266
require(["lib/play-manager"], function(a) {
// via http://stackoverflow.com/a/6274381
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]
function shuf(o) { for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); }
shuf(a.source.models);
var c = a.getCurrentSound(), p = c ? c.isPaused() : false;
a.playNext(); a.playPrev(); // poor man's refresh of current playing song
if (p) a.pause(c); // preserve first-run state
@rctay
rctay / playlist_shuffle.py
Last active Nov 14, 2018
SoundCloud set/playlist shuffle via Python + Requests + mpg123. Ctrl-C for next (press Ctrl-C in quick succession to quit), Ctrl-Z/fg to pause/resume
View playlist_shuffle.py
import requests
import soundcloud
import random
import socket
import subprocess
SOUNDCLOUD_API_KEY = '00000000000000000000000000000000'
PLAYLIST_URL = 'https://soundcloud.com/nervomusic/sets/nervomusic-com'
@rctay
rctay / gist:9045618
Created Feb 17, 2014
ocaml prologue for HackerRank (reading ints, |>, etc)
View gist:9045618
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 / gist:9036785
Created Feb 16, 2014
Python: linked lists
View gist:9036785
class Node(object):
prev = None
next = None
value = None
def __init__(self, prev, value):
self.value = value
self.appendTo(prev)
def unlink(self):
View gist:8506594
'''A-1'''
alpha2num = lambda s: (ord(char) - ord('A') + 1 for char in s)
'''0-0'''
num2num = lambda s: (ord(num) - ord('0') for num in s)
import itertools
import re
LOOKUP = ('A', 'Z', 'Y', 'X', 'U', 'T', 'S', 'R', 'P', 'M', 'L', 'K', 'J', 'H', 'G', 'E', 'D', 'C', 'B')
@rctay
rctay / README.md
Last active Jan 2, 2016
[python] aggregate m,...,n to m-n
View README.md

The program reads a sequence of integers (sorted), and outputs x-y ranges for consecutive integers, outputting it verbatim otherwise.

cat | python ranges.py
1
2
3
7
8
9
^D