Skip to content

Instantly share code, notes, and snippets.

@jtratner
jtratner / README.md
Last active December 19, 2015 09:39
Transparent virtualenv switching with Git and zsh
  • Source p, virtualenv.zsh, auto-workon.sh in your .zshrc
  • Put _p in your fpath. (e.g., /usr/share/zsh/site-functions)
  • Change PYTHONPROJECTS in virtualenv.zsh to the base directory for your Python projects.
  • Now you can use p to get to a project and switch to its virtualenv and p alone to deactivate your virtualenv entirely. Plus you now have tab completion into your projects - yay!
@jtratner
jtratner / trace.py
Last active December 18, 2015 10:39
Easy to slap-on trace function.
import inspect
import os
def trace(f, indent_level=[0]):
"""
slap this on a function as a decorator and
it'll show you what it's called with
and what it returns.
As an aside, if you put this on classes, be sure to
@jtratner
jtratner / Guardfile
Created June 7, 2013 23:33
Guardfile for pandas dev
# grab guard from https://github.com/guard/guard to use this file.
# Place it in the toplevel of the pandas repo and it'll just work for you.
def python_test(testpath)
base = "nosetests -A \"not slow\""
if testpath.nil?
puts "Running all non-slow tests"
return `#{base}`
else
puts "Running test on #{testpath}"
if !File.exists? testpath
@jtratner
jtratner / dynamic_blueprints.py
Last active November 26, 2019 14:24
Dynamic blueprints flask pseudocode
import os
PATH = path/to/my/blueprints/directory
BLUEPRINT = 'the_blueprint'
def import_file(path, name=None):
""" imports a file with given name and path """
# use the imp module to do actual imports
import imp
name = name or os.path.split(path)[-1].replace(".", "_")
@jtratner
jtratner / class_factory.py
Created July 30, 2012 01:39
Injecting global imports/locals
# In an actual use case, pkg1 and pkg2 would be packages,
# but to make this runnable I used classes
# to make a namespace instead (same idea regardless)
# make a dummy class so we can use it as a namespace
class Dummy(object): pass
pkg1, pkg2 = Dummy(), Dummy()
pkg1.average = lambda *args : sum(args) * 1.0 / len(args)
pkg2.get_lengths = lambda *args : map(len, args)
@jtratner
jtratner / barebones_loader.py
Created July 16, 2012 00:40
Barebones loader for sudoku checker and solver
import sys, os, imp
filepath = os.path.abspath(sys.argv[1])
mod_name = os.path.splitext(filepath)[0]
user_mod = imp.load_source(mod_name, filepath)
check_sudoku = user_mod.check_sudoku
solve_sudoku = user_mod.solve_sudoku
# Then just run it, e.g. with do_test(check_sudoku, solve_sudoku)
# 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]]
@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)
@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 / 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)