def removeCity(cityName): #Check if the city is in the city dictionary if cityName in QueryingData.cityDicationary: cityCode = UserQuerying.nameToCode(cityName) continentInfo = QueryingData.cityDicationary[cityName].continent #Remove from the cityDicationary del QueryingData.cityDicationary[cityName] #Remove the city from the take off port for route in QueryingData.routeList: if route.takeoffPortCode == cityCode: UserQuerying.minusHubCity(cityCode) QueryingData.routeList.remove(route) #Remove the city from the land port for route in QueryingData.routeList: if route.landPortCode == cityCode: UserQuerying.minusHubCity(route.takeoffPortCode) QueryingData.routeList.remove(route) else: print 'Cannot find ' + cityName
def addCity(): ''' This function will add a new city and all the relevant data about that city into existing cities. All the data is from user input ''' cityName = raw_input("Please enter city name: \n") cityCode = raw_input("Please enter city code: \n") country = raw_input("Please enter country: \n") continent = raw_input("Please enter continent: \n") timezone = raw_input("Please enter timezone: \n") latitude = raw_input("Please enter latitude: \n") # Type should be: S 30 longitude = raw_input("Please enter longitude: \n") # Type should be: W 30 while True: population = raw_input("Please enter population: \n") #Check if the input is valid if int(population) > 0: break region = raw_input("Please enter region: \n") newCoordinates = UserQuerying.formCoordinate(latitude, longitude) # Get the right format for the coordinates newCityInfo = CityInfo.CityInfo(cityCode, cityName, country, continent, int(timezone), newCoordinates, int(population), int(region)) QueryingData.cityDicationary[cityName] = newCityInfo # Add the new class into city Dictionary QueryingData.codeToName[cityCode] = cityName # Add the code and the name into code to name dictionary UserQuerying.cityToContinent(cityName, continent) # Add the city into right continent
def calculateDistance(cityList): distance = 0 counter = len(cityList) i = 0 while counter > 1: nextCity = cityList[i+1] flyToCityDic = UserQuerying.getFlyToCity(cityList[i]) distanceToNextCity = flyToCityDic[nextCity] distance += distanceToNextCity counter -= 1 i += 1 return distance
def calculateTime(cityList): flyingTime = 0.0 layoverTime = 0.0 acceleration = 750.0*750/(2*200) counter = len(cityList) i = 0 while counter > 1: nextCity = cityList[i+1] flyToCityDic = UserQuerying.getFlyToCity(cityList[i]) distanceToNextCity = flyToCityDic[nextCity] # calculate the flying time if distanceToNextCity >= 400: accTime = 200.0*2/750 decTime = accTime cruisingTime = (distanceToNextCity - 400.0)/750 flyingTime = flyingTime + accTime + decTime + cruisingTime else: accTime = math.sqrt(2*distanceToNextCity/acceleration) flyingTime = flyingTime + 2*accTime # calculate the layover time if i != 0: cityCode = UserQuerying.nameToCode(cityList[i]) hubNumber = QueryingData.hubCity[cityCode] waitingTime = (120.0 - hubNumber*10)/60 layoverTime = layoverTime + waitingTime counter -= 1 i += 1 return (flyingTime + layoverTime)
def removeCity(): ''' This function will remove a city from existing cities, and update all the relevant data ''' cityName = raw_input("Please enter the city you want to remove: \n") if cityName in QueryingData.cityDicationary: #Check if the input city is valid or not cityCode = UserQuerying.nameToCode(cityName) del QueryingData.continent[cityName] #Remove the city from the continent del QueryingData.cityDicationary[cityName] #Remove from the cityDicationary #Remove relevant routes including the remove city deleteRoute = [route for route in QueryingData.routeList if route.takeOffPortCode == cityCode] QueryingData.routeList[:] = [route for route in QueryingData.routeList if route.landPortCode != cityCode and route.takeOffPortCode != cityCode] for route in deleteRoute: UserQuerying.minusHubCity(route.takeOffPortCode) UserQuerying.minusHubCity(route.landPortCode) else: print 'Cannot find ' + cityName #Invalid input
def calculateCost(cityList): cost = 0 counter = len(cityList) i = 0 while counter > 1: nextCity = cityList[i+1] flyToCityDic = UserQuerying.getFlyToCity(cityList[i]) distanceToNextCity = flyToCityDic[nextCity] rate = 0.35 - 0.05*i if rate <= 0: rate = 0 cost = cost + distanceToNextCity*rate counter -= 1 i += 1 return cost
def testRemoveRoute(self): UserQueryingExpand.removeRoute() source = "Hong Kong" dest = "Shanghai" self.assertFalse(dest in UserQuerying.getFlyToCity(source))
def getShortestPath(): ''' This function will find the shortest path between two cities And calculate the distance, cost and consuming time about the Shortest path ''' graph = {} pathList = None source = raw_input("Please enter the source city: \n") destionation = raw_input("Please enter the destination city: \n") for city in QueryingData.cityDicationary: graph[city] = UserQuerying.getFlyToCity(city) pathList = DijkstraAlgorithm.shortestPath(graph, source, destionation) print "Ths shortest path: " for city in pathList: print '>' + city print "" distance = CostHelper.calculateDistance(pathList) cost = CostHelper.calculateCost(pathList) time = CostHelper.calculateTime(pathList) print "Total distance is: " + str(distance) + " kilometers" print "Total cost is: $" + str(cost) print "Total consuming time is: " + str(time) + " hours"
def removeRoute(): ''' This function will remove a particular route, And update all the relevant data ''' sourcePortName = raw_input("Please enter the source port you want to remove: \n") destPortName = raw_input("Please enter the destination port you want to remove: \n") sourcePortCode = UserQuerying.nameToCode(sourcePortName) destPortCode = UserQuerying.nameToCode(destPortName) for route in QueryingData.routeList: #Remove source to destination if route.takeOffPortCode == sourcePortCode and route.landPortCode == destPortCode: QueryingData.routeList.remove(route) UserQuerying.minusHubCity(sourcePortCode) break for route in QueryingData.routeList: #Remove destination to source if route.takeOffPortCode == destPortCode and route.landPortCode == sourcePortCode: QueryingData.routeList.remove(route) UserQuerying.minusHubCity(destPortCode) break
def addRoute(): ''' This function will add a new route between two existing cities And update all the relevant data ''' sourcePortName = raw_input("Please enter the source port name: \n") destPortName = raw_input("Please enter the destination port name: \n") while True: distance = raw_input("Please enter the distance: \n") #Check if the input is valid if int(distance) > 0: break sourcePortCode = UserQuerying.nameToCode(sourcePortName) destPortCode = UserQuerying.nameToCode(destPortName) newSourceToDestination = RouteInfo.RouteInfo(sourcePortCode, destPortCode, int(distance)) newDestinationToSource = RouteInfo.RouteInfo(destPortCode, sourcePortCode, int(distance)) QueryingData.routeList.append(newSourceToDestination) #Append source to destination to route list QueryingData.routeList.append(newDestinationToSource) #Append destination to source to route list UserQuerying.addHubCity(sourcePortCode) #update hub city data UserQuerying.addHubCity(destPortCode) #update hub city data
def testShorestFlight(self): key = UserQuerying.shortestFlight() self.assertEqual(key, 334)
def testLongestFlight(self): key = UserQuerying.longestFlight() self.assertEqual(key, 12051)
def editCity(): ''' This function will take user's input about a existing city And call relative editing function to edit relative data ''' cityName = raw_input("Please enter the city you want to edit") if cityName in QueryingData.cityDicationary: print pstr n = raw_input("Please enter a number: \n") if int(n) == 1: UserQuerying.changeCityName(cityName) if int(n) == 2: UserQuerying.changeCityCode(cityName) if int(n) == 3: UserQuerying.changeCountry(cityName) if int(n) == 4: UserQuerying.changeContinent(cityName) if int(n) == 5: UserQuerying.changeTimezone(cityName) if int(n) == 6: UserQuerying.changeCoordinates(cityName) if int(n) == 7: UserQuerying.changePopulation(cityName) if int(n) == 8: UserQuerying.changeRegion(cityName) else: print cityName + " does not exist."
''' Created on Feb 27, 2013 @author: shengchao ''' from DataParse import Parser from DataParse import QueryingData from DataParse import UserQueryingExpand from DataParse import UserQuerying from DataParse import DijkstraAlgorithm Parser.Parser() graph = {} for city in QueryingData.cityDicationary: graph[city] = UserQuerying.getFlyToCity(city) print DijkstraAlgorithm.shortestPath(graph, "Los Angeles", "London")
def testBiggestCity(self): key = UserQuerying.biggestCity() self.assertEqual(key, 34000000)
""" from DataParse import Parser from DataParse import UserQuerying from DataParse import UserQueryingExpand queryString = "1.Type 1 to get all the cities that CSAir flies to.\n2.Type 2 to get special information about a special city.\n3.Type 3 to get statical information about CSAir's route network.\n4.Type 4 to map your route.\n5.Type 5 to Add a new City.\n6.Type 6 to Remove a City.\n7.Type 7 to Add a new Route.\n8.Type 8 to remove a Route.\n9.Type 9 to get Route Information.\n10.Type 10 to edit an Existing City.\n11.Type 11 to Saving the route to the disk.\n12.Type 12 to get shortest path between two country.\n13.Type 13 to exit.\n" Parser.Parser() while True: print "-----------------------------------------------------------------------" print queryString n = raw_input("Please choose a number: \n") if n.strip() == "1": cityList = UserQuerying.getCityList() print "Here is all cities that CSAir flies to..." for city in cityList: print city print "\n" elif n.strip() == "2": cityName = raw_input("Please type querying city name: \n") if UserQuerying.getQueryCity(cityName) == False: print "Sorry, CSAir does not fly to " + cityName print "\n" elif n.strip() == "3": UserQuerying.getStaticalInfomation() print "\n"
def testAddRoute(self): UserQueryingExpand.addRoute() source = "Champaign" dest = "Shanghai" self.assertTrue(dest in UserQuerying.getFlyToCity(source))