Skip to content

Instantly share code, notes, and snippets.

View namoshizun's full-sized avatar
👋

Di Lu namoshizun

👋
View GitHub Profile
@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):
@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 / 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);
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
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):
@namoshizun
namoshizun / naive_gp.py
Created July 8, 2017 00:16
simple gaussian process
"""
Implement a very simple gaussian process for regression task.
Credit : http://katbailey.github.io/post/gaussian-processes-for-dummies/
"""
import numpy as np
import matplotlib.pyplot as pl
def prepare_data(n, fn):
@namoshizun
namoshizun / regret_matching.py
Created July 14, 2017 01:03
Use regret matching to play rock-paper-scissors
from __future__ import division
from random import random
import numpy as np
import pandas as pd
'''
Use regret-matching algorithm to play Scissors-Rock-Paper.
'''
class RPS:
# Install GPFlow
!git clone https://github.com/namoshizun/GPflow.git
!mv ./GPflow/gpflow ./
!rm -rf GPflow
!pip install multipledispatch
!pip install pytest
# Install Other libraries
!pip install edward
@namoshizun
namoshizun / print_progress.py
Last active May 30, 2018 04:26
Python decorator for printing function progresses
import functools
import inspect
from tqdm import tqdm
def print_progress(total=None, enabled=True):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if not enabled or total is None:
@namoshizun
namoshizun / test.py
Created June 11, 2018 10:56
[Python + FFMPEG] change video speed without affecting pitch and/or concatenate multiple clips
from vid_utils import Video, concatenate_videos
videos = [
Video(speed=1.0, path="C:/temp/my_video_1.mp4"),
Video(speed=2.0, path="C:/temp/my_video_2.mp4"),
Video(speed=0.5, path="C:/temp/my_video_3.mp4"),
]
concatenate_videos(videos=videos, output_file=f"C:/temp/output_video.mp4")