Skip to content

Instantly share code, notes, and snippets.

View rob-smallshire's full-sized avatar

Robert Smallshire rob-smallshire

View GitHub Profile
@rob-smallshire
rob-smallshire / LedThermometer.ino
Created November 17, 2012 19:46
Arduino based LED digital thermometer based on DS18B20
// An Arduino sketch for continually reading the temperature from
// a Dallas DS18B20 digital thermometer and turning on or off red,
// yellow or green LEDs to indicate the temperature range
// OneWire 2 library (an improved version of the original OneWire)
// http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <OneWire.h>
// Dallas Temperature Control library from
// http://milesburton.com/Dallas_Temperature_Control_Library
@rob-smallshire
rob-smallshire / client_controller.py
Created November 17, 2012 20:01
Arduino Web Service and Python Client for a Digital Thermometer with LED display
"""
A Python web service client which GETs temperatures over a web service API
from an Arduino based server and PUTs the status of LEDs back to the Arduino
to provide a visual temperature indication.
The intent of this system is to demonstrate how control logic can be moved
to remote systems which communicate with the Arduino over the network.
Pass the base url of the server e.g. "http://192.168.1.101" as the
only command line argument.
@rob-smallshire
rob-smallshire / mkvenv
Created January 20, 2013 12:49
A shell script to create a new Python 3.3 virtualenv, and then download and install distribute and pip into it.
#!/bin/bash
# Create a new Python 3.3 venv and configure it with pip
# Usage: source path/to/mkvenv <path>
set -e
pyvenv $1
cd $1
source bin/activate
curl -O http://python-distribute.org/distribute_setup.py
python distribute_setup.py
easy_install pip
@rob-smallshire
rob-smallshire / proxy.pac
Last active March 28, 2022 11:42
PAC file for routing iPlayer requests over a local SOCKS proxy
function FindProxyForURL(url, host)
{
if (shExpMatch(url, "*.bbc.co.uk/iplayer*")
|| shExpMatch(url, "*.bbc.co.uk/mediaselector*")
|| shExpMatch(url, "zaphod-live.bbc.co.uk.edgesuite.net/*")
|| shExpMatch(url, "bbcfmhds.vo.llnwd.net/*"))
{
return "SOCKS 127.0.0.1:8080";
}
else
@rob-smallshire
rob-smallshire / csv_to_dict.py
Created December 7, 2013 03:29
Read a CSV file into a dictionary of lists, using the column names in the first row as dictionary keys mapping to lists of numeric column data taken from subsequent rows in the CSV file.
import csv
with open('faithful.dat', newline='') as csvfile:
reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC)
names = next(reader)
columns = zip(*reader)
data = {name: column for name, column in zip(names, columns)}
@rob-smallshire
rob-smallshire / group_when.py
Last active August 29, 2015 14:04
Python group_when(predicate, iterable) function
def group_when(pred, iterable):
"""Yield groups (as lists), starting a new group when pred(item) is True.
The predicate is ignored for the first item in iterable, which always starts
a new group.
Args:
pred: A predicate function used to determine when a new group
should be started.
@rob-smallshire
rob-smallshire / transducer.py
Last active August 29, 2015 14:06
Transducers à la Clojure in Python
"""Transducers in Python
http://blog.cognitect.com/blog/2014/8/6/transducers-are-coming
"""
from functools import reduce
def compose(*fs):
"""Compose functions right to left.
@rob-smallshire
rob-smallshire / transducer2.py
Created September 26, 2014 06:57
Transducers in Python implemented as classes.
"""Transducers in Python
http://blog.cognitect.com/blog/2014/8/6/transducers-are-coming
"""
from abc import abstractmethod, ABCMeta
from collections import deque
from functools import reduce
@rob-smallshire
rob-smallshire / while_else.py
Last active August 29, 2015 14:09
This is the only quasi-legitmiate use I have found for the while..else construct in Python.
def pop_count_until(stack, predicate):
"""Count the number of items which need to be popped off a stack
for the top item to satisfy a predicate.
The stack argument is modified by this call. If the return value is
non-negative the top item on the stack will satisfy the predicate
on return.
Args:
stack: Any object supporting:
@rob-smallshire
rob-smallshire / diamond.py
Last active August 29, 2015 14:10
Print a diamond
def d(t):s=ord(t)-65;h=lambda i:'-'*(s-i)+chr(i+65)+'-'*i;m=[h(i)+h(i)[-2::-1] for i in range(s+1)];return'\n'.join(m+m[-2::-1])