Skip to content

Instantly share code, notes, and snippets.

View y3nr1ng's full-sized avatar

Liu, Yen-Ting y3nr1ng

View GitHub Profile
@y3nr1ng
y3nr1ng / stopwatch.py
Last active December 31, 2017 07:49
Using context manager to time a code snippet (sourced from Dropbox SDK example).
import contextlib
import time
@contextlib.contextmanager
def stopwatch(message):
"""Context manager to print how long a block of code took."""
t0 = time.time()
try:
yield
finally:
@y3nr1ng
y3nr1ng / paramiko_ssh_config.py
Last active July 14, 2021 10:07
Paramiko with .ssh/config
client = paramiko.SSHClient()
client._policy = paramiko.WarningPolicy()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_config = paramiko.SSHConfig()
user_config_file = os.path.expanduser("~/.ssh/config")
if os.path.exists(user_config_file):
with open(user_config_file) as f:
ssh_config.parse(f)
@y3nr1ng
y3nr1ng / pick_unused_port.py
Last active September 27, 2016 09:48
Pick unused port on OS. Race condition might occur after close().
import socket
def pick_unused():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 0))
addr, port = s.getsockname()
s.close()
return port
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
int kbhit(void) {
struct termios oldt, newt;
int ch;
int oldf;
@y3nr1ng
y3nr1ng / batchsips.sh
Last active September 18, 2016 01:02
Batch image format conversion using SIPS (Scriptable Image Processing System) in OS X.
#!/bin/bash
echo ""
read -e -p "Target folder name: " folder
echo "Duplicate folder structure of $folder.."
if [ -d "Compressed-${folder}" ]; then
echo ""
read -r -p "\"Compressed-${folder}\" exist, overwrite? [y/n] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
@y3nr1ng
y3nr1ng / gifextract.py
Last active July 10, 2016 06:14 — forked from BigglesZX/gifextract.py
Extract frames from an animated GIF, correctly handling palettes and frame update modes
'''
A fork of this gist https://gist.github.com/BigglesZX/4016539 .
Ported to Python3 and verify it with Pillow.
'''
import argparse
import os
from PIL import Image
def find_dir(path):
@y3nr1ng
y3nr1ng / genpkl
Created May 19, 2016 17:51
Task 2 - DNN
#!/usr/bin/env python3
import os, argparse, logging
# using doc2vec model
from gensim.models import Doc2Vec
# generate compressed pickle file
import pickle, numpy, gzip
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s')
@y3nr1ng
y3nr1ng / cmb-test.py
Created May 19, 2016 17:49
Task 2 - Voting
#!/usr/bin/env python3
import os, sys, argparse, logging
baseline = '/tmp2/b03902036/train-punc.pro'
result = '/tmp2/b03902036/result.cmb'
ground_truth = dict()
with open(baseline, 'r') as in_file :
for line in in_file :
from gensim.models.doc2vec import Doc2Vec
from gensim.test.test_doc2vec import ConcatenatedDoc2Vec
from sklearn.linear_model import LogisticRegression, SGDClassifier, LogisticRegressionCV
from sklearn import svm
from sklearn.externals import joblib
import jieba
import numpy as np
import logging
import os
import sys
@y3nr1ng
y3nr1ng / words-crawl.py
Created May 19, 2016 16:45
Dictionary Crawler
#!/usr/bin/env python3
import requests, json, sys, random
from html.parser import HTMLParser
from time import sleep
from tqdm import tqdm
import cProfile
url = 'http://words.sinica.edu.tw/ftms-bin/scripts/words_scripts/zizi1.pl'