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 / worker.py
Last active June 15, 2016 15:19
A single worker with queue and promise pattern (inspiration from Javascript)
from Queue import Queue
from threading import Thread
'''
A single worker with queue and promise pattern
usage: worker.add_job(fn).then(callback_fn)
note: this might be anti-pattern
'''
@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 / cacher.py
Last active June 7, 2018 16:55
A simple memory (with storage) python cache system with live cache update.
from database import pickledb
from os.path import exists, join, dirname
from os import makedirs
from null import Null
import uuid
class CacheObject:
def __init__(self, cacher, key, val):
assert isinstance(cacher, Cacher)
@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 / catchableprocess.py
Last active May 19, 2019 00:35
Python CatchableProcess: Catch exceptions in spawned processes
from multiprocessing import Process
class CatchableProcess(Process):
def __init__(self, target, args=[], kwargs=dict()):
from multiprocessing import Queue
self.error = Queue(1)
self.result = Queue(1)
def wrapper(target, args, kwargs):
try:
@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