Skip to content

Instantly share code, notes, and snippets.

@jtratner
Created July 16, 2012 00:40
Show Gist options
  • Select an option

  • Save jtratner/3119440 to your computer and use it in GitHub Desktop.

Select an option

Save jtratner/3119440 to your computer and use it in GitHub Desktop.
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)
def main():
import sys, os, imp
raise_errors = False
checker_name = len(sys.argv) > 2 and sys.argv[2] or "check_sudoku"
solver_name = len(sys.argv) > 3 and sys.argv[3] or "solve_sudoku"
check_sudoku, solve_sudoku = None, None
if len(sys.argv) < 2:
sys.exit("You need to pass the file name for check_sudoku")
else:
filepath = os.path.abspath(sys.argv[1])
mod_name = os.path.splitext(filepath)[0]
user_mod = imp.load_source(mod_name, filepath)
try:
check_sudoku = getattr(user_mod, checker_name)
except AttributeError:
msg = "Module has no function '%s'" % checker_name
if raise_errors:
raise AttributeError(msg)
print msg
try:
solve_sudoku = getattr(user_mod, solver_name)
except AttributeError:
msg = "Module has no function '%s'" % solver_name
if raise_errors:
raise AttributeError(msg)
print msg
# Then just run it, e.g. with do_test(check_sudoku, solve_sudoku)
if __name__ == '__main__':
# this makes it only parse args when run as main file
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment