def main(args): """ ThoughtWorks Interactive Graph (TWIG) Command Line Interface (CLI) """ # Initialize a log file. This is a good habit for production-quality utilities/systems. if not os.path.isdir(os.path.join(os.getcwd(), '../log')): os.makedirs('../log') log_file = os.path.join(os.getcwd(), '../log', 'trace.log') if args.append: if args.debug: logging.basicConfig(filename=log_file, level=logging.DEBUG, filemode='a', format='%(asctime)s-%(msecs)d %(name)s %(levelname)s %(message)s', datefmt='%Y%m%d %H:%M:%S') else: logging.basicConfig(filename=log_file, level=logging.WARNING, filemode='a', format='%(asctime)s-%(msecs)d %(name)s %(levelname)s %(message)s', datefmt='%Y%m%d %H:%M:%S') else: if args.debug: logging.basicConfig(filename=log_file, level=logging.DEBUG, filemode='w', format='%(asctime)s-%(msecs)d %(name)s %(levelname)s %(message)s', datefmt='%Y%m%d %H:%M:%S') else: logging.basicConfig(filename=log_file, level=logging.WARNING, filemode='w', format='%(asctime)s-%(msecs)d %(name)s %(levelname)s %(message)s', datefmt='%Y%m%d %H:%M:%S') if args.verbose: logging.debug("Logging configured for %s", log_file) now = datetime.datetime.now() logging.debug("Starting ThoughtWorks coding-test-problem-1 command at %s", str(now)) # Initialize a class DiGraph object as per the CLI-supplied (TWEF-format) filename graph = DiGraph() if graph.import_twef(args.graph_filename): logging.debug("ABORT- graph.import_twef(%s) fails", args.graph_filename) return 0 logging.debug("graph.dump():\n%s", graph.dump()) edge_list = graph.edges() logging.debug("edge_list:\n%s", edge_list) # Delegate to the appropriate command handler (for this command line's query) if args.ikind.lower() == 'route': if not cmd_route(args, graph): logging.debug("cmd_route() succeeds") else: print 'Route command failed. Something went wrong! \nConsult {0}'.format(log_file) elif args.ikind.lower() == 'num_routes': if not cmd_num_routes(args, graph): logging.debug("cmd_num_routes() succeeds") else: print 'Num_routes command failed. Something went wrong! \nConsult {0}'.format(log_file) else: print 'Unrecognized command: {0}. No handler. Giving up.'.format(args.ikind.lower()) logging.debug("Logging complete for %s. Main() is done.", log_file) return 0
class TestDiGraph(unittest.TestCase): def setUp(self): print 'setUp w/prob1.twef' self.graph = DiGraph() if self.graph.import_twef('examples/prob1.twef'): assert False, "import_twef() fails" def testTrivial(self): self.failUnless(True) def testRouteDistanceABC(self): self.failIf(self.graph.distance('ABC') != 9) def testRouteDistanceAD(self): self.failIf(self.graph.distance('AD') != 5) def testRouteDistanceADC(self): self.failIf(self.graph.distance('ADC') != 13) def testRouteDistanceAEBCD(self): self.failIf(self.graph.distance('AEBCD') != 22) def testRouteDistanceAED(self): self.failIf(self.graph.distance('AED') >= 0) def testNRforCCwhereStopsLT3Cyclic(self): (result, routes) = self.graph.get_routes('CC', 'cyclic', 'stops', 'lt', '3') print "result == {0}".format(result) print "routes == {0}".format(routes) self.failIf(result != 0) self.failIf(set(routes) != set(['CEBC', 'CDC'])) def testNRforACwhereStopsEQ3Cyclic(self): (result, routes) = self.graph.get_routes('AC', 'cyclic', 'stops', 'eq', '3') print "result == {0}".format(result) print "routes == {0}".format(routes) self.failIf(result != 0) self.failIf(set(routes) != set(['ABCDC', 'ADCDC', 'ADEBC'])) def testRShortestAC(self): (result, distance, route) = self.graph.get_shortest_distance('A', 'C') print "result == {0}".format(result) print "distance == {0}".format(distance) print "route == {0}".format(route) self.failIf(result != 0) self.failIf(route != 'ABC') self.failIf(distance != 9) def testRShortestBB(self): (result, distance, route) = self.graph.get_shortest_distance('B', 'B') print "result == {0}".format(result) print "distance == {0}".format(distance) print "route == {0}".format(route) self.failIf(result != 0) self.failIf(route != 'BCEB') self.failIf(distance != 9) def testNRforCCwhereDistanceLT30Cyclic(self): (result, routes) = self.graph.get_routes('CC', 'cyclic', 'distance', 'lt', '30') print "result == {0}".format(result) print "routes == {0}".format(routes) self.failIf(result != 0) self.failIf(set(routes) != set(['CEBC', 'CEBCEBC', 'CEBCEBCEBC', 'CEBCDC', 'CDC', 'CDCEBC', 'CDEBC']))