Skip to content

Instantly share code, notes, and snippets.

@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)
@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 / 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 / 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 / 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 / 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 / testing_out_string_comparisons_for_inf_like_values.ipynb
Last active December 19, 2015 15:59
Perf-tests on Cython string comparisons
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jtratner
jtratner / Comparing cost of function calls.ipynb
Last active December 21, 2015 18:29
Comparing cost of function calls [ipython-notebook]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.