Skip to content

Instantly share code, notes, and snippets.

View dcollien's full-sized avatar

David Collien dcollien

View GitHub Profile
@dcollien
dcollien / config.py
Last active September 12, 2018 08:18
ESP8266 Traffic Lights
import socket
import network
import time
CONTENT = b"""\
HTTP/1.0 200 OK
<!doctype html>
<html>
@dcollien
dcollien / classify_text.py
Last active August 22, 2018 20:47
Simple Text Classification using NLTK Naive Bayes and TextRank
import nltk
from summa.keywords import keywords
def get_features(text):
# get the top 80% of the phrases from the text, scored by relevance
return dict(keywords(text, ratio=0.8, split=True, scores=True))
def train_texts(classified_texts):
# process the training set
features = []
@dcollien
dcollien / config.py
Last active August 16, 2018 05:52
OAuth2.0 Sign-On for various providers, and retrieving user details: id, name, email, photo
class Config(object):
def __init__(self, **entries):
self.__dict__.update(entries)
self._entries = entries
def __repr__(self):
return "Config(%s)" % str(self._entries)
def __getattr__(self, value):
return None
const WebSocket = require('ws');
const PythonShell = require('python-shell');
const tmp = require('tmp');
const shortid = require('shortid');
const fs = require('fs');
const mkdirp = require('mkdirp');
// TODO clean up code, error handling, watchdog timer
const wss = new WebSocket.Server({ port: 8080 });
infinity = float("inf")
def argmin(seq, fn):
"""Return an element with lowest fn(seq[i]) score; tie goes to first one.
>>> argmin(['one', 'to', 'three'], len)
'to'
"""
best = seq[0]; best_score = fn(best)
for x in seq:
x_score = fn(x)
#!/usr/bin/env python
import pyrax
pyrax.set_setting('identity_type', 'rackspace')
pyrax.set_credentials(RACKSPACE_API_USER, RACKSPACE_API_KEY, region='SYD')
def server_list_generator(detailed=True, search_opts=None, limit=None):
servers = pyrax.cloudservers.servers.list(detailed=detailed,
search_opts=search_opts,
const ContrastTools = {
relLuminance(rgba) {
// http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
var rgb = rgba.slice();
for (var i = 0; i < 3; i++) {
var channel = rgb[i] / 255;
rgb[i] = channel < .03928 ? channel / 12.92 : Math.pow((channel + .055) / 1.055, 2.4);
}
@dcollien
dcollien / multipart.js
Last active March 30, 2024 18:15
Parse multi-part formdata in the browser
var Multipart = {
parse: (function() {
function Parser(arraybuf, boundary) {
this.array = arraybuf;
this.token = null;
this.current = null;
this.i = 0;
this.boundary = boundary;
}
@dcollien
dcollien / package.json
Last active February 9, 2017 16:14
HTTP-ify anything: A server which executes commands.
{
"name": "http-exec",
"version": "1.0.0",
"description": "Command Execution Server",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "David Collien",
<!DOCTYPE html>
<head>
<script>
var start_indicator = 'START';
var lineNum = 0;
var render = function(newData) {
var content = '';
newData = newData.replace(/^\s+|\s+$/g, '');
if (newData === '') return '';
var lines = newData.split('\\n');