-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.py
More file actions
51 lines (40 loc) · 1.8 KB
/
Copy pathroutes.py
File metadata and controls
51 lines (40 loc) · 1.8 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
import datetime
FOLDER = "smart_fare_data"
ALL_ROUTES = {
"R01": {"name": "City Centre to Airport", "distance_km": 25},
"R02": {"name": "Old Town to Tech Park", "distance_km": 18},
"R03": {"name": "University to Railway Station", "distance_km": 12},
"R04": {"name": "Suburbs to Shopping Mall", "distance_km": 10},
"R05": {"name": "Hospital to IT Corridor", "distance_km": 15},
}
def _fpath(f): return FOLDER + "/" + f
def _read(f):
try:
with open(_fpath(f)) as fp: return [l.strip() for l in fp if l.strip()]
except FileNotFoundError: return []
def _write(f, lines):
with open(_fpath(f), "w") as fp: fp.write("\n".join(lines) + "\n")
def load_users():
users = {}
for l in _read("users.txt"):
u, pw, w, j = l.split("|")
users[u] = {"password": pw, "wallet": float(w), "joined_on": j}
return users
def save_users(u):
_write("users.txt", [f"{k}|{v['password']}|{v['wallet']}|{v['joined_on']}" for k, v in u.items()])
def load_tickets():
t = {}
for l in _read("tickets.txt"):
u, tid, route, fare, dt, st = l.split("|")
t.setdefault(u, []).append({"ticket_id": tid, "route": route, "fare": fare, "booked_on": dt, "status": st})
return t
def save_tickets(t):
_write("tickets.txt", [f"{u}|{x['ticket_id']}|{x['route']}|{x['fare']}|{x['booked_on']}|{x['status']}" for u, xs in t.items() for x in xs])
def load_passes():
p = {}
for l in _read("passes.txt"):
u, pid, label, price, s, e = l.split("|")
p.setdefault(u, []).append({"pass_id": pid, "label": label, "price": price, "start_date": s, "end_date": e})
return p
def save_passes(p):
_write("passes.txt", [f"{u}|{x['pass_id']}|{x['label']}|{x['price']}|{x['start_date']}|{x['end_date']}" for u, xs in p.items() for x in xs])