Example #1
0
def handle_route_information_option(dict_of_edges, dict_of_cities, graph):
    """
    Handles queries asking for route information for a given complete route (inputted from the console)
    :param dict_of_edges:
    :param dict_of_cities:
    :param graph:
    :return: Nothing
    """
    print("ROUTE INFORMATION")
    print("------------------")
    print("Enter the subsequent cities and enter 'STOP' when you're done entering.\n")
    locations = []
    current_city = input()
    while not current_city.upper() == 'STOP':

        current_city = validate_city_name_or_code(current_city, dict_of_cities)
        if not current_city:
            return

        locations.append(current_city)
        current_city = input()

    print("Route:- ")
    for city in locations:
        print(city, end = "-->")
    print("||")

    Edge.getRouteInformation(dict_of_edges,graph,locations)
Example #2
0
 def test_remove_city_edges(self):
   dict_of_edges = prepare_test_data()
   print("\n\nREMOVE CITY EDGES\n----------------")
   print("\nBefore deletion:")
   print(dict_of_edges)
   Edge.remove_city_edges(dict_of_edges, 'ESS')
   print("\nAfter deletion:")
   print(dict_of_edges)
Example #3
0
def find_shortest_path(graph, source, destination, dict_of_edges):
    """
    Uses DIJKSTRA'S ALGORITHM TO GET THE SHORTEST PATH
    :param graph:
    :param source:
    :param destination:
    :param dict_of_edges: to later find out the route information
    :return:
    """
    dist = {}
    prev = {}
    unvisited = []
    found = False
    for key in graph:
        dist[key] = 999999
        prev[key] = "NA"
        unvisited.append(key)

    dist[source] = 0

    while len(unvisited) > 0:
        u = unvisited[0]
        for city in unvisited:
            if dist[city]<dist[u]:
                u = city
        unvisited.remove(u)
        if u==destination:
            found = True
            break

        for neighbour in graph[u]:
            neighbour_key = neighbour[0]
            neighbour_distance = neighbour[1]
            alt = dist[u] + neighbour_distance
            if alt < dist[neighbour_key]:
                dist[neighbour_key] = alt
                prev[neighbour_key] = u
    stack_of_path = []
    if found:
        previous = destination
        while not previous == source:
            stack_of_path.append(previous)
            # print(previous, end  = "<--")
            previous = prev[previous]
    else:
        print("No path between ends found!")

    if found:
        locations = []
        stack_of_path.append(source)
        print("\nShortest path is:\n")
        while len(stack_of_path) > 1:
            city_reached = stack_of_path.pop()
            print(city_reached, end="-->")
            locations.append(city_reached)
        print(destination)
        locations.append((destination))
        Edge.getRouteInformation(dict_of_edges,graph,locations)
Example #4
0
def handle_statistics_option(dict_of_edges, dict_of_cities, graph):
        """
        Handles all statistical queries
        :param dict_of_edges:
        :param dict_of_cities:
        :param graph:
        :return: Nothing
        """
        print("\n---------------------------------------------\n")
        print("Get Statistical Information about our Route Network")
        print("\n---------------------------------------------\n")

        print("\nCurrent options:")
        print("\n1. Get the longest single flight in the network")
        print("\n2. Get the shortest single flight in the network")
        print("\n3. Get the average distance of all the flights in the network")
        print("\n4. Get the biggest city (by population) served by CSAir")
        print("\n5. Get the smallest city (by population) served by CSAir")
        print("\n6. Get the average size (by population) of all the cities served by CSAir")
        print("\n7. Get a list of the continents served by CSAir and which cities are in them")
        print("\n8. Get CSAir's hub cities")

        stat_choice = input("\n\nPlease enter the number of your choice: ")

        if not str(stat_choice).isdigit() or int(stat_choice)>8 or int(stat_choice)<1:
            print("Invalid option. Please Try again.")
            input("Press Enter to Continue.")
            return

        if int(stat_choice) == 1:
            print(Edge.get_longest_flight(dict_of_edges))

        if int(stat_choice) == 2:
            print(Edge.get_shortest_flight(dict_of_edges))

        if int(stat_choice) == 3:
            print("Average Distance: " + str(Edge.get_average_distance(dict_of_edges)))

        if int(stat_choice) == 4:
            print(City.get_biggest_city(dict_of_cities))

        if int(stat_choice) == 5:
            print(City.get_smallest_city(dict_of_cities))

        if int(stat_choice) == 6:
            print("Average Population: " + str(City.get_average_population(dict_of_cities)))

        if int(stat_choice) == 7:
            print(City.make_dict_of_continents(dict_of_cities))


        if int(stat_choice) == 8:
            print("The Hub cities are:")
            hub_cities = get_hub_cities(graph)
            for city in hub_cities:
                print(dict_of_cities[city].name)
Example #5
0
import json
from unittest import TestCase
from csAir.Edge import Edge
from csAir.Graph import make_graph

__author__ = 'an5ra'
def parse_json(text_file_name):
    with open (text_file_name, "r") as myfile:
        data=myfile.read()
    return json.loads(data)
#parse the json
json_data = parse_json("mockJsonText.txt")

#construct dictionary of edges
dict_of_edges = Edge.make_dict(json_data)

#construct graph
graph = make_graph(dict_of_edges)

class TestMake_graph(TestCase):
  def test_make_graph(self):
    no_of_cities_made = len(graph)
    self.assertEqual(3,no_of_cities_made)
Example #6
0
 def test_get_shortest_flight(self):
   dict_of_edges = prepare_test_data()
   answer = Edge.get_shortest_flight(dict_of_edges)
   self.assertTrue("LON-ESS" in answer or "ESS-LON" in answer)
Example #7
0
 def test_get_longest_flight(self):
   dict_of_edges = prepare_test_data()
   answer = Edge.get_longest_flight(dict_of_edges)
   self.assertTrue("LON-NYC" in answer or "NYC-LON" in answer)
Example #8
0
def prepare_test_data():
  json_data = parse_json("mockJsonText.txt")
  dict_of_edges = Edge.make_dict(json_data)
  return dict_of_edges
Example #9
0
def handle_admin_option(json_data, dict_of_edges, dict_of_cities, graph):
    """
    Handles all admin stuff like add, delete, edit city/routes
    :param json_data:
    :param dict_of_edges:
    :param dict_of_cities:
    :param graph:
    :return: Nothing
    """
    print("\n---------------------------------------------\n")
    print("Admin Options")
    print("\n---------------------------------------------\n")
    print("\nCurrent options:")
    print("\n1. Add a City")
    print("\n2. Add a Route")
    print("\n3. Edit a City")
    print("\n4. Remove a City")
    print("\n5. Remove a Route")
    print("\n6. Go back to previous menu")

    admin_choice = input("\n\nPlease enter the number of your choice: ")

    if int(admin_choice) == 1:
        print("ADD A NEW CITY")
        print("---------------")
        code = input("Enter New City Code: (like NYC) ")
        name = input("Enter New City Name: (like New York) ")
        country = input("Enter New City Country: (like US) ")
        continent = input("Enter New City Continent: (like North America) ")
        timezone = input("Enter New City Time Zone: (like -5) ")
        coordinates = input("Enter New City Coordinates: (like {'N'' : 41, 'W' : 74}) ")
        population = input("Enter New City Population: (like 20000000) ")
        region = input("Enter New City Region: (like 3) ")

        if not str(population).isdigit() or int(population) < 0:
            print("Population invalid!")
            return

        # if not str(timezone).isnumeric():
        #     print("TimeZone invalid!")
        #     return

        if not str(region).isdigit() or int(region) < 0:
            print("Region invalid!")
            return

        City.add_city(dict_of_cities,code,name,country,continent,timezone,coordinates,population,region)
        jsonUtility.add_city_to_json_data(json_data,code,name,country,continent,timezone,coordinates,population,region)

    if int(admin_choice) == 2:
        print("ADD A NEW ROUTE")
        print("---------------")

        source = input("Enter Source Code or Name: (like NYC or New York City) ")
        source = validate_city_name_or_code(source, dict_of_cities)
        if not source:
            return

        destination = input("Enter Destination Code or Name: (like LON or London) ")
        destination = validate_city_name_or_code(destination, dict_of_cities)
        if not destination:
            return

        distance = input("Enter Distance (in mi): (like 12345) ")
        if not str(distance).isnumeric() or int(distance) < 0:
            print("Distance invalid!")
            return

        Edge.add_edge(dict_of_edges,source,destination,distance)
        jsonUtility.add_route_to_json_data(json_data,source,destination,distance)

    if int(admin_choice) == 3:
        print("EDIT CITY")
        print("---------------")
        city_to_edit = input("Enter City Code or Name: (like NYC or New York City) ")
        city_to_edit = validate_city_name_or_code(city_to_edit, dict_of_cities)
        if not city_to_edit:
            return
        print("For the following prompts, enter NA if you don't want to change.\n")
        name = input("Enter New City Name: (like New York) ")
        country = input("Enter New City Country: (like US) ")
        continent = input("Enter New City Continent: (like North America) ")
        timezone = input("Enter New City Time Zone: (like -5) ")
        coordinates = input("Enter New City Coordinates: (like {'N'' : 41, 'W' : 74}) ")
        population = input("Enter New City Population: (like 20000000) ")
        region = input("Enter New City Region: (like 3) ")

        city = dict_of_cities[city_to_edit]
        if not name == 'NA':
            city.name = name
        if not country == 'NA':
            city.country = country
        if not continent == 'NA':
            city.continent = continent
        if not timezone == 'NA':
            city.timezone = timezone
        if not coordinates == 'NA':
            city.coordinates = coordinates
        if not population == 'NA':
            city.population = population
        if not region == 'NA':
            city.region = region

        # change things in the json_data - by deleting old city and adding new one
        jsonUtility.delete_city_from_json_data(json_data,city_to_edit)
        jsonUtility.add_city_to_json_data(json_data,city.code,city.name,city.country,city.continent,city.timezone,city.coordinates,city.population,city.region)
        return

    if int(admin_choice) == 4:
        print("REMOVE CITY")
        print("---------------")
        city_to_remove = input("Enter City Code or Name: (like NYC or New York City) ")
        city_to_remove = validate_city_name_or_code(city_to_remove, dict_of_cities)
        if not city_to_remove:
            return
        jsonUtility.delete_city_from_json_data(json_data,city_to_remove)
        Edge.remove_city_edges(dict_of_edges, city_to_remove)
        City.remove_city(dict_of_cities,city_to_remove)

    if int(admin_choice) == 5:
        print("REMOVE ROUTE")
        print("---------------")
        edge_string = input("Enter SourceCode-DestinationCode: (like NYC-LON) ")
        result = Edge.remove_edge(dict_of_edges,edge_string)
        if not result:
            print("Either route format was invalid or route does not exist.")
        else:
            print("Route successfully deleted.")