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 / 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])
@rob-smallshire
rob-smallshire / conj.py
Created January 9, 2015 15:00
A Python 3.4 conj function for mutable and immutable sequences and strings.
from functools import singledispatch
from collections import Sequence, MutableSequence
@singledispatch
def conj(sequence, item):
raise TypeError("conj() not supported for type '{}'".format(type(sequence)))
@conj.register(MutableSequence)
def _(sequence, item):
sequence.append(item)
@rob-smallshire
rob-smallshire / test_extended_textual_header.py
Last active August 29, 2015 14:19
A Hypothesis 1.2 strategy for creating multiline strings.
PRINTABLE_ASCII_RANGE = (32, 127)
def multiline_ascii_encodable_text(min_num_lines, max_num_lines):
"""A Hypothesis strategy to produce a multiline Unicode string.
Args:
min_num_lines: The minimum number of lines in the produced strings.
max_num_lines: The maximum number of lines in the produced strings.
@classmethod
def bisecting_planes(cls, p, q):
"""Determine the bisector of two planes.
Args:
p, q: The two planes to be bisected.
Returns:
The plane bisecting planes p and q.
@rob-smallshire
rob-smallshire / invariant.py
Created May 18, 2015 15:16
A decorator for checking that class invariants are established and maintained.
import functools
def invariant(predicate):
"""Create a class decorator which checks a class invariant.
Args:
predicate: A callable to which, after every method invocation,
the object on which the method was called will be passed.
The predicate should evaluate to True if the class invariant
@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