Skip to content

Instantly share code, notes, and snippets.

View jsheedy's full-sized avatar

Joseph L. Sheedy jsheedy

  • San Francisco Bay Area
View GitHub Profile
@jsheedy
jsheedy / iter_file.py
Last active February 2, 2024 06:59
Sometimes you need a file-like object when all you have is an iterator, for instance when using psycopg2's cursor.copy_from. This class will handle the impedance mismatch.
import io
import sys
class IteratorFile(io.TextIOBase):
""" given an iterator which yields strings,
return a file like object for reading those strings """
def __init__(self, it):
self._it = it
self._f = io.StringIO()
@jsheedy
jsheedy / Dockerfile
Created July 2, 2018 19:17
dockerfile-compose for a celery worker with autoreload on code change
FROM python:3.6
RUN mkdir /app
ADD requirements.txt /app/
WORKDIR /app/
RUN pip install -r requirements.txt
CMD [ \
"watchmedo", \
"auto-restart", \
@jsheedy
jsheedy / pg_copy_from.py
Last active January 23, 2023 17:52
benchmark for postgres inserts using copy_from and IteratorFile from https://gist.github.com/jsheedy/ed81cdf18190183b3b7d
import time
import psycopg2
from iter_file import IteratorFile
conn = psycopg2.connect(host="localhost", database="test")
# args = [(1,2), (3,4), (5,6)]
args = [(i,i+1) for i in range(1,1*10**4,2)]
@jsheedy
jsheedy / skeletonize.py
Created June 10, 2016 23:56
OpenCV-Python skeletonize function
def skeletonize(img):
""" OpenCV function to return a skeletonized version of img, a Mat object"""
# hat tip to http://felix.abecassis.me/2011/09/opencv-morphological-skeleton/
img = img.copy() # don't clobber original
skel = img.copy()
skel[:,:] = 0
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
@jsheedy
jsheedy / ansi_grafx.py
Last active March 30, 2022 19:47
ansi grafx demo
import io
import itertools
import shutil
import sys
BLOCK = chr(9608)
ESC = "\x1b["
RST = ESC + "0m"
@jsheedy
jsheedy / run.sh
Created September 15, 2020 16:31
bash run() - exit script if subprocess fails
function run() {
# usage: run "command string --with-args"
cmd_output=$(eval $1)
return_value=$?
if [ $return_value != 0 ]; then
echo "Command $1 failed"
exit -1
else
echo "output: $cmd_output"
echo "Command succeeded."
""" quick hack to press the buttons on the Spotify desktop app based on nanoKONTROL transport buttons using cliclick.
The volume control on slider 1 is especially janky """
import subprocess
import rtmidi
midiin = rtmidi.RtMidiIn()
def print_message(midi):
@jsheedy
jsheedy / README.md
Created December 2, 2016 20:47
asyncio + uvloop echo server benchmark

asyncio + uvloop echo server benchmark

This is an attempt at a bare minimum client/server benchmark of asyncio with optional use of uvloop. On my 2015 macbook pro, I get almost 2.5X improvement using uvloop in the server.

The client runs PARALLEL tasks at once. Running only a single task results in about 1/8 the throughput of 100 simultaneous tasks.

# with uvloop
$ python client.py
satisfied 100000 requests in 1.41021 seconds (70911.42 reqs/s)
@jsheedy
jsheedy / Makefile
Created March 28, 2017 00:43
boring old cube GL
cube:
gcc boring_old_cube.cpp -framework OpenGL -framework GLUT -o boring_old_cube -w
@jsheedy
jsheedy / floyd_steinberg.py
Created May 25, 2016 23:46
floyd-steinberg dithering of a single channel uint8 image
def floyd_steinberg_dither(data):
result = data.astype(np.float64)
fs_matrix = np.array( (
( np.nan, np.nan, 7/16 ),
( 3/16, 5/16, 1/16),
), dtype=np.float64 );
fs_mask = np.array( (