Skip to content

Instantly share code, notes, and snippets.

@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){
@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 / 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 / .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 / .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 / jsonformatter.py
Created March 2, 2017 18:15
Format JSON with Slack slash command and Flask
#!/usr/bin/python
# -*-coding: utf-8 -*-
# This is a Flask app that accepts raw JSON from Slack with /json command and returns indented JSON
from flask import Flask, request, jsonify, abort
import json
import demjson
import requests
@gyli
gyli / long_table_to_wide_table_in_python_pandas.py
Last active May 29, 2017 05:47
Convert long table to wide table in Python Pandas
# Convert long table like
# key long_column other_column_1
# ------------------------------------
# 0 Ann 1 A
# 1 Ann 3 A
# 2 Ann 5 A
# 3 John 3 B
# 4 John 5 B
# 5 George 1 A
# 6 George 3 A
@gyli
gyli / lazy_db_connection.py
Last active June 23, 2017 01:57
Lazy DB Connection in Python
import dictmysql # or whatever db library
class LazyConnector:
def __init__(self, *args, **kwargs):
self.__dict__['__factory'] = dictmysql.DictMySQL
self.__dict__['__con'] = None
self.__dict__['__args'] = args
self.__dict__['__kwargs'] = kwargs
def _add_conn(self):
@gyli
gyli / generate_fontawesome_mapping.py
Last active November 21, 2017 16:44
Generate a Font Awesome icon mapping into a Python script
# This script generates a .py file with a dictionary of Font Awesome icon mapping, like '500px': '\uf26e'
# Run:
# python generate_fontawesome_mapping.py > fontawesome_mapping.py
import yaml
import requests
import sys
INDENT = ' ' * 4
@gyli
gyli / asyncio_aiohttp_requests.py
Created January 8, 2018 22:03
Making requests with asyncio and aiohttp
import aiohttp
import asyncio
NUMBERS = range(12)
URL = 'http://httpbin.org/get?a={}'
async def fetch_async(a):
async with aiohttp.ClientSession() as session: