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 / palindrome.py
Last active May 20, 2016 12:38
The longest palindromic primes of n-digits reproducing the first part of an integer sequence seen in a mural at Oslo Gardermoen airport
# The longest palindromic primes of n-digits
# reproducing the first part of an integer
# sequence seen in a mural at Oslo Gardermoen
# airport
from urllib.request import urlopen
def main():
with urlopen('https://oeis.org/A002385/b002385.txt') as response:
@rob-smallshire
rob-smallshire / scanner.ino
Created October 23, 2015 08:52
Arduino sketch for a slide scanner using a GAF 503 slide projector and a Canon 20D camera
int NUM_SLIDES = 50;
// Pin 2 has a slide projector connected
int PROJECTOR_ADVANCE_PIN = 2;
int CAMERA_FOCUS_PIN = 3;
int CAMERA_SHUTTER_PIN = 4;
int ARDUINO_LED_PIN = 13;
// the setup routine runs once when you press reset:
@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
@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 / 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.
@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 / 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 / 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 / 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 / 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.