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 dis | |
def filter_code(func, replacement): | |
name = func.__name__ | |
code = func.__code__.co_code | |
begin, do_fix = None, None | |
for i in dis.get_instructions(func): | |
if begin is not None: # every time except the first | |
end = i.offset |
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 os, shutil | |
from pathlib import Path | |
import pytest | |
@pytest.fixture(scope='session', autouse=True) | |
def session(tmp_path_factory): | |
try: | |
yield | |
finally: | |
shutil.rmtree(str(tmp_path_factory.getbasetemp())) |
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_adjacent(things, predicate, distance): | |
"""Make groups of things that are within distance of each other when the predicate is applied.""" | |
result = [] | |
for thing in things: | |
if result and abs(predicate(thing) - predicate(result[-1])) > distance: | |
yield result | |
result = [] | |
result.append(thing) | |
if result: | |
yield result |
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 itertools import product | |
from random import choice | |
collisions = { | |
'b': 'pv', 'c': 'js', 'd': 't' , 'f': 'pv', | |
'g': 'kx', 'j': 'cz', 'k': 'gx', 'l': 'r', | |
'm': 'n' , 'n': 'm' , 'p': 'bf', 'r': 'l', | |
's': 'cz', 't': 'd' , 'v': 'bf', 'x': 'gk', | |
'z': 'js' |
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 itertools import combinations, product | |
from operator import mul, div, add, sub | |
def intersperse(values, operations): | |
result = values[0] | |
for v, o in zip(values[1:], operations): result = o(result, v) | |
return result | |
def results(*values): | |
count = len(values) |
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 collections import Counter | |
from sys import argv | |
letters = Counter(argv[2:]) | |
letter_count = len(argv) - 2 | |
with file(argv[1]) as f: words = [word.strip() for word in f] # one word per line | |
longest = max(len(word) for word in words) | |
for i in reversed(xrange(min(longest, letter_count) + 1)): | |
possible_words = [ |