Skip to content

Instantly share code, notes, and snippets.

View phizaz's full-sized avatar
😀

Konpat phizaz

😀
View GitHub Profile
@phizaz
phizaz / threading.py
Last active June 15, 2016 15:21
EasyThreading: an alternative to Python's ThreadPool, but this version can make use of any kind of functions
from threading import Thread
from Queue import Queue
'''
an alternative to Python's ThreadPool, but this version can make use of any kind of functions
'''
class ThreadPool:
def __init__(self, cnt):
@phizaz
phizaz / window_slide.py
Created June 17, 2016 04:12
A sliding window implementation in python
def window_slide(iter, size=2):
mem = [None] * size
for i, each in enumerate(iter):
mem[i % size] = each
if i >= size - 1:
yield mem[(i + 1) % size:] + mem[:(i + 1) % size]
@phizaz
phizaz / l_method.py
Created March 21, 2016 11:42
A Python implementation of Salvador and Chan's L-Method for determining the number of clusters in hierarchical clustering algorithm
from fastcluster import linkage
from collections import deque
from sklearn.metrics import mean_squared_error
import numpy
def f_creator(coef, intercept):
def f(x):
return intercept + coef * x
return f
@phizaz
phizaz / freeport.py
Last active August 3, 2016 15:48
Python Getting A Free Port Number : A Multiprocess-safe Recipe
import fasteners
import threading
class BindFreePort(object):
def __init__(self, start, stop):
self.port = None
import random, socket
self.sock = socket.socket()
@phizaz
phizaz / requirements.txt
Last active August 7, 2016 08:45
Python SafeQuitProcess: On termination the process also propagates the termination signal to all its children
psutil
@phizaz
phizaz / run.sh
Last active August 25, 2016 10:54
RemoteRun: run a script at a remote server seamlessly
HOST="user@host"
REMOTE_PATH="/remoth/path"
RUN_COMMAND="docker run --rm -v ${REMOTE_PATH}:/run -w /run debian:jessie echo 'hello world!'"
echo "connecting to ${HOST} and sycing files to the remote ..."
rsync -a ./ "${HOST}:${REMOTE_PATH}"
echo "running the script at the remote ..."
ssh ${HOST} "cd ${REMOTE_PATH} && ${RUN_COMMAND}"
@phizaz
phizaz / .remoterunignore
Created August 29, 2016 06:41
RemoteRun - Example: docker-compose recipe
.remoterunignore
.remoterun_lock
.remoterunrc
.git
.gitignore
.DS_Store
@phizaz
phizaz / ptty.py
Last active September 2, 2016 10:10
Python Pseudo Terminal using pty implementation
# Copyright (c) 2011 Joshua D. Bartlett
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
@phizaz
phizaz / awaitboth.py
Last active December 20, 2016 03:48
Python awaitasync: wait both async and sync in one function
import asyncio
def unzip(l):
return zip(*l)
def merge(*itrs):
from itertools import chain
return chain.from_iterable(itrs)
async def wait(l):
@phizaz
phizaz / keybindings.json
Created January 6, 2017 16:25
VSCode Key Bindings for Windows using "ALT" as the main key
// Place your key bindings in this file to overwrite the defaults
[
{
"key": "alt+a",
"command": "editor.action.selectAll"
},
{
"key": "alt+x",
"command": "editor.action.clipboardCutAction"
},