Skip to content

Instantly share code, notes, and snippets.

@daryl314
daryl314 / BackgroundPty.py
Created May 8, 2019 16:58
Execute in a background pseudo-terminal
import pty, sys, os, re, tty, select
class ReadBuffer(object):
"""Buffer read operations"""
def __init__(self, fd):
if isinstance(fd, file):
fd = fd.fileno()
self.fd = fd
@daryl314
daryl314 / SortedListHelpers.py
Created January 9, 2019 17:23
Helpers to find entries in a sorted list (using python bisect module)
from bisect import *
##### HELPER FUNCTIONS FROM PYTHON DOCUMENTATION #####
def index(a, x):
'Locate the leftmost value exactly equal to x'
i = bisect_left(a, x)
if i != len(a) and a[i] == x:
return i
raise ValueError
@daryl314
daryl314 / SearchQueue.py
Created January 8, 2019 14:21
BFS search queue in python
from collections import deque
class SearchQueue(object):
"""BFS search queue"""
def __init__(self, seeds=[]):
"""Initialize with an optional list of seeds"""
self.Q = deque()
self.explored = set()
self.pushAll(seeds)
@daryl314
daryl314 / ProgressBar.py
Created October 6, 2017 13:22
Python ANSI progress bar
import sys,subprocess,time
class ProgressBar:
"""Class to handle a progress bar for a given job size"""
def __init__(self, jobSize, leader='', logger=sys.stdout, maxCols=80):
ttyr,ttyc = self.ttysize()
self.nCols = min(ttyc,maxCols-2) - len(leader)
self.n = jobSize
self.counter = 0
@daryl314
daryl314 / ParallelProcessing.py
Last active May 3, 2019 19:34
Python: Parallel map/foreach over an input array without Pool pickling restrictions
from multiprocessing import Process, BoundedSemaphore, RawArray, Pipe, cpu_count, RawValue
from threading import Thread
import resource, time, sys, os, signal
try:
import psutil
except:
print("psutil import failure -- ParallelJob timeouts disabled")
################################################################################