Skip to content

Instantly share code, notes, and snippets.

@jtratner
jtratner / makegraphs.py
Created May 2, 2012 01:34
DefaultColor(defaultdict)
matplotcolors= ['b','g','r','c','m','y','k']
defaultcolorlist = ["#00af64","#0b61a4","#ff9200","#ff4900","#9c02a7"]
defaultcolorlist.extend(matplotcolors)
from collections import defaultdict
class DefaultColor(defaultdict):
""" defaultdict that returns sequential elements of a list(if not specified, a color) each time it is
called Note that if input list is len(1) it will always return the same
value"""
def __init__(self,colorlst = defaultcolorlist):
@jtratner
jtratner / ipython-error
Created May 2, 2012 05:27
ipython_gtk error - no gdk backend found
$ python --colors=Linux --pylab=gtk
WARNING: Couldn't start log: [Errno 2] No such file or directory: u'$HOME/.ipython/ipython_logs/ipython_log.py'Python 2.7.2+ (default, Oct 4 2011, 20:03:08)
Type "copyright", "credits" or "license" for more information.
IPython 0.12 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
[TerminalIPythonApp] Error in enabling GUI event loop integration:
@jtratner
jtratner / rest_to_markdown.vim
Created May 4, 2012 06:39
Checks your vim regexp escapes -- save yourself tons of hassle!
command! -range -nargs=* CheckRegexpEscapes :call CheckRegexpEscapes(<f-args>)
" call with either no arguments or a:1 for escape character +
" an argument for each character to be escaped
" e.g. CheckRegexpEscapes('\\','(',')','_') ->
" checks that you put in '\\' behind each of '(',')','_',')'
" and prompts you to change it
" always asks to substitute
function! CheckRegexpEscapes(...)
" NonEscapedCharRegexp:
@jtratner
jtratner / new-post.zsh
Created May 16, 2012 09:06
Create and edit an octopress post from anywhere (and avoid zsh globbing issues)
#!/bin/zsh
# export BLOG="/path/to/your/octopress/install"
# handles globbing weirdness with zsh + use it from anywhere
# use: new-post "This is my exciting new post!!!"
# set the blog variable to your octopress directory
# TODO: fix this so it handles double-titling
function new-post () {
@jtratner
jtratner / example-source.rst
Created May 16, 2012 14:49
Jekyll-rst escapes pullquote html instead of passing it through
layout title date author comments
post
Testing pullquotes
2012-05-16 09:11
Jeffrey Tratner
true

So this is me, starting out.

@jtratner
jtratner / portmanteau_compare
Created June 5, 2012 16:46
Comparing portmanteau
My answer:
8588 function calls in 0.015 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.015 0.015 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 __future__.py:48(<module>)
1 0.000 0.000 0.000 0.000 __future__.py:74(_Feature)
@jtratner
jtratner / memcache_mixin.py
Created June 28, 2012 22:29
Memcache mixin
class DBCacheMixin(object):
""" mixin for datastore objects with cache/key lookups, inheriting classes must define:
MCBASE, (optionally NAMESPACE -- though handled internally) """
NAMESPACE = None
MCBASE = "NONE_"
@classmethod
def get_cache(cls, key, base=None, group=None):
base = (base or cls.MCBASE) + (group or cls.GROUP)
r = memcache.get(key=base + str(key), namespace=cls.NAMESPACE)
@jtratner
jtratner / sudoku.py
Created July 12, 2012 00:48
Sudoku Solver/Checker - Udacity CS258
def get_subgrids(three_rows):
""" splits given set of three rows into subgrids"""
return zip(*[(row[0:3], row[3:6], row[6:]) for row in three_rows])
def check_sudoku(sudoku):
try:
def check_row(row):
""" checks that the row contains 1 and only 1 element from 1 to 9 and
that the total count == 9"""
counts = [0 for x in range(10)]
for i in range(9):
@jtratner
jtratner / sudoku_fuzzer.py
Created July 12, 2012 03:26
Fuzzer for CS258 Sudoku Checker
def fuzzer_for_sudoku_checker(sudoku_checker):
""" given `sudoku_checker` a sudoku checker function, attempts to ferret out common issues with checking
for valid input. Raises AssertionError s if the function fails to conform to expectations"""
try:
illegal = (0, [], range(10), [range(10), range(10), 0, range(10), 1, range(10), range(10), range(10), range(10)])
valid_row = range(1, 10)
invalid = ([[1]*9] * 9, [valid_row * 8] + [range(2, 11)])
for s in illegal:
res = sudoku_checker(s)
assert res is None, "Failed to detect that {s} was illegal. Returned {res} instead of `None`".format(s=s, res=res)
# Use a different solved board to generate different tests.
valid = [[5,3,4,6,7,8,9,1,2],
[6,7,2,1,9,5,3,4,8],
[1,9,8,3,4,2,5,6,7],
[8,5,9,7,6,1,4,2,3],
[4,2,6,8,5,3,7,9,1],
[7,1,3,9,2,4,8,5,6],
[9,6,1,5,3,7,2,8,4],
[2,8,7,4,1,9,6,3,5],
[3,4,5,2,8,6,1,7,9]]