Skip to content

Instantly share code, notes, and snippets.

View zahlman's full-sized avatar

Karl Knechtel zahlman

  • Toronto, Ont. Canada
View GitHub Profile
@zahlman
zahlman / x.py
Created June 26, 2022 04:39
Initial prototype: hack recursive functions to look themselves up in the function's pre-bound consts, instead of doing a global namespace lookup
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
@zahlman
zahlman / gist:7963824f7e2a103ab83e2d7d27da9c7a
Created August 16, 2020 16:33
Clean up pytest session completely after creating per-test 'environment' folders.
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()))
@zahlman
zahlman / gist:a5bc019cd42ca2650e56
Created September 25, 2014 18:00
Generator for grouping adjacent values
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
@zahlman
zahlman / gist:4108758
Created November 19, 2012 03:14
gimste simulation thingy
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'
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)
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 = [