Skip to content

Instantly share code, notes, and snippets.

View kkew3's full-sized avatar

Kaiwen kkew3

View GitHub Profile
@kkew3
kkew3 / pickle_cached.py
Last active June 22, 2018 20:11
Cache function call (function name, positional arguments, keyword arguments) using pickle. The overhead could be huge, so don't try to use this decorator unless the decorated function call takes at least ten minutes to return.
import tempfile
import pickle
import os
from functools import wraps
def _encode_fcall(f, *args, **kwargs):
"""
Encode a function invocation to a dictionary (function call descriptor).
Structure of the function call descriptor:
@kkew3
kkew3 / toggle_arxiv.ahk
Last active July 27, 2018 17:57
AutoHotKey script that toggles between arXiv abstract page and PDF. This script only supports Chrome for security (you won't want it to ruin other applications with possibly conflicting key bindings), but it's trivial to change.
/*
Under context of Chrome browser, and if the URL starts with "https://arxiv.org/":
"Windows + Alt + P" - Go to PDF is in abstract page, and go to abstract page if in PDF
(will not overwrite clipboard)
*/
#IfWinActive ahk_exe chrome.exe
#!p::
Send ^l
Sleep, 100
oldClipboard := Clipboard
@kkew3
kkew3 / print_datastruct.py
Created August 10, 2018 06:10
Inspect complex recursive python data structure
from boltons.iterutils import remap # pip install boltons
from pprint import pprint
def inspect_structure(obj):
"""
>>> inspect_structure([1, 2])
[<type 'int'>, <type 'int'>]
>>> import numpy as np
>>> inspect_structure([1, {3:'hola'}, np.eye(2)])
@kkew3
kkew3 / node-and-npm-in-30-seconds.sh
Last active September 21, 2018 22:07 — forked from isaacs/node-and-npm-in-30-seconds.sh
Use one of these techniques to install node and npm without having to sudo. Discussed in more detail at http://joyeur.com/2010/12/10/installing-node-and-npm/ Note: npm >=0.3 is *safer* when using sudo. The first two scripts work for me.
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl https://www.npmjs.org/install.sh | sh
@kkew3
kkew3 / requirements-jupyter.txt
Last active November 1, 2018 21:41
A set of jupyter notebook dependency without prompt-toolkit version conflict.
# this set of dependencies is acquired from:
# https://github.com/jupyter/jupyter_console/issues/158#issue-330642313
jupyter==1.0.0
jupyter-client==5.2.3
jupyter-console==5.2.0
jupyter-core==4.4.0
ipython==6.4.0
prompt-toolkit==1.0.15
@kkew3
kkew3 / rt.sh
Created November 26, 2018 09:26
Search for python virtualenv upwards from current directory till home directory, and source it if found. The virtualenv name is assumed to be "rt".
#!/bin/bash
if [ "${BASH_SOURCE[0]}" = "\${0}" ]; then
echo This script intends to be sourced rather than be called >> /dev/stderr
exit 1
fi
__findrt_upward() {
local acc=""
local targetrt=""
local venv="rt" # virtualenv name to search for
@kkew3
kkew3 / ensure_called.sh
Last active December 19, 2018 19:23
Ensure bash to be sourced or to be called as executable
if [ "${BASH_SOURCE[0]}" != "$0" ]; then
echo "This script intends to be called rather than be sourced"
return 1
fi
@kkew3
kkew3 / _check_shebang_bash.sed
Last active December 19, 2018 21:56
Ensure that bash shebang exists at the head of a script
/^ *$/d
/^#[^!]/d
1,/^#!\(\/bin\/\|\/usr\/bin\/env \)\(ba\)\?sh$/p
/^#!\(\/bin\/\|\/usr\/bin\/env \)\(ba\)\?sh$/q
@kkew3
kkew3 / mmnist.py
Created March 31, 2019 18:29
moving mnist dataset for pytorch, adapted from https://gist.github.com/tencia/afb129122a64bde3bd0c
"""
Defines Moving MNIST dataset and function to generate moving mnist.
Adapted from: https://gist.github.com/tencia/afb129122a64bde3bd0c
"""
import os
import math
import collections
import itertools
import bisect
@kkew3
kkew3 / csvselect.py
Created July 11, 2019 15:32
Small utility to select columns by name (assuming the first row contains titles) from CSV file without ambiguity (`csvcut` from `csvkit` has ambiguity currently)
#!/usr/bin/env python3
import argparse
import logging
import sys
def make_parser():
parser = argparse.ArgumentParser(
description='Select column(s) of CSV file by name assuming the first '
'row of the CSV lists the column names. Currently the '