Skip to content

Instantly share code, notes, and snippets.

View thomasballinger's full-sized avatar

Tom Ballinger thomasballinger

View GitHub Profile
@thomasballinger
thomasballinger / gist:5915611
Created July 3, 2013 05:15
An object which appears to respect the semantics described at http://akaptur.github.io/blog/2013/06/28/side-effecting-assignment/
import gc
class Foo(object):
"""Object which appears to take the name of the first reference name
>>> Foo().name
>>> f = Foo()
>>> f.name
'f'
>>> g = f
@thomasballinger
thomasballinger / gist:5910849
Created July 2, 2013 16:30
example of music stuff
import subprocess
import os
def play_waveform(form):
s = 'echo %s | sox -r 8000 -b 8 -c 1 -t raw -s - -d' % form
print s
os.system(s)
#p = subprocess.Popen(['sox', '-r', '8000', '-b', '8', '-c', '1', '-t', 'raw', '-s', '-', '-d'], stdin=subprocess.PIPE)
@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"
@thomasballinger
thomasballinger / gist:5879705
Created June 27, 2013 19:44
Iterative and recursive minimax
class Board(object):
"""
>>> Board().rows
((' ', ' ', ' '), (' ', ' ', ' '), (' ', ' ', ' '))
>>> print Board()
| |
-----
| |
-----
@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: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 / 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: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 / 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: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()