def city_arrival_time(): #t = Train("Express One", 50, 100) #c1 = City("City 1",0,3,300) #j = Journey(t,[c1],200) #print(j.city_arrival_time(c1)) # 200 #t = Train("Express One", 50, 10) #c1 = City("City 1",0,3,300) #c2 = City("City 2",0,3003,300) #j = Journey(t,[c1,c2],200) #print(j.city_arrival_time(c2)) # 800 #t = Train("Express One", 50, 10) #c1 = City("City 1",100,3,120) #c2 = City("City 2",78,300,300) #j = Journey(t,[c1,c2],1000) #print(j.city_arrival_time(c1)) #,1000 ##print(j.city_arrival_time(c2)) # 1149 t = Train("Express One", 50, 10) c1 = City("City 1", 100, 3, 120) c2 = City("City 2", 78, 300, 300) c3 = City("City 3", 50, 100, 240) j = Journey(t, [c1, c2, c3], 1000) print(j.city_arrival_time(c3)) # 1469
def CityInJourney(): t = Train("Express One", 50, 100) c1 = City("City 1", 5, 10, 600) cities = [c1, City("City 2", 2, 3, 900), City("City 3", 0, 0, 300)] j = Journey(t, cities, 200) r = j.city_in_journey(c1) print(r) print(j.city_in_journey("City 1"))
def AddDest(): t = Train("Express One", 50, 100) c1 = City("City 1", 5, 10, 600) c2 = City("City 2", 2, 3, 900) j = Journey(t, [c1, c2], 200) print(j) ct = City("City 3", 8, 4, 440) j.add_destination(ct) print(j)
def TotalJourneyDistance(): #t = Train("Express One", 50, 100) #c1 = City("City 1",0,3,300) #j = Journey(t,[c1],200) #print(j.total_journey_distance()) # 0 t = Train("Express One", 50, 100) c1 = City("City 1", 0, 3, 300) c2 = City("City 2", 0, 8, 300) j = Journey(t, [c1, c2], 200) print(j.total_journey_distance()) # 5
def NewJourney(): #MyTrain = Train("Norman", 100, 22) #(name, max passengers, fps) #Journey1 = Journey(MyTrain, ["xxx", "yyy"], 4000) #print(Journey1) #MyTrain1 = "" #Journey2 = Journey(MyTrain1, ["aaa", "bbb"], 4000) #print(Journey2) t = Train("Express One", 50, 100) j = Journey(t, [], 0) print(j.__str__())
def build_full_no_change_graph(self): """ For each pair of stations in the supplied list, create a Journey object and save the journey times in a list. Arguments: - `station_list`: the list of stations created by load_stations(). """ from Journey import Journey for i in range(len(self.my_station_list.get_list())): self.journey_time_matrix.append([]) for j in range(len(self.my_station_list.get_list())): if i != j: print "Journey:",\ self.my_station_list.get_list()[i][1].get_name(), \ "to",\ self.my_station_list.get_list()[j][1].get_name() self.this_journey = Journey(self.my_station_list.get_list()[i][1],\ self.my_station_list.get_list()[j][1],\ "20130801",\ "0800") self.this_journey.read_api() self.journey_time_matrix[i].append(\ self.this_journey.get_no_change_journey_time()) self.this_journey.cleanup_files() else: self.journey_time_matrix[i].append(0)
def check_timetable(timetables: dict, last_check: int) -> int: file_name = f"tt-{NAME}" try: stat_info = os.stat(file_name) if stat_info.st_mtime > last_check: last_check = int(time.time()) # Clear all the old entries timetables.clear() with open(file_name, "r") as in_file: # We are not interested in the header info lines = in_file.readlines()[1:] for line in lines: line = line.strip("\n") info = line.split(",") journey = Journey(line, info[-1], info[0], info[-2]) if journey.destination not in timetables.keys(): timetables[journey.destination] = [] timetables[journey.destination].append(journey) except FileNotFoundError: print(f"{file_name} for node {NAME} not found, exiting") sys.exit(0) return last_check
def CheckJourney(): #t = Train("Express One", 50, 100) #c1 = City("City 1",5,10,600) #c2 = City("City 2",2,3,900) #c3 = City("City 3", 0,0,300) #c4 = City("City 4", 12,4,1000) #c5 = City("City 5", 12,4,1000) #j = Journey(t,[c1,c2,c3,c4],200) #print(j.check_journey_includes(c5,c2)) t = Train("Express One", 50, 100) c1 = City("City 1", 5, 10, 600) c2 = City("City 2", 2, 3, 900) c3 = City("City 3", 0, 0, 300) c4 = City("City 4", 12, 4, 1000) j = Journey(t, [c1, c2, c3, c4], 200) print(j.check_journey_includes(c3, c2))
def JourneyTestSTR(): t = Train("Express One", 50, 100) c1 = City("City 1", 5, 10, 600) c2 = City("City 2", 2, 3, 900) c3 = City("City 3", 0, 0, 300) c4 = City("City 4", 12, 4, 1000) j = Journey(t, [c1, c2, c3, c4], 200) #MyTrain = Train("Norman", 324, 65) #(name, max passengers, fps) #Journey1 = Journey(MyTrain, ["aaa", "bbb", "ccc", "ddd", "eee", "fff"], 4060) print(j)
def AllPassengersAccommodated(): #t = Train("Express One", 50, 100) #c1 = City("City 1",0,3,300) #j = Journey(t,[c1],200) #print(j.all_passengers_accommodated([5],[5])) # False #no passenger to be unloaded #t = Train("Express One", 50, 100) #c1 = City("City 1",0,3,300) #j = Journey(t,[c1],200) #print(j.all_passengers_accommodated([0],[100])) # False) #too many to load #t = Train("Express One", 50, 10) #c1 = City("City 1",0,3,300) #j = Journey(t,[c1],200) #print(j.all_passengers_accommodated([0],[30])) # ,True) t = Train("Express One", 50, 10) c1 = City("City 1", 0, 3, 300) c2 = City("City 2", 78, 300, 300) j = Journey(t, [c1, c2], 200) print(j.all_passengers_accommodated([0, 40], [30, 10])) #,False)
def total_journey_time(): t = Train("Express One", 50, 100) c1 = City("City 1", 0, 3, 300) j = Journey(t, [c1], 200) print(j.total_journey_time()) #,300) t = Train("Express One", 50, 10) c1 = City("City 1", 0, 3, 300) c2 = City("City 2", 0, 3003, 300) j = Journey(t, [c1, c2], 200) print(j.total_journey_time()) #,900)
def get_journeys(departure_point, arrival_point, departure_date, region): mode = 'journeys?' starting_from = 'from=' + departure_point going_to = 'to=' + arrival_point at_time = 'datetime=' + departure_date url_final = ROOT_URL + region + mode + starting_from + '&' + going_to + '&' + at_time data = requests.get( url=url_final, auth=(get_tokens('navitia'), '')) #url = recherche vers navitia et auth = notre username/pwd print(url_final) data = data.json()["journeys"][ 0] #on stock dans la variable data la partie du json intéréssante pour la suite du processus #je crée un objet journey et je lui donne les données json journey = Journey( data["sections"][1]["from"] ["name"], #je récupère le nom du point de départ data["sections"][1]["to"] ["name"], #je récupère le nom du point d'arrivée data[ "requested_date_time"], #je récupère la date demandé par l'utilisateur data["departure_date_time"], #je récupère la date de départ du trajet data["arrival_date_time"], #je récupère la date d arrivée du trajet data["duration"], #je récupère le temps du trajet data["sections"][1]["display_informations"] ["physical_mode"], #je récupère le type de transport en commun data["sections"][1]["display_informations"] ["name"], #je récupère le nom du trajet data["sections"][1]["display_informations"] ["network"], #je récupère le nom du réseau de transport data["sections"][1]["display_informations"] ["trip_short_name"], #je récupère l'ID du trajet data["sections"][1]["stop_date_times"] ) #je récupère le tableau de tout les arrets du trajet return journey
def aa_test(): MyTrain = Train("Chuggington", 100, 22) #(name, max passengers, fps) MyCity1 = City("Philly", 9, 20, 50) name = MyTrain.name MyTrain.num_passengers = 60 #LoadPassengers = MyTrain.load_passengers(5) #TimeTravel = MyTrain.time_to_travel(2200) UnloadPassengers = MyTrain.unload_passengers(50) #print(name) #print(MyTrain) # print(LoadPassengers) #print(TimeTravel) #print(TrainCapacityException(5,"full")) #print(TrainCapacityException(15,"empty")) #print(UnloadPassengers) #print(MyCity.distance_to_city("fairfax")) #MyCity2 = City("Vegas", 9, 22, 100) #print(MyCity1.__eq__(MyCity2)) #MyTrain1 = "" Journey1 = Journey(MyTrain1, "gfagfgagf", 4000)
def build_part_graph(self): """ Start from what has already been put into xml file and continue, writing to the xml file after each journey. This allows getting the full graph (eventually) despite SSH timeout and broken pipes etc. """ from Journey import Journey import xml.etree.ElementTree as ET self.my_tree = ET.parse('TubeJourneyTimesGraph.gexf') self.my_gexf = self.my_tree.getroot() self.my_meta = self.my_gexf.find('{https://www.gexf.net/1.2draft}meta') self.my_graph = self.my_gexf.find('{https://www.gexf.net/1.2draft}graph') self.my_nodes = self.my_graph.find('{https://www.gexf.net/1.2draft}nodes') self.my_edges = self.my_graph.find('{https://www.gexf.net/1.2draft}edges') self.id_list = [] for edge in self.my_edges: self.id_list.append(int(edge.get('id'))) if len(self.id_list) != 0: self.max_id = self.id_list.pop() else: self.max_id = 0 self.journey_time_matrix.append([]) self.add_xml_node(0) self.i_start = self.max_id / len(self.my_station_list.get_list()) self.j_start = self.max_id % len(self.my_station_list.get_list()) for cols in range(self.i_start+1): self.journey_time_matrix.append([]) for rows in range(self.j_start+1): self.journey_time_matrix[cols].append([]) # finish current origin: for j in range(self.j_start+1, len(self.my_station_list.get_list())): if (self.i_start != j)\ and not(self.i_start==94 and j==95) and not(self.i_start==95 and j==94)\ and not(self.i_start==102 and j==103) and not(self.i_start==103 and j==102)\ and not(self.i_start==102 and j==104) and not(self.i_start==104 and j==102)\ and not(self.i_start==103 and j==104) and not(self.i_start==104 and j==103)\ and not(self.i_start==171 and j==172) and not(self.i_start==172 and j==171)\ and not(self.i_start==198 and j==199) and not(self.i_start==199 and j==198): print "Journey:",\ self.my_station_list.get_list()[self.i_start][1].get_name(), \ "to",\ self.my_station_list.get_list()[j][1].get_name() self.this_journey = Journey(self.my_station_list\ .get_list()[self.i_start][1],\ self.my_station_list\ .get_list()[j][1],\ "20130801",\ "0800") self.this_journey.read_api() self.journey_time_matrix[self.i_start].append(\ self.this_journey.get_no_change_journey_time()) self.this_journey.cleanup_files() else: self.journey_time_matrix[self.i_start].append(0) if self.journey_time_matrix[self.i_start][j] != 0: self.add_xml_edge(self.i_start,j) # then do the rest: for i in range(self.i_start + 1, len(self.my_station_list.get_list())): self.journey_time_matrix.append([]) self.add_xml_node(i) for j in range(len(self.my_station_list.get_list())): if (i != j)\ and not(i==94 and j==95) and not(i==95 and j==94)\ and not(i==102 and j==103) and not(i==103 and j==102)\ and not(i==102 and j==104) and not(i==104 and j==103)\ and not(i==103 and j==104) and not(i==104 and j==103)\ and not(i==171 and j==172) and not(i==172 and j==171)\ and not(i==198 and j==199) and not(i==199 and j==198): print "Journey:",\ self.my_station_list.get_list()[i][1].get_name(), \ "to",\ self.my_station_list.get_list()[j][1].get_name() self.this_journey = Journey(self.my_station_list\ .get_list()[i][1],\ self.my_station_list\ .get_list()[j][1],\ "20130801",\ "0800") self.this_journey.read_api() self.journey_time_matrix[i].append(\ self.this_journey.get_no_change_journey_time()) self.this_journey.cleanup_files() else: self.journey_time_matrix[i].append(0) if self.journey_time_matrix[i][j] != 0: self.add_xml_edge(i,j)
class GraphBuilder(object): """ Build a graph of the tube network to export to GEXF format. """ def __init__(self, station_list): """ Intilise object. """ self.journey_time_matrix = [] self.my_station_list = station_list def build_full_graph(self): """ For each pair of stations in the supplied list, create a Journey object and save the journey times in a list. Arguments: - `station_list`: the list of stations created by load_stations(). """ from Journey import Journey for i in range(len(self.my_station_list.get_list())): self.journey_time_matrix.append([]) for j in range(len(self.my_station_list.get_list())): if i != j: print "Journey:",\ self.my_station_list.get_list()[i][1].get_name(), \ "to",\ self.my_station_list.get_list()[j][1].get_name() self.this_journey = Journey(self.my_station_list.get_list()[i][1],\ self.my_station_list.get_list()[j][1],\ "20130801",\ "0800") self.this_journey.read_api() self.journey_time_matrix[i].append(\ self.this_journey.get_journey_time()) self.this_journey.cleanup_files() else: self.journey_time_matrix[i].append(0) def build_full_no_change_graph(self): """ For each pair of stations in the supplied list, create a Journey object and save the journey times in a list. Arguments: - `station_list`: the list of stations created by load_stations(). """ from Journey import Journey for i in range(len(self.my_station_list.get_list())): self.journey_time_matrix.append([]) for j in range(len(self.my_station_list.get_list())): if i != j: print "Journey:",\ self.my_station_list.get_list()[i][1].get_name(), \ "to",\ self.my_station_list.get_list()[j][1].get_name() self.this_journey = Journey(self.my_station_list.get_list()[i][1],\ self.my_station_list.get_list()[j][1],\ "20130801",\ "0800") self.this_journey.read_api() self.journey_time_matrix[i].append(\ self.this_journey.get_no_change_journey_time()) self.this_journey.cleanup_files() else: self.journey_time_matrix[i].append(0) def build_part_graph(self): """ Start from what has already been put into xml file and continue, writing to the xml file after each journey. This allows getting the full graph (eventually) despite SSH timeout and broken pipes etc. """ from Journey import Journey import xml.etree.ElementTree as ET self.my_tree = ET.parse('TubeJourneyTimesGraph.gexf') self.my_gexf = self.my_tree.getroot() self.my_meta = self.my_gexf.find('{https://www.gexf.net/1.2draft}meta') self.my_graph = self.my_gexf.find('{https://www.gexf.net/1.2draft}graph') self.my_nodes = self.my_graph.find('{https://www.gexf.net/1.2draft}nodes') self.my_edges = self.my_graph.find('{https://www.gexf.net/1.2draft}edges') self.id_list = [] for edge in self.my_edges: self.id_list.append(int(edge.get('id'))) if len(self.id_list) != 0: self.max_id = self.id_list.pop() else: self.max_id = 0 self.journey_time_matrix.append([]) self.add_xml_node(0) self.i_start = self.max_id / len(self.my_station_list.get_list()) self.j_start = self.max_id % len(self.my_station_list.get_list()) for cols in range(self.i_start+1): self.journey_time_matrix.append([]) for rows in range(self.j_start+1): self.journey_time_matrix[cols].append([]) # finish current origin: for j in range(self.j_start+1, len(self.my_station_list.get_list())): if (self.i_start != j)\ and not(self.i_start==94 and j==95) and not(self.i_start==95 and j==94)\ and not(self.i_start==102 and j==103) and not(self.i_start==103 and j==102)\ and not(self.i_start==102 and j==104) and not(self.i_start==104 and j==102)\ and not(self.i_start==103 and j==104) and not(self.i_start==104 and j==103)\ and not(self.i_start==171 and j==172) and not(self.i_start==172 and j==171)\ and not(self.i_start==198 and j==199) and not(self.i_start==199 and j==198): print "Journey:",\ self.my_station_list.get_list()[self.i_start][1].get_name(), \ "to",\ self.my_station_list.get_list()[j][1].get_name() self.this_journey = Journey(self.my_station_list\ .get_list()[self.i_start][1],\ self.my_station_list\ .get_list()[j][1],\ "20130801",\ "0800") self.this_journey.read_api() self.journey_time_matrix[self.i_start].append(\ self.this_journey.get_no_change_journey_time()) self.this_journey.cleanup_files() else: self.journey_time_matrix[self.i_start].append(0) if self.journey_time_matrix[self.i_start][j] != 0: self.add_xml_edge(self.i_start,j) # then do the rest: for i in range(self.i_start + 1, len(self.my_station_list.get_list())): self.journey_time_matrix.append([]) self.add_xml_node(i) for j in range(len(self.my_station_list.get_list())): if (i != j)\ and not(i==94 and j==95) and not(i==95 and j==94)\ and not(i==102 and j==103) and not(i==103 and j==102)\ and not(i==102 and j==104) and not(i==104 and j==103)\ and not(i==103 and j==104) and not(i==104 and j==103)\ and not(i==171 and j==172) and not(i==172 and j==171)\ and not(i==198 and j==199) and not(i==199 and j==198): print "Journey:",\ self.my_station_list.get_list()[i][1].get_name(), \ "to",\ self.my_station_list.get_list()[j][1].get_name() self.this_journey = Journey(self.my_station_list\ .get_list()[i][1],\ self.my_station_list\ .get_list()[j][1],\ "20130801",\ "0800") self.this_journey.read_api() self.journey_time_matrix[i].append(\ self.this_journey.get_no_change_journey_time()) self.this_journey.cleanup_files() else: self.journey_time_matrix[i].append(0) if self.journey_time_matrix[i][j] != 0: self.add_xml_edge(i,j) def export_to_gexf(self): """ Export the generated graph to GEXF format used by gephi. """ import time import subprocess import xml.etree.ElementTree as ET self.begin_xml_file() for i in range(len(self.journey_time_matrix)): self.new_node = ET.Element('node', {'id': str(i),\ 'label':\ self.my_station_list\ .get_station(i)}) self.my_atts = ET.Element('attvalues') self.my_stn_lat = ET.Element('attvalue', {'for': 'latitude',\ 'value':\ str(self.my_station_list\ .fetch_station_id(i)\ .get_lat_long()[0])}) self.my_stn_long = ET.Element('attvalue', {'for': 'longitude',\ 'value':\ str(self.my_station_list\ .fetch_station_id(i)\ .get_lat_long()[1])}) self.my_atts.append(self.my_stn_lat) self.my_atts.append(self.my_stn_long) self.new_node.append(self.my_atts) self.my_nodes.append(self.new_node) for j in range(len(self.journey_time_matrix)): if self.journey_time_matrix[i][j] != 0: self.edge_id = str((i * len(self.journey_time_matrix)) + j) self.new_edge = ET.Element('edge', {'id': self.edge_id,\ 'source': str(i),\ 'target': str(j),\ 'weight':\ str(round(self.journey_time_matrix[i][j],1))}) self.my_edges.append(self.new_edge) self.my_tree.write('TubeJourneyTimesGraph.gexf') def begin_xml_file(self): """ Create a new xml file and the appropriate meta data. Does not need to be called every time the program is run - only if there is no starting tree. """ import time import subprocess import xml.etree.ElementTree as ET subprocess.call(['touch', 'TubeJourneyTimesGraph.gexf']) self.my_gexf = ET.Element('gexf',\ {'xmlns': 'https://www.gexf.net/1.2draft',\ 'xmlns:xsi':\ 'http://www.w3.org/2001/XMLSchema-instance',\ 'xsi:schemaLocation': \ 'http://www.gexf.net/1.2draft \n \http://www.gexf.net/1.2draft/gexf.xsd',\ 'version': '1.2'}) self.my_tree = ET.ElementTree(self.my_gexf) self.my_meta = ET.Element('meta', {'lastmodifieddate': time.strftime('%Y-%m-%d')}) self.my_creator = ET.Element('creator') self.my_creator.text = "Andy Holt" self.my_description = ET.Element('description') self.my_description.text = "Graph of tube network" self.my_meta.append(self.my_creator) self.my_meta.append(self.my_description) self.my_gexf.append(self.my_meta) self.my_graph = ET.Element('graph', {'defaultedgetype': 'directed'}) self.my_gexf.append(self.my_graph) self.my_attributes = ET.Element('attributes', {'class': 'node'}) self.my_lat = ET.Element('attribute',\ {'id': 'latitutde', 'title': 'latitude', 'type':'double'}) self.my_long = ET.Element('attribute',\ {'id': 'longitude', 'title': 'longitude', 'type':'double'}) self.my_attributes.append(self.my_lat) self.my_attributes.append(self.my_long) self.my_graph.append(self.my_attributes) self.my_nodes = ET.Element('nodes') self.my_graph.append(self.my_nodes) self.my_edges = ET.Element('edges') self.my_graph.append(self.my_edges) self.my_tree.write('TubeJourneyTimesGraph.gexf') def add_xml_node(self, i): """ Add a new station to the xml file and store. """ import xml.etree.ElementTree as ET self.new_node = ET.Element('node', {'id': str(i),\ 'label':\ self.my_station_list.get_station(i)}) self.my_atts = ET.Element('attvalues') self.my_stn_lat = ET.Element('attvalue', {'for': 'latitude',\ 'value':\ str(self.my_station_list.fetch_station_id(i)\ .get_lat_long()[0])}) self.my_stn_long = ET.Element('attvalue', {'for': 'longitude',\ 'value':\ str(self.my_station_list.fetch_station_id(i)\ .get_lat_long()[1])}) self.my_atts.append(self.my_stn_lat) self.my_atts.append(self.my_stn_long) self.new_node.append(self.my_atts) self.my_nodes.append(self.new_node) self.my_tree.write('TubeJourneyTimesGraph.gexf') def add_xml_edge(self, i, j): """ Add a new journey to the xml file and store. """ import xml.etree.ElementTree as ET self.edge_id = str((i * len(self.my_station_list.get_list())) + j) self.new_edge = ET.Element('edge', {'id': self.edge_id,\ 'source': str(i),\ 'target': str(j),\ 'weight': str(round(self.journey_time_matrix[i][j],1))}) self.my_edges.append(self.new_edge) self.my_tree.write('TubeJourneyTimesGraph.gexf')
def __init__(self, builder): self.builder = builder Journey(builder.startCharacterJourney())