Skip to content

Instantly share code, notes, and snippets.

@gyli
gyli / .cvimrc
Last active April 1, 2016 05:34
.cvimrc
set noautofocus
set nocncpcompletion
set smoothscroll
set hud
set noregex
set noinsertmappings
set typelinkhints
set defaultnewtabpage
let scrollduration = 20
let searchlimit = 30
@gyli
gyli / .tmux.conf
Last active August 29, 2015 14:25
My tmux config
# unbind some default keybindings
unbind C-b
# set prefix key to ctrl-a
set -g prefix C-a
# pass through a ctrl-a if you press it twice
bind C-a send-prefix
# vim style bindings for pane movement
bind -r h select-pane -L
bind -r j select-pane -D
@gyli
gyli / parameter-validator-with-decorator.py
Last active April 21, 2019 05:36
Parameter Validator with decorator
def accepts(*types):
def check_accepts(f):
assert len(types) == f.func_code.co_argcount
def new_f(*args, **kwds):
for (a, t) in zip(args, types):
assert isinstance(a, t), "arg %r does not match %s" % (a, t)
return f(*args, **kwds)
new_f.func_name = f.func_name
@gyli
gyli / abb2state.R
Last active May 9, 2018 15:08
Convert state abbreviation into full name (or opposite)
abb2state <- function(name, convert = F, strict = F){
data(state)
# state data doesn't include DC
state = list()
state[['name']] = c(state.name,"District Of Columbia")
state[['abb']] = c(state.abb,"DC")
if(convert) state[c(1,2)] = state[c(2,1)]
single.a2s <- function(s){
@gyli
gyli / pkg.install.R
Last active August 29, 2015 14:06
Package loading in R (Check if installed before loading)
pkg.install <- function(x)
{
if (!require(x,character.only = TRUE))
{
install.packages(x,dep=TRUE)
if(!require(x,character.only = TRUE)) stop("Package not found")
}
}
pkg.install("DBI")
@gyli
gyli / options.R
Created September 19, 2014 01:21
Input arguments (flags like) through command line to R
#Rscript filename.R --host= --dbname= --user= --password=
options<-paste0("--",c("host","dbname","user","password"))
args <- commandArgs(TRUE)
if(length(args)!=4)stop("Please input the full options of host, dbname, user and password.")
db.options<-function(option){
option.location <- grepl(option,args)
if(sum(option.location)!=1)
stop(paste0("Something wrong with ", option, "."))
if(!grepl("=",args[option.location]))
@gyli
gyli / num.to.LETTERS.R
Last active August 29, 2015 14:01
Excel column index translator with R
#Translate integer into Excel column index, such as:
# 1-26 -> A-Z
#27-52 -> AA-AZ
#53-78 -> BA-BZ
#...
num.to.LETTERS<-function(num,letter=""){
if(num<=0){
return(NA)
}else if(num/26<=1){