Skip to content

Instantly share code, notes, and snippets.

def get_transitions(n):
"""Return a dictionary of array spots to neighbors for an nxn grid"""
def neighbors(row, col):
return {at(to_row, to_col)
for to_row in around(row)
for to_col in around(col)
if (to_row, to_col) != (row, col)}
def at(row, col):
return row * n + col
def around(i):
@thomasballinger
thomasballinger / curry.py
Last active December 14, 2015 10:08
curryable functions in python - no kwargs yet
import inspect
from functools import partial
def curryable(func):
spec = inspect.getargspec(func)
nargs = len(spec.args)
if spec.varargs:
raise ValueError('Hard to make a multiarity function curryable')
if spec.defaults:
raise ValueError('Hard to make a function with default arguments curryable')
@thomasballinger
thomasballinger / gist:5104059
Created March 6, 2013 23:06
simplified WSGI server and app
import socket
# see wsgiref.simple_server for a real implementation of a WSGI server
def serve(app):
listener = socket.socket()
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind(('', 8080))
listener.listen(5)
while True:
s, addr = listener.accept()
@thomasballinger
thomasballinger / gist:5173558
Created March 15, 2013 22:17
My tmux config. I got it mostly from somewhere else, can't remember where.
new-session
set-option -g default-command "reattach-to-user-namespace -l bash"
unbind C-b
# I'm tempted to use C-a, but I want 'beginning of line'
set -g prefix C-q
set -g history-limit 1000000
@thomasballinger
thomasballinger / gist:5731729
Last active December 18, 2015 05:19
A section of my vimrc for hy
function! g:Refresh_hy_python_preview()
redir => message
redir END
let pyfile = substitute(bufname("%"), ".hy", "", "") . ".generated.py"
let errfile = substitute(bufname("%"), ".hy", "", "") . ".error"
exec "silent !~/hy/bin/hy2py % > " . pyfile . " 2> " . errfile . " || cat " . errfile . " > " . pyfile
call MyPreviewVSplit(pyfile)
set nomodified
"exec "silent !rm " . pyfile . " " . errfile
redraw!
@thomasballinger
thomasballinger / collide.js
Created June 10, 2013 18:12
Crap sphere collision with ray library
console.log("collide.js loaded");
function Ray(dx, dy, dz, x, y, z){
if (x === undefined){x = 0};
if (y === undefined){y = 0};
if (z === undefined){z = 0};
this.dx = dx;
this.dy = dy;
this.dz = dz;
this.x = x;
@thomasballinger
thomasballinger / gist:5783398
Created June 14, 2013 16:39
Examining __call__ of function objects - it's just a "method-wrapper," an object that wraps what is internally a method. I'm guessing the __call__ method is dynamically created when we ask for it, and there must be a short-circuit where function objects actually can get called with ().
>>> def foo(): return 1
>>> foo
<function foo at 0x10c5af488>
>>> foo.__call__
<method-wrapper '__call__' of function object at 0x10c5af488>
>>> foo.__call__.__call__
<method-wrapper '__call__' of method-wrapper object at 0x10c562c50>
>>> foo.__call__.__call__.__call__
<method-wrapper '__call__' of method-wrapper object at 0x10c64c8d0>
>>> foo.__call__.__call__.__call__()
@thomasballinger
thomasballinger / gist:5807241
Created June 18, 2013 17:01
Simple WSGI server and app
import socket
# see wsgiref.simple_server for a real implementation of a WSGI server
def serve(app):
listener = socket.socket()
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind(('', 8080))
listener.listen(5)
while True:
s, addr = listener.accept()
@thomasballinger
thomasballinger / gist:5879705
Created June 27, 2013 19:44
Iterative and recursive minimax
class Board(object):
"""
>>> Board().rows
((' ', ' ', ' '), (' ', ' ', ' '), (' ', ' ', ' '))
>>> print Board()
| |
-----
| |
-----
@thomasballinger
thomasballinger / gist:5910836
Created July 2, 2013 16:29
Possible Music api
from Music import *
s = Song(Note('f#5', 1.0/4), Note('f#5', 1.0/4), Note('g5', 1.0/4), Note('a6', 1.0/4))
s.play()
# plays the first four notes of "Ode to joy"