Skip to content

Instantly share code, notes, and snippets.

@npryce
npryce / publish-to-bintray
Created Apr 25, 2014
A shell script to publish JARs to the Bintray Maven repository
View publish-to-bintray
#!/bin/sh
set -e
if [ ! -f ~/.bintray-login ]
then
echo save your bintray.com <userid>:<api-key> in ~/.bintray-login
exit 1
fi
groupid=${1:?group-id}
@npryce
npryce / approval.py
Last active Aug 29, 2015
A quick-and-dirty Approval Testing tool for Python and Py.Test
View approval.py
import string
import os
from filecmp import cmp as file_contents_equal
def _extension(template_file, format):
if format is not None:
return "." + format
elif template_file is not None:
return os.path.splitext(template_file)[1]
@npryce
npryce / gpio-cleanup
Created Jul 17, 2014
Script to unexport all gpio device directories that are exported to userspace
View gpio-cleanup
#!/bin/bash
set -e
for f in $(find /sys/class/gpio/ -name 'gpio[0-9]*')
do
echo $(basename $f | cut -c 5-) | tee /sys/class/gpio/unexport
done
View er-count-java
#!/bin/sh
set -e
for f in `find . -name '*.class'`
do
echo $f `basename $f .class|grep -Eo '[[:lower:]][eo]r([^[:lower:]]|$)'|wc -l`
done | sort -rnk 2
@npryce
npryce / build
Created Oct 14, 2014
Pass Git version info to SBT
View build
#!/bin/sh
set -e
version=`git describe --tags --match 'v*' --always --dirty=-monkeypatched | cut -c 2-`
sbt -Dversion=$version "$@"
@npryce
npryce / MerchantOfVenice.java
Last active Aug 29, 2015
merchant-of-venice
View MerchantOfVenice.java
public interface Prickable {
void prick();
boolean isBleeding();
}
public interface Ticklable {
void tickle();
boolean isLaughing();
}
@npryce
npryce / LoggerTest.java
Last active Aug 29, 2015
How to create a Log4J Logger for unit testing Java code that logs
View LoggerTest.java
LoggingEvent loggedEvent = null;
private Logger capturingLogger() {
return new Logger("testing") {
{
level = Level.ALL;
repository = mock(LoggerRepository.class);
when(repository.isDisabled(anyInt())).thenReturn(false);
}
@npryce
npryce / Makefile
Last active Aug 29, 2015
Makefile to bootstrap and build a Purescript project
View Makefile
npm_bin:=$(shell npm bin)
psc=$(npm_bin)/psc
bower=$(npm_bin)/bower
outdir=target
all: $(outdir)/js/HelloWorld.js
$(outdir)/js/HelloWorld.js: src/HelloWorld.purs
@npryce
npryce / js-sync-to-async-cps-transform.md
Last active Aug 29, 2015
Refactoring to Asynchronous in JavaScript
View js-sync-to-async-cps-transform.md

Note: the latest version of this document is at http://natpryce.com/articles/000812.html

Here's some code that gets and uses a value from a synchronous call or built in data structure:

function to_be_refactored() {
    var x;
    ...
    x = get_x();
@npryce
npryce / react-for-mocha.js
Last active Sep 10, 2015
Testing react components with Mocha and NodeJS
View react-for-mocha.js
// React decides if it has a DOM on loading, so if we always need to
// initialise JSDOM before loading it.
var testdom = require('testdom');
testdom('<html><body></body></html>');
// But React hates you even more than that... it caches the rendered component
// in the DOM and if the same type of component with the same key is rendered at
// into the same element, then it uses the cached instance, not the just rendered
// instance. Even if the props are different. So the instance being tested will
// not be exercised.