Skip to content

Instantly share code, notes, and snippets.

@richmarr
richmarr / index.js
Created August 3, 2011 09:04
Export all JS modules in a directory as submodules
/*
I found it handy to reduce the amount of manual require() lines in a big
project by grouping small modules together like below.
If you put this code into "index.js" then it'll pick up any other
JS modules in that directory and expose them as sub-modules.
- Any exports in *this* module would be myPackage.exportName
- Any exports in other modules would be myPackage.moduleName.exportName
@richmarr
richmarr / threadpool.groovy
Created April 30, 2012 10:36
Convenience defer technique for passing execution to a thread pool in Groovy
import java.util.concurrent.Callable
import java.util.concurrent.Executors
def THREADS = 2
def pool = Executors.newFixedThreadPool(THREADS)
def done = 0
def defer = { closure ->
pool.submit( closure as Callable )
}
@richmarr
richmarr / greenhouse.ino
Last active February 17, 2017 12:41
Arduino code my Dad wrote to automate his greenhouse lamps
int lightPin0 = 0;
int lightPin1 = 1;
int relayPin = 2;
int night = 1000;
int sunny = 8000;
float filter = 0.0;
void setup() {
pinMode(relayPin,OUTPUT);
Serial.begin(9600);
@richmarr
richmarr / gist:7747115
Created December 2, 2013 09:34
Example test that reads every Express route and verifies that every value pulled from `req.query`, `req.param` or `req.body` has been cleaned using `express-validator` and that each mother at least calls to `req.validationErrors()` to inspect what's wrong. This example assumes that all routes are exposed as a single package, e.g. `routes.user.ha…
/**
*/
var routes = require('../routes'),
assert = require('assert');
describe('routes', function(){
it("should exist", function(done){
assert(routes);
@richmarr
richmarr / algo.js
Created November 5, 2013 10:33
Quick Node.js speed test for different crypto algorithms exposed through OpenSSL. Don't forget, speed == good but speed !== good
var crypto = require('crypto'),
algos = crypto.getCiphers(),
testString = 'something to match, something to match, something to match, something to match, something to match '+algos.join("#"),
password = '12345asdfgh',
ciphertextEncoding = 'binary',
cleartextEncoding = 'utf8',
iterations = 1000;
algos.forEach(function( algorithm ){
@richmarr
richmarr / gist:3944934
Created October 24, 2012 08:56 — forked from mattb/gist:3888345
Some pointers for Natural Language Processing / Machine Learning

Here are the areas I've been researching, some things I've read and some open source packages...

Nearly all text processing starts by transforming text into vectors: http://en.wikipedia.org/wiki/Vector_space_model

Often it uses transforms such as TFIDF to normalise the data and control for outliers (words that are too frequent or too rare confuse the algorithms): http://en.wikipedia.org/wiki/Tf%E2%80%93idf

Collocations is a technique to detect when two or more words occur more commonly together than separately (e.g. "wishy-washy" in English) - I use this to group words into n-gram tokens because many NLP techniques consider each word as if it's independent of all the others in a document, ignoring order: http://matpalm.com/blog/2011/10/22/collocations_1/

@richmarr
richmarr / ka_bnet_numpy.py
Created March 28, 2012 10:24 — forked from kohlmeier/ka_bnet_numpy.py
Bayes net example in Python with Khan Academy data
#!/usr/bin/env python
from numpy import asmatrix, asarray, ones, zeros, mean, sum, arange, prod, dot, loadtxt
from numpy.random import random, randint
import pickle
MISSING_VALUE = -1 # a constant I will use to denote missing integer values
def impute_hidden_node(E, I, theta, sample_hidden):
@richmarr
richmarr / streaming-xml-standardiser.groovy
Created February 21, 2012 12:56
Streaming XML standardiser implementation in Groovy using NUX. Allows feeds to be processed via DB configuration rather than XSL
package richmarr.xml.example
import org.xml.sax.InputSource
import nu.xom.*
import nux.xom.io.*
import nux.xom.xquery.*
// this would normally be pulled from a database
def config = [
item : "/xpath/to/item/elements",
fields : [
$.fn.parsley.defaults = {
// basic data-api overridable properties here..
inputs: 'input, textarea, select' // Default supported inputs.
, excluded: 'input[type=hidden], :disabled' // Do not validate input[type=hidden] & :disabled.
, trigger: false // $.Event() that will trigger validation. eg: keyup, change..
, animate: true // fade in / fade out error messages
, animateDuration: 300 // fadein/fadout ms time
, focus: 'first' // 'fist'|'last'|'none' which error field would have focus first on form validation
, validationMinlength: 3 // If trigger validation specified, only if value.length > validationMinlength
, successClass: 'has-success' // Class name on each valid input