Skip to content

Instantly share code, notes, and snippets.

@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 {
'''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 / 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")
@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 / 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 / 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)