This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} |