Skip to content

Instantly share code, notes, and snippets.

Douglas Duhaime duhaime

Block or report user

Report or block duhaime

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View socket_stream.py
import sys, socket, redis, json
# config
stream = {'host': '127.0.0.1', 'port': 6000} # streaming data host / port
r = redis.Redis(host='127.0.0.1', port=6379) # redis instance host / port
# consume data from stdin and publish to redis on localhost
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((socket.gethostbyname(stream['host']), stream['port'])) # host, port
@duhaime
duhaime / signal.py
Created Jun 20, 2019
Signal Listener
View signal.py
# class to gracefully handle sigint / sigterm (SO 18499497)
class SignalListener:
kill_now = False
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self,signum, frame):
self.kill_now = True
@duhaime
duhaime / server.py
Created Jun 20, 2019
Flask + Redis + background process in while loop
View server.py
from flask import Flask, jsonify, request, send_from_directory
import redis, celery, sys, socket, os, json
# config
port = 6000 # port on which data streams
# app
app = Flask(__name__, static_url_path='')
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'
@duhaime
duhaime / index.html
Last active Jun 20, 2019
Simple Flask Server
View index.html
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>title</title>
<style>
* {
margin: 0;
padding: 0;
}
@duhaime
duhaime / knn.py
Created Jun 17, 2019
Find KNN for Gensim Model
View knn.py
from annoy import AnnoyIndex
import json
import os
def build_ann_index(model):
'''Build an ANN model and persist to disk for faster vector similarity queries'''
words = list(model.wv.vocab.keys()) # list of strings, one per word
idx_to_word = {str(idx): i for idx, i in enumerate(words)} # d[word] = word_idx in words
dims = model.wv[words[0]].shape[0] # number of dimensions in each input vector
@duhaime
duhaime / unproject.js
Created Jun 2, 2019
three.js unproject unprojection event to world coords
View unproject.js
function getWorldCoords(e) {
// identify the x,y coords in canavs that got event
var rect = canvas.getBoundingClientRect(),
x = e.clientX - rect.left,
y = e.clientY - rect.top;
// convert x,y to clip space in canvas:
// canvas coords from top left in clockwise order
// (-1,1), (1,1), (-1,-1), (1, -1)
var mouse = new THREE.Vector3();
mouse.x = ( (x / canvas.clientWidth ) * 2) - 1;
@duhaime
duhaime / index.html
Last active May 27, 2019
Draw a Neural Network
View index.html
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>Drawing a Neural Network</title>
<script src='https://cdnjs.cloudflare.com/ajax/libs/d3/5.9.2/d3.min.js'></script>
</head>
<body>
<script>
@duhaime
duhaime / 1812_Page_03.jpg
Last active May 24, 2019
Extracting illustrations with picsnip
@duhaime
duhaime / index.html
Last active May 15, 2019
Minimal Frame Buffer Object (Three.js)
View index.html
<html>
<head>
<style>
html, body { width: 100%; height: 100%; background: #000; }
body { margin: 0; overflow: hidden; }
canvas { width: 100%; height: 100%; }
</style>
<meta charset='UTF-8'>
</head>
<body>
You can’t perform that action at this time.