Skip to content

Instantly share code, notes, and snippets.

@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: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
prices = {'lollipop': 200}
candies = 100
xp = 100
has_map = False
def needs_map(func):
def new_func():
@thomasballinger
thomasballinger / gist:5984855
Created July 12, 2013 14:25
generator and decorator examples
def by_token(fileobj):
for line in fileobj:
for token in line.split():
yield token
import os
def open_file(func):
def newfunc(filename):
@thomasballinger
thomasballinger / test18.py
Created July 16, 2013 19:29
Python threads and file io
import sys
import tty
import Queue
import threading
import time
tty.setcbreak(sys.stdin)
events = Queue.Queue()
@thomasballinger
thomasballinger / gist:6254383
Created August 16, 2013 23:29
Autoextending numpy array proxy
import numpy
#TODO do something cooler and more robust like
#http://stackoverflow.com/a/9059858/398212
class AutoExtending(object):
"""Numpy array wrapper that automatically extends rows down
>>> a = AutoExtending(10, 14)
>>> a.shape
@thomasballinger
thomasballinger / gist:6254441
Created August 16, 2013 23:41
2d char array + 3d formatting array -based printing to screen
"""Another way of formatting text: 2d char array and 3d formatting array"""
ON_GREY = 40
ON_RED = 41
ON_GREEN = 42
ON_YELLOW = 43
ON_BLUE = 44
ON_MAGENTA = 45
ON_CYAN = 46
ON_WHITE = 47
@thomasballinger
thomasballinger / history.py
Created August 16, 2013 23:58
bpython frontend history module that I'm using bpython stuff for now
"""implementations of readline control sequences to do with history
Implementing these similar to how they're done in bpython:
* never modifying previous history entries
* always appending the executed line
in the order of description at http://www.bigsmoke.us/readline/shortcuts
"""
import logging
@thomasballinger
thomasballinger / gist:6285452
Created August 20, 2013 18:40
Caching, overloading __getitem__ etc, memoization
import pickle
import glob
cache = {}
def fib(x):
if x in cache:
return cache[x]
if x < 2: return 1
@thomasballinger
thomasballinger / curriedgetitem.py
Created September 10, 2013 05:19
Currying dictionary assignment in Python
class ProxiedGetitem(object):
"""Dictionary whose keys are 2-element tuples; if one element tuple used,
the __getitem__ method is curried
>>> d = ProxiedGetitem()
>>> d # doctest: +ELLIPSIS
<__main__.ProxiedGetitem object at 0x...>
>>> d[1,2] = 'a'
>>> d[1,2]
'a'