Skip to content

Instantly share code, notes, and snippets.

View imcomking's full-sized avatar

Donghyun Kwak imcomking

View GitHub Profile
@imcomking
imcomking / wave_file_read_write.py
Last active May 7, 2020 04:41
wave file read & write by Python Built-in function
import numpy as np
import wave
import matplotlib.pyplot as plt
import sounddevice as sd
class SoundFile:
def __init__(self, file_name=None):
# https://docs.python.org/3.6/library/wave.html
pass
@imcomking
imcomking / korean_character_parser.py
Created April 16, 2020 09:19
Korean subcharacter level unicode parsing python
# -*- coding: utf-8 -*-
cho = "ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ" # len = 19
jung = "ㅏㅐㅑㅒㅓㅔㅕㅖㅗㅘㅙㅚㅛㅜㅝㅞㅟㅠㅡㅢㅣ" # len = 21
jong = "ㄱ/ㄲ/ㄱㅅ/ㄴ/ㄴㅈ/ㄴㅎ/ㄷ/ㄹ/ㄹㄱ/ㄹㅁ/ㄹㅂ/ㄹㅅ/ㄹㅌ/ㄹㅍ/ㄹㅎ/ㅁ/ㅂ/ㅂㅅ/ㅅ/ㅆ/ㅇ/ㅈ/ㅊ/ㅋ/ㅌ/ㅍ/ㅎ".split('/') # len = 27
test = cho + jung + ''.join(jong)
hangul_length = len(cho) + len(jung) + len(jong) # 67
@imcomking
imcomking / partial_black.py
Last active June 15, 2023 13:04
It is partial black in python to be utilized on Windows PyCharm.
"""
It is partial black in python to be utilized in windows PyCharm.
- To see partial black here (https://blog.godatadriven.com/black-formatting-selection)
- This code is conversion of this shell script (https://gist.github.com/BasPH/5e665273d5e4cb8a8eefb6f9d43b0b6d)
If you want to use in windows, you should first complie this python to exe.
- To do that, see (https://pypi.org/project/auto-py-to-exe/)
"""
import os
import sys
@imcomking
imcomking / exponentially_weighted_average for Reinforcement Learning.py
Last active March 15, 2019 07:46
RL utility function exponentially_weighted_average for Calculating sum of discounted future reward or TD(lambda)
import numpy as np
def exponentially_weighted_matrix(discount, mat_len):
DisMat = np.triu(np.ones((mat_len, mat_len)) * discount, k=1)
DisMat[DisMat==0] = 1
DisMat = np.cumprod(DisMat, axis=1)
DisMat = np.triu(DisMat)
return DisMat
def exponentially_weighted_cumsum(discount, np_data):
@imcomking
imcomking / map.py
Created September 19, 2018 06:44
python dictionary dot access class
class Map(dict):
def __init__(self, *args, **kwargs):
super(Map, self).__init__(*args, **kwargs)
for arg in args:
if isinstance(arg, dict):
for k, v in arg.items():
self[k] = v
if kwargs:
@imcomking
imcomking / archive_the_bk_canvas.py
Last active May 9, 2018 06:52
Jupyter notebook bokeh canvas archive via javascript hacking
from IPython.core.display import display, HTML, Javascript
def Archive_the_bk_canvas():
archiving_the_bkcanvas= "\
console.log(this); \
var loop = setInterval(execute.bind(this, null), 1000); \
function execute(output_area){ \
console.log(this); \
if(this.element.find(\".bk-canvas\").length == 0){\
console.log(\"loop\"); \
@imcomking
imcomking / ipy2html.py
Last active May 2, 2018 10:53
Convert the ipython notebook to html with embedded attachment image files.
import nbformat
import nbconvert
import sys
if len(sys.argv) < 2:
print("Usage:", sys.argv[0], 'filename.ipynb', '[--slides]')
exit(-1)
with open(sys.argv[1]) as nb_file:
nb_contents = nb_file.read()
import sys, termios, atexit
from select import select
# save the terminal settings
fd = sys.stdin.fileno()
new_term = termios.tcgetattr(fd)
old_term = termios.tcgetattr(fd)
# new terminal setting unbuffered
new_term[3] = (new_term[3] & ~termios.ICANON & ~termios.ECHO)
@imcomking
imcomking / get_char_keyboard_nonblock
Created August 25, 2016 06:39
python linux key input
import termios, fcntl, sys, os
def get_char_keyboard_nonblock():
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try: