Skip to content

Instantly share code, notes, and snippets.

# how exactly do conditions work??
from threading import Thread, Condition, Lock
import logging
logging.basicConfig()
log = logging.root
log.setLevel(logging.DEBUG)
mylock = Lock()
def wait_for_cond(c, msg):
@mfm24
mfm24 / png_bargraph.py
Created August 12, 2014 03:43
Small PNG bar graph generator using Python standard library
# -*- coding: utf-8 -*-
from __future__ import division
import struct
import zlib
def yield_block(header, data):
assert len(header)==4, 'header must be 4 bytes!'
# length:
yield struct.pack('! L', len(data))
# chunk type, 4 byte header
import itertools
nationalities = ["Brit", "Dane", "Norwegian", "German", "Swede"]
# assign arbitrary number to each nationality
Brit, Dane, Norwegian, German, Swede = range(len(nationalities))
# this returns an iterater over all the permutations of 5 numbers
choices = lambda: itertools.permutations(range(len(nationalities)))
next_to_ltor = lambda l, r: l - 1 == r
next_to = lambda l, r: abs(l - r) == 1
@mfm24
mfm24 / scrabble_pair_words.py
Last active August 29, 2015 14:13
Finds words composed of valid Scrabble subwords
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 10 18:34:00 2015
@author: matt
"""
from __future__ import print_function
from functools import wraps
from collections import defaultdict
path = "../localanagrampage/wordswithfriends.txt"
@mfm24
mfm24 / png_bargraph_progressive.py
Created June 4, 2015 22:25
Progressive png writer
# -*- coding: utf-8 -*-
# MFM 2015-06-04
# Trying to make this send data over wsgi with a
# generator to give a time-dependant graph
# goal is to acceot a generator like:
# def source():
# while True:
# time.sleep(1)
# yield random()
# and get updates live in a webpage.
# config_sectioner
#
# Treats a text file as a python file object, only
# inserting, adding or removing text in a marked off section.
# If the section doesn't exist it is appended to the file.
# Include information about the last date the file was written
# and by which programs. All blocks have a unique key, so that
# a file can contain multiple ConfigSections with different keys.
from __future__ import division, print_function
from cStringIO import StringIO
@mfm24
mfm24 / julia.py
Last active March 4, 2016 18:07
Creates Julia set using smaller sections
# copied from a ipython notebook
import numpy as np
from matplotlib import pyplot as plt
import time
import warnings
%matplotlib inline
def julia(c, extent, pixels):
t, l, b, r = extent
ny, nx = pixels
@mfm24
mfm24 / running_filter.py
Created March 20, 2017 19:21
Running filter (eg median, mean) using just numpy
# inspired by https://gist.github.com/bhawkins/3535131, but simpler
import numpy as np
def running_filter(data, k=7, op=np.median):
"""
Turns a 1-d source [a,b,c,d,e]
into a 2d [[d,e,... .]
[c,d,e,....]
[b,c,d,e...]
[a,b,c,d,e.]]
@mfm24
mfm24 / gist:e62ec5d50c672524107ca00a391e6104
Created November 15, 2019 19:44
Multiprocessing Logging Deadlock
# See eg https://codewithoutrules.com/2018/09/04/python-multiprocessing/
# this deadlocks (Process p never terminates)
import logging
import multiprocessing
import threading
import time
class SlowHandler(logging.Handler):
""" Waits 1 second, then prints """
def emit(self, record):
@mfm24
mfm24 / aoc2021_day15.py
Last active December 15, 2021 06:21
AoC2021 Day 15
import heapq
import numpy as np
x = np.array([[int(c) for c in l.strip()] for l in open('day15.txt')])
def score_map(mp):
h, w = mp.shape
def neighbours(y, x):
for (dy, dx) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
if 0 <= y + dy < h and 0 <= x + dx < w: