- Source
p
,virtualenv.zsh
,auto-workon.sh
in your.zshrc
- Put
_p
in your fpath. (e.g.,/usr/share/zsh/site-functions
) - Change
PYTHONPROJECTS
invirtualenv.zsh
to the base directory for your Python projects. - Now you can use
p
to get to a project and switch to its virtualenv andp
alone to deactivate your virtualenv entirely. Plus you now have tab completion into your projects - yay!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(".", "_") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |