This repository was archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUS-D.py
More file actions
265 lines (200 loc) · 7.46 KB
/
US-D.py
File metadata and controls
265 lines (200 loc) · 7.46 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import sqlite3
con = sqlite3.connect("jernbanen.db")
"""
Henter ut alle stasjonene fra databasen og returnerer en dict med navn som key og stasjonId som value.
"""
def getStations():
queryRes = con.cursor().execute("SELECT navn, stasjonId FROM Jernbanestasjon").fetchall()
map = {}
for station in queryRes:
map[station[0]] = station[1]
stations = []
for station in queryRes:
stations.append(station[0])
return map, stations
"""
Printer ut alle stasjonene i en fin tabell.
@params stations: en liste med stasjonsnavn
"""
def printStations(stations):
print("Eksisterende stasjoner:\n")
print("|-----------|")
print("| Navn |")
print("|-----------|")
for station in stations:
print("| %9s |" % (station))
print("|-----------|\n")
"""
Henter ut alle ruter som går på en gitt dato etter gitt klokkeslett og påfølgende dato.
"""
def getRoutesData(date, time, nextdate):
cur = con.cursor()
res = cur.execute("""
SELECT TF.dato, S.rutenummer, S.stoppNummer, S.stasjonId FROM Togrute T
JOIN Stoppested S ON S.rutenummer = T.rutenummer
JOIN Togruteforekomst TF ON TF.rutenummer = T.rutenummer
JOIN Stoppested avreise ON (avreise.rutenummer = T.rutenummer AND avreise.stoppNummer = 1)
WHERE (dato = ? AND avreise.avgangsTid > ?)OR dato= ?
ORDER BY dato, avreise.avgangsTid, S.rutenummer, S.stoppnummer ASC
""", (date, time, nextdate))
return res.fetchall()
"""
Strukturerer dataen slik at vi får en dict med datoer som keys, og rutenummer som keys i disse dictene.
Dette gjør det enklere å finne ruter som går mellom to stasjoner.
{
"2023-04-03": {
"1": "123456",
"2": "123456",
"3": "4321"
},
"2023-04-04": {
"1": "123456",
"2": "123445",
"3": "4321"
}
@param routeData: Dataen som skal struktureres
@return: Strukturert data på formen som er beskrevet over
"""
def routeDataToDict(routeData):
routes = {}
for route in routeData:
if route[0] not in routes:
routes[route[0]] = {}
routes[route[0]][route[1]] = str(route[3])
else:
if route[1] not in routes[route[0]]:
routes[route[0]][route[1]] = str(route[3])
else:
routes[route[0]][route[1]] += str(route[3])
return routes
"""
Henter ut alle ruter som går fra startstasjon til sluttstasjon.
@param routes: Alle ruter i et dict
@param fromStationId: Avreisestasjon
@param toStationId: Endestasjon
"""
def getExistingRoutes(routes, fromStationId, toStationId):
existingRoutes = []
for date in routes:
for route in routes[date]:
if str(fromStationId) in routes[date][route] and str(toStationId) in routes[date][route]:
if routes[date][route].index(str(fromStationId)) < routes[date][route].index(str(toStationId)):
existingRoutes.append([date, str(route)])
return existingRoutes
"""
Henter ut data om en bestemt rute og printer den ut.
@param date: Datoen ruten går
@param route: Rutenummeret
@param fromStationId: Avreisestasjon
@param toStationId: Endestasjon
"""
def getRouteData(date, route, fromStationId, toStationId):
cur = con.cursor()
res = cur.execute("""
SELECT Togrute.rutenummer, j1.navn, s.avgangstid, j2.navn, e.ankomstTid FROM Togrute
JOIN Togruteforekomst ON Togruteforekomst.rutenummer = Togrute.rutenummer
JOIN Stoppested s ON s.stasjonId = ? AND s.rutenummer = Togrute.rutenummer
JOIN Stoppested e ON e.stasjonId = ? AND e.rutenummer = Togrute.rutenummer
JOIN Jernbanestasjon j1 ON s.StasjonId = j1.stasjonId
JOIN Jernbanestasjon j2 ON e.StasjonId = j2.stasjonId
WHERE Togruteforekomst.dato = ? AND Togrute.rutenummer = ?
""",(fromStationId, toStationId, date, route))
for row in res:
print("""
-------------------------
Dato: %s
Rute: %s
Fra: %10s kl. %5s
Til: %10s kl. %5s
-------------------------
""" % (date, row[0], row[1], row[2], row[3], row[4]))
"""
Henter ut neste dato.
@Param date: Datoen som skal brukes som utgangspunkt for neste dato på formen "yyyy-mm-dd"
"""
def getNextDate(date):
dates = date.split("-")
year = int(dates[0])
month = int(dates[1])
day = int(dates[2])
if day == 30:
day = 1
if month == 12:
month = 1
year += 1
else:
month += 1
else:
day += 1
if(month < 10):
month = "0" + str(month)
if(day < 10):
day = "0" + str(day)
return str(year) + "-" + str(month) + "-" + str(day)
def main():
print("Velkommen til Jernbanen, her kan du se hvilke ruter som går fra og til en stasjon med utgangspunkt i dato")
print("Tast inn q for å avslutte programmet\n")
stationsMap, stations = getStations()
printStations(stations)
res = ""
while res != "q":
res = input("Velg to stasjoner fra listen over stasjoner separert med komma uten mellomrom (f.eks. Trondheim,Bodø): ").strip()
if res == "q":
break
res = res.split(",")
if len(res) != 2:
print("Ugyldig input, prøv igjen")
continue
fromStation = res[0].strip()
toStation = res[1].strip()
if fromStation not in stations or toStation not in stations:
print("Ugyldige stasjoner, velg stasjoner fra listen")
continue
fromStationId = stationsMap[fromStation]
toStationId = stationsMap[toStation]
res = input("Velg dato på formatet YYYY-MM-DD: ").strip()
if res == "q":
break
dates = res.split("-")
if len(dates) != 3:
print("Ugyldig dato, prøv igjen")
continue
if(len(dates[0]) != 4 or len(dates[1]) != 2 or len(dates[2]) != 2):
print("Ugyldig dato, prøv igjen")
continue
try:
int(dates[0])
int(dates[1])
int(dates[2])
except:
print("Ugyldig dato, prøv igjen")
continue
date = dates[0] + "-" + dates[1] + "-" + dates[2]
nextDate = getNextDate(date)
res = input("Velg tidspunkt på formatet HH:MM: ")
if res == "q":
break
times = res.strip().split(":")
if len(times) != 2:
print("Ugyldig tidspunkt, prøv igjen")
continue
if(len(times[0]) != 2 or len(times[1]) != 2):
print("Ugyldig tidspunkt, prøv igjen")
continue
try:
numHours = int(times[0])
numMinutes = int(times[1])
except:
print("Ugyldig tidspunkt, prøv igjen")
continue
if numHours < 0 or numHours > 23 or numMinutes < 0 or numMinutes > 59:
print("Ugyldig tidspunkt, prøv igjen")
continue
time = times[0] + ":" + times[1]
routesData = getRoutesData(date, time, nextDate)
routes = routeDataToDict(routesData)
existingRoutes = getExistingRoutes(routes, fromStationId, toStationId)
for route in existingRoutes:
getRouteData(route[0], route[1], fromStationId, toStationId)
main()
con.close()