Skip to content

Instantly share code, notes, and snippets.

View thomasballinger's full-sized avatar

Tom Ballinger thomasballinger

View GitHub Profile
@thomasballinger
thomasballinger / gist:6979684
Created October 14, 2013 18:13
blackjack chart parsing
s = open('chart.txt')
s = s.read()
s
# OUT: ' Dealer\xe2\x80\x99s Up Card\nYour\nHand 2 3 4 5 6 7 8 9 10 A\n8 H H H H H H H H H H\n9 H D D D D H
# OUT: H H H H\n10 D D D D D D D D H H\n11 D D D D D D D D D H\n12 H H S S S H H H H H\n13
# OUT: S S S S S H H H H H\n14 S S S S S H H H H H\n15 S S S S S H H H H H\n16 S S S S S H
# OUT: H H H H\n17 S S S S S S S S S S\n \nA,2 H H H D D H H H H H\nA,3 H H H D D H H H H H\nA,4
# OUT: H H D D D H H H H H\nA,5 H H D D D H H H H H\nA,6 H D D D D H H H H H\nA,7 S D D D D S
# OUT: S H H H\nA,8 S S S S S S S S S S\nA,9 S S S S S S S S S S\n\nA,A P P P P P P P P P

==========================================

fmtstr annotates portions of strings with terminal colors and formatting str(yourstring) will be the string with [ANSI escape codes] (http://en.wikipedia.org/wiki/ANSI_escape_code) specifying color and other formatting to a terminal.

fmtstr.FmtStr

@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'
@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 / 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: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 / 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 / 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: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):
prices = {'lollipop': 200}
candies = 100
xp = 100
has_map = False
def needs_map(func):
def new_func():