This repository has been archived by the owner on Nov 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathfinder.py
80 lines (64 loc) · 2.8 KB
/
pathfinder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import csv
import json
class PathFinder:
def __init__(self, departure, destination):
self.departure = departure
self.destination = destination
self.parseTimeTable()
def findFastest(self, trips):
fastest = {"notFound" : True, "duree": 9999999}
for trip in self.findEveryTrips():
if int(trip["duree"]) <= int(fastest["duree"]):
fastest = trip
fastest["notFound"] = False
return fastest
def findEveryTrips(self):
trips = self.findDirectTrips() + self.findPossibleEscale()
return trips
# return self.findDirectTrips().append(self.findPossibleEscale())
def findDirectTrips(self):
return [trip for trip in self.rows if (trip["departure"] == self.departure and trip["destination"] == self.destination)]
def findDepartureTrips(self):
return [trip for trip in self.rows if trip["departure"] == self.departure]
def findDestinationTrips(self):
return [trip for trip in self.rows if trip["destination"] == self.destination]
def findPossibleEscale(self):
possibleDeparture = self.findDepartureTrips()
possibleDestination = self.findDestinationTrips()
possibles = []
for depTrip in possibleDeparture:
for desTrip in possibleDestination:
if (depTrip["destination"] == desTrip["departure"]):
trip = {
"trip_ids": [depTrip["trip_id"], desTrip["trip_id"]],
"departure": depTrip["departure"],
"escale": depTrip["destination"],
"destination": desTrip["destination"],
"duree": str(int(desTrip["duree"]) + int(depTrip["duree"]))
}
possibles.append(trip)
return possibles
# return [trip for trip in if ()]
def parseTimeTable(self):
file = open('dataset/timetables.csv')
csvreader = csv.reader(file, delimiter='\t')
headers = next(csvreader)
rows = []
for row in csvreader:
trajet = row[1].split(" - ")
if not trajet[0].startswith("Gare de"):
trajet[0] = "Gare de " + trajet[0]
if not trajet[1].startswith("Gare de"):
trajet[1] = "Gare de " + trajet[1]
departure = trajet[0].split("Gare de ")[1]
destination = trajet[1].split("Gare de ")[1]
row = {
headers[0]: row[0],
"departure": departure,
"destination": destination,
headers[2]: row[2],
}
rows.append(row)
self.rows = rows
def printJson(self, var, prependText=None):
print(prependText, json.dumps(var, sort_keys=False, indent=4))