Skip to content

Instantly share code, notes, and snippets.

View namoshizun's full-sized avatar
👋

Di Lu namoshizun

👋
View GitHub Profile
import time
from functools import wraps
def timeit(comment=None):
"""
auto establish and close mongo connection
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
def checktype(*types):
def typerror(expect, got):
raise TypeError('expecting {}, got{}'.format(expect, type(got)))
def checker(fn):
def receiver(*args, **kwargs):
for _type, arg in zip(types, args):
type(arg) is _type or typerror(_type, arg)
return fn(*args, **kwargs)
return receiver
@namoshizun
namoshizun / mini_async_await.js
Created June 12, 2017 00:47
implement async await using generators
// Helper
function print(obj) {
console.log(obj)
}
// Mimic Async-Await
function coRunner(feedback) {
let iterator = this;
let result = iterator.next(feedback);
@namoshizun
namoshizun / line_counter.py
Created June 12, 2017 00:43
count number of lines in the file ends with some extension
import sys
import os
import io
from prettytable import PrettyTable
def countLines(path, extension) :
pt = PrettyTable()
pt.field_names = ['filename', '#lines']
total_line = 0
total_files = 0
@namoshizun
namoshizun / k-mab.py
Last active July 4, 2017 23:39
multi_armed_bandits
from __future__ import division
from matplotlib import pylab as plt
import numpy as np
MAX = np.inf
DEBUG = True
# DEBUG = False
class Arm:
def __init__(self, name, mean, stddev, cost):