Skip to content

Instantly share code, notes, and snippets.

@speezepearson
speezepearson / conway.py
Last active August 29, 2015 13:58
A Game of Life simulator in PyGUI.
#!/usr/bin/python
#
# A package that runs Conway's Game of Life. Simple GUI interface.
# Requires PyGUI and srptools (a small collection of libraries I wrote, available via my Github account, speezepearson.
#
import pickle
from GUI import (Application, View, Document, Window, Task, Frame,
Slider, Label, Button, ViewBase)
@speezepearson
speezepearson / position.py
Created April 7, 2014 23:30
A module to deal with n-dimensional positions and vectors.
import numpy
from math import cos, sin, atan2, sqrt, pi
def floateq(x, y, tolerance=1e-10):
"""Tells if two floats are within a given tolerance of each other."""
return abs(x-y) < tolerance
class Vector(numpy.ndarray):
"""An n-dimensional vector."""
def __new__(cls, coords, polar=False):
@speezepearson
speezepearson / raindrop.py
Created April 7, 2014 23:36
Simulates refraction of light inside a raindrop.
#!/usr/bin/python
#
from math import sin,cos,tan,sqrt,pi,asin, atan2 as atan
from GUI import View, Application, Window, Task
from GUI.Colors import Color
pale_blue = Color(.5,.5,1)
black = Color(0,0,0)
white = Color(1,1,1)
@speezepearson
speezepearson / logfloat.py
Created April 8, 2014 18:03
Stores floating-point numbers in log-space (intended for probability calculations).
from math import log, exp
from functools import total_ordering
@total_ordering
class LogFloat(object):
"""Represents a positive real number in log-space to avoid underflow."""
def __init__(self, normal=None, log_space=None):
if not ((normal is not None) ^ (log_space is not None)):
raise ValueError("must specify normal-space XOR log-space value")
'''To help debug, provides a decorator that logs every call to a function.
Basically,
>>> @log_calls
... def sqrt(x):
... if x < 0:
... raise ValueError("can only sqrt positive numbers")
... return x ** 0.5
...
@speezepearson
speezepearson / compressednetworkgraph.py
Last active May 21, 2016 22:01
Summarizes the ancestry of several given Git commits, using Graphviz.
#!/usr/bin/python
'''Summarizes the ancestry of several given commits, using Graphviz.
In a handwavey sense, it tries to show all of the "interesting" forks and merges in the ancestry of the commits you name.
Example:
$ python compressednetworkgraph.py origin/{master,environment,slice}
digraph G {
@speezepearson
speezepearson / chime-success.sh
Created April 2, 2017 02:20
Run a command, then chime success or failure.
#!/bin/bash
#
# Prefixed to a command, runs that command, and then makes a noise
# indicating whether the command passed or failed.
#
# Ubuntu only.
#
if eval "$@"; then
NAME='Mallet'
@speezepearson
speezepearson / watchman_context.py
Created July 22, 2019 01:30
Python context manager to [de]register Watchman triggers.
import contextlib
import json
import subprocess
from pathlib import Path
from typing import Any, Collection, Iterator, Mapping
JsonTrigger = Mapping[str, Any]
@contextlib.contextmanager
def with_triggers(dir: Path, triggers: Collection[JsonTrigger]) -> Iterator[None]:
@speezepearson
speezepearson / aiohttp-boilerplate.py
Last active May 20, 2020 01:10
aiohttp boilerplate
"""Demo usage: python --open-browser $THIS_FILE
"""
import argparse
import typing as t
from aiohttp import web
class Server:
def __init__(self) -> None:
pass
@speezepearson
speezepearson / proxy-boilerplate.nginx.conf
Last active April 6, 2020 06:50
nginx proxy boilerplate
# Put this in /etc/nginx/conf.d/SOME_NAME.conf
# It should get included because of the
# http {
# include /etc/nginx/conf.d/*.conf;
# }
# in your /etc/nginx/nginx.conf
server {
listen 80;
listen [::]:80;