def setUp(self): self.listOfMetros = {} #Where Key is the country's code self.listOfRoutes = [] #A list of the routes self.userQuery = "" #The buffer that holds the user input self.data = json.loads(open('../map_data.json').read() ) #The data that is read from the .json file self.cityData = self.data["metros"] #A list of the metro data self.routeData = self.data["routes"] #A list of the route data for x in range(0, len(self.cityData)): self.listOfMetros[self.cityData[x]['code']] = Metro( self.cityData[x]['code'], self.cityData[x]['name'], self.cityData[x]['country'], self.cityData[x]['continent'], self.cityData[x]['timezone'], self.cityData[x]['coordinates'], self.cityData[x]['population'], self.cityData[x]['region'], {}) for x in range(0, len(self.routeData)): route = Route(self.listOfMetros[self.routeData[x]['ports'][0]], self.listOfMetros[self.routeData[x]['ports'][1]], self.routeData[x]['distance']) returnRoute = Route( self.listOfMetros[self.routeData[x]['ports'][1]], self.listOfMetros[self.routeData[x]['ports'][0]], self.routeData[x]['distance']) self.listOfRoutes.append(route) #Add the route to the specific city self.listOfMetros[self.routeData[x]['ports'][0]].routes[ self.routeData[x]['ports'][1]] = route self.listOfMetros[self.routeData[x]['ports'][1]].routes[ self.routeData[x]['ports'][0]] = returnRoute
def setUp(self): ''' Sets up a simple Metro object to run tests on ''' self.metro = Metro("NYC", "New York City", "United States", "North America", 69, {"W": 69, "N": 69}, 6969696969, 455, {"testRoute": None})
def makeListOfMetros(self): ''' Compiles the dictionary of metros by iterating through the city_data list parsed from the json file ''' for x in range (0,len(self.city_data)): self.list_of_metros[self.city_data[x]['code']] = Metro(self.city_data[x]['code'], self.city_data[x]['name'], self.city_data[x]['country'], self.city_data[x]['continent'], self.city_data[x]['timezone'], self.city_data[x]['coordinates'], self.city_data[x]['population'], self.city_data[x]['region'], {})
def add_city(self): ''' Adds a city (node) to the graph ''' code = raw_input("What is the metro's code? ") name = raw_input("What is the metro's name? ") country = raw_input("Where country is the metro in? ") continent = raw_input("What continent is the metro in? ") timezone = raw_input("What timezone is the metro in? ") coordOneDir = raw_input("What is the first heading of the coordinate? ") coordOneVal = raw_input("What is the degree of the first heading? ") coordTwoDir = raw_input("What is the second heading of the coordinate? ") coordTwoVal = raw_input("What is the degree of the second heading? ") coordinates = {coordOneDir: coordOneVal, coordTwoDir: coordTwoVal} population = raw_input("What is the population of the metro? ") region = raw_input("What is the region of the metro? ") city = Metro(code, name, country, continent, timezone, coordinates, population, region, {}) self.list_of_metros[city.name] = city city.printInformation()
def test_graph(self): origin = "Hong Kong" travel_list = ["Taipei", "Hong Kong"] ''' Tests the calculations ''' self.assertEquals(524.55, self.CSAir.calculate_cost(origin, travel_list)) self.assertEquals(1614, self.CSAir.calculate_distance(origin, travel_list)) self.assertEquals(233.12, self.CSAir.calculate_time(origin, travel_list)) ''' Tests add city ''' city = Metro("ABC", "Jersey City", "United States", "North America", 3, { "N": 25, "W": 14 }, 41232312, 11, {}) self.CSAir.list_of_metros[city.name] = city self.assertEquals(True, self.CSAir.list_of_metros.has_key("Jersey City")) ''' Tests remove city ''' removedCity = self.CSAir.list_of_metros.pop("Jersey City") for route in removedCity.routes: destinationCity = removedCity.routes[route].destination for returnRoute in destinationCity.routes.keys(): if returnRoute == removedCity.code: del destinationCity.routes[returnRoute] self.CSAir.list_of_routes = [ route for route in self.CSAir.list_of_routes if not (route.destination.name == "Jersey City" or route.origin.name == "Jerey City") ] self.assertEquals(False, self.CSAir.list_of_metros.has_key("Jersey City"))