def testCheapestRouteFile(self): """ Test the file input - cheapest route functionality. Return list of [name, result, message] """ # Test the file input function. Uses sampleinput.csv file which contains 7 routes. # Sums all cheapest route costs together and compares them to expected total. test_name = "File Input - Cheapest Route" test_result = "Fail" try: test_file = "sampleinput.csv" test_output = "sampleoutput.csv" expected_total_cost = 124230 file_routes = FileHandler.getRouteInputFile(test_file) if not file_routes: test_message = "Error reading routes from file. Make sure {file} still exists.".format(file=test_file) else: results, errors = self.agent.cheapestRoutesFromFile(file_routes, printout=False, fileout=test_output) if errors: test_message = errors[0] elif len(results) != 7: test_message = "Incorrect number of results" else: total_cost = 0 for itin in results: total_cost += itin.cheapest_cost if total_cost == expected_total_cost: test_result = "Pass" test_message = "Calculated total cost of 7 routes: {total_cost} ({expected_total_cost} expected). Output file {test_output} successfully created.".format(**locals()) except Exception as e: test_message = "Exception {e}".format(e=e) return test_name, test_result, test_message
def fileInputCheapestRoutes(): print("---- CHEAPEST ROUTES BY FILE INPUT -----") routes = None while not routes: filename = input("Filename: ") routes = FileHandler.getRouteInputFile(filename) if not routes: print("File not found or not a correctly formatted .csv.") print("Make sure input file is located in the /input/ directory") outfile = input("Output filename (optional): ") print() agent.cheapestRoutesFromFile(routes, fileout=outfile)
def testInvalidInputFiles(self): """ Test the file input - cheapest route function with invalid and non-existant filenames """ # Testing filenames for files that don't exist and filenames with invalid characters. # Trying to calculate the cheapest routes with these files should only return errors and no results. test_name = "Invalid/Non-exist Filename Test" test_result = "Fail" try: invalid_name = "af\\4**!//?.pptx" non_existant_file = "thisfilenothere.csv" file_routes_inv = FileHandler.getRouteInputFile(invalid_name) file_routes_ne = FileHandler.getRouteInputFile(non_existant_file) results_inv, errors_inv = self.agent.cheapestRoutesFromFile(file_routes_inv, printout=False, fileout=False) results_ne, errors_ne = self.agent.cheapestRoutesFromFile(file_routes_ne, printout=False, fileout=False) if errors_inv[0] == "Invalid file" and errors_ne[0] == "Invalid file": test_result = "Pass" test_message = "Testing File Input for: {invalid_name} and {non_existant_file}: Invalid file error successful.".format(**locals()) else: test_message = "Testing File Input for: {invalid_name} and {non_existant_file}: Error not raised properly.".format(**locals()) except Exception as e: test_message = "Exception {e}".format(e=e) return test_name, test_result, test_message
def benchmarkCheapestRoutes(self): """ Benchmark file input - cheapest route function with 1000 itineraries. Print out total time taken. """ # Used to monitor performance and optimize efficiency. Averages around 7 seconds for me. print("Benchmarking Cheapest Routes...") input_file = "random1000.csv" file_routes = FileHandler.getRouteInputFile(input_file) if not file_routes: print("Test file ", input_file, " not found. Cannot benchmark.") return start = time.time() self.agent.cheapestRoutesFromFile(file_routes, printout=False) end = time.time() duration = end - start print("Cheapest routes for 1000 itineraries calculated in: {duration:.3f} seconds".format(**locals()))
def fileCheapestRoutes(self, event=None): """ Calculate cheapest routes from csv file entered in text entry. Display results as requested. """ self.textoutput.set("Processing...") self.resultsout.delete(1.0, tkinter.END) # Delete any previous results self.update() fileinput = self.file_in.get() fileoutput = self.file_out.get() # None if left blank printoption = self.print_option.get() file_routes = FileHandler.getRouteInputFile(fileinput) if not file_routes: self.textoutput.set("Filename not found") return result_itins, errors = self.agent.cheapestRoutesFromFile(file_routes, printout=False, fileout=fileoutput) if result_itins: # If calculations were successful self.results_avail = True num_results = len(result_itins) num_errors = len(errors) self.textoutput.set("Successfully processed {0} routes. ({1} errors)".format(num_results, num_errors)) if printoption: self.showResults() for itin in result_itins: # Prints results in box to the right. Uses custom tags for font color. self.resultsout.insert(tkinter.END, "{name:15s}".format(name=itin.name)) self.resultsout.insert(tkinter.END, "\t{route}\n".format(route=itin.route.getSimpleString()), "route") self.resultsout.insert(tkinter.END, "Cheapest Route:") self.resultsout.insert(tkinter.END, "\t{cheap_route:50s}\n".format(cheap_route=itin.cheapest_route.getSimpleString()), "route") self.resultsout.insert(tkinter.END, "Cost: € ") self.resultsout.insert(tkinter.END, str(itin.cheapest_cost) + "\n\n", "number") if errors: self.resultsout.insert(tkinter.END, "\nErrors: {num_errors} with routes.\n-------------\n".format(num_errors=num_errors)) for error in errors: self.resultsout.insert(tkinter.END, error + "\n") else: # If not displaying results, remove results box. self.resultsout.grid_forget() self.scroll.grid_forget() self.hideresults.grid_forget() self.showresults.grid_forget() else: self.results_avail = False self.textoutput.set("Error processing routes") self.update()
def testFileWithErrors(self): """ Test the file input - cheapest route functionality with a file that contains errors. Return list of [name, result, message] """ test_name = "Cheapest Route File w/ Errors" test_result = "Fail" try: test_file = "random50errors.csv" expected_errors = 5 file_routes = FileHandler.getRouteInputFile(test_file) if not file_routes: test_message = "Error reading routes from file. Make sure {file} still exists.".format(file=test_file) else: results, errors = self.agent.cheapestRoutesFromFile(file_routes, printout=False, fileout=False) num_errors = len(errors) num_results = len(results) if num_errors == expected_errors: test_result = "Pass" test_message = "Processed input file {test_file}. {num_results} routes calculated successfully. {num_errors} errors ({expected_errors} expected).".format(**locals()) except Exception as e: test_message = "Exception {e}".format(e=e) return test_name, test_result, test_message
def __init__(self, currency_file = currency_fn, country_file = country_fn, airport_file = airport_fn): from inputoutput import FileHandler self.currencies = FileHandler.buildCurrencyDict(currency_file) self.countries = FileHandler.buildCountryDict(country_file, self.currencies) self.airports = FileHandler.buildAirportDict(airport_file, self.countries)
def generateRandomInput(): print("---- GENERATE RANDOM INPUT FILE -----") filename = input("Filename: ") num_people = int(input("Number of people: ")) FileHandler.generateRandomInput(filename, num_people, travel_db)