def routeinfo(): cityList = QueryData.citylist() isValid = False while not isValid: print "Type the first valid city of new route." startcity = raw_input() isValid = startcity in cityList if startcity == "exit" or startcity == "quit" or startcity == "q" or startcity == "0": return legs = [] stops = [] # Gather dest city while (True): isValid = False while not isValid: endcity = raw_input( "Type the next valid city of route, or type exit to end") isValid = endcity in cityList or endcity == "exit" or endcity == "q" if not isValid: print "Wrong city. Not counted." if endcity == "exit" or endcity == "q": break stops.append(endcity) startcode = QueryData.nametocode(startcity) endcode = QueryData.nametocode(endcity) for route in GraphLibrary.route_list: if route.startportcode == startcode and \ route.endportcode == endcode: dist = route.distance legs.append(dist) stops.append(endcode) break startcity = endcity # after getting all the list print "Total Distance = " + str(totaldistance(legs)) + " km" print "Total Cost = $" + str(totalcost(legs)) print "Total Time = " + str(totaltime(legs, stops)) + " hrs"
def routeinfo(): cityList = QueryData.citylist() isValid = False while not isValid: print "Type the first valid city of new route." startcity = raw_input() isValid = startcity in cityList if startcity == "exit" or startcity == "quit" or startcity == "q" or startcity == "0": return legs = [] stops = [] # Gather dest city while(True): isValid = False while not isValid: endcity = raw_input("Type the next valid city of route, or type exit to end") isValid = endcity in cityList or endcity == "exit" or endcity == "q" if not isValid: print "Wrong city. Not counted." if endcity == "exit" or endcity == "q": break stops.append(endcity) startcode = QueryData.nametocode(startcity) endcode = QueryData.nametocode(endcity) for route in GraphLibrary.route_list: if route.startportcode == startcode and \ route.endportcode == endcode: dist = route.distance legs.append(dist) stops.append(endcode) break startcity = endcity # after getting all the list print "Total Distance = " + str(totaldistance(legs)) + " km" print "Total Cost = $" + str(totalcost(legs)) print "Total Time = " + str(totaltime(legs, stops)) + " hrs"
def removeroute(city, city2): code = QueryData.nametocode(city) code2 = QueryData.nametocode(city2) isValid = False for route in GraphLibrary.route_list: if code in route.startportcode and code2 in route.endportcode: isValid = True del route elif code2 in route.startportcode and code in route.startportcode: isValid = True del route if isValid: print "Remove of route between " + city + " and " + city2 + " is successful." else: print "Remove unsuccessful."
def removecity(city): del GraphLibrary.city_dictionary[city] # since the city is removed, we have to remove any route # which has the city as starting point code = QueryData.nametocode(city) for route in GraphLibrary.route_list: if code in route.startportcode: del route
def addnetwork(): while(True): cityList = QueryData.citylist() s = "Type the number for corresponding options:\n" \ "1. Remove a city\n" \ "2. Remove a route\n" \ "3. Add a city\n" \ "4. Add a route\n" \ "0. Quit" print s i = raw_input("Enter the number.") i = int(i) if i == 1: isValid = False while(not isValid): print "Type a valid city to remove, or type 0 or exit to quit" user_city = raw_input() isValid = user_city in cityList if user_city == "exit" or user_city == "quit" or user_city == "q" or user_city == "0": return removecity(user_city) elif i == 2: isValid = False while(not isValid): print "Type the first valid city of route." user_city = raw_input() isValid = user_city in cityList if user_city == "exit" or user_city == "quit" or user_city == "q" or user_city == "0": return isValid = False while(not isValid): city2 = raw_input("Type the second valid city of route.") isValid = city2 in cityList removeroute(user_city, city2) elif i == 3: newcode = raw_input("Type the new code of city.") newname = raw_input("Type the new name of city.") newcountry = raw_input("Type the new country of city.") newcont = raw_input("Type the new continent of city.") newzone = raw_input("Type the new timezone of city.") newcoord1 = raw_input("Type the Latitude of city.") newcoord2 = raw_input("Type the longitude of city.") newpop = raw_input("Type the population of city.") newdist = raw_input("Type the distance of city.") newreg = raw_input("Type the region of city.") newpop = int(newpop) newdist = int(newdist) newreg = int(newreg) if newpop > 0 and newdist > 0 and newreg > 0: addcity(newcode, newname, newcountry, newcont, newzone, newcoord1, newcoord2, newpop, newreg) else: print "Invalid info on population/distance/region." elif i == 4: isValid = False while not isValid: print "Type the first valid city of new route, or type exit to quit." city1 = raw_input() isValid = city1 in cityList if city1 == "exit" or city1 == "quit" or city1 == "q" or city1 == "0": return isValid = False while not isValid: city2 = raw_input("Type the second valid city of route.") isValid = city2 in cityList if city2 == "q" or city2 == "exit": return newdist = raw_input("Type the distance of city.") newdist = int(newdist) newstart = QueryData.nametocode(city1) newend = QueryData.codetoname(city2) if newdist > 0: addroute(newstart, newend, newdist) else: print "Invalid distance." elif i == 0: break else: print "Invalid Number!"