-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataparser.py
55 lines (47 loc) · 1.46 KB
/
dataparser.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
# URL: https://gtfsrt.api.translink.com.au/GTFS/SEQ_GTFS.zip
from pathlib import Path
import csv
import contextlib
class DataParser:
"""Class to load and parse GTFS dataset datasets
Args:
path (Path): path to folder containing GTFS files
dataset_names (Tuple[str, str]): dataset name and key to use as index
Attributes:
agency (dict)
routes (dict)
stops (dict)
trips (dict)
"""
def __init__(
self,
path=Path("SEQ_GTFS"),
dataset_names=(
("agency", "agency_name"),
("routes", "route_id"),
("stops", "stop_id"),
("trips", "trip_id"),
),
):
self.agency = self.routes = self.stops = self.trips = None
with contextlib.ExitStack() as stack:
datasets = {
dataset: {
row[key]: row
for row in csv.DictReader(
stack.enter_context(open(path / (dataset + ".txt")))
)
}
for dataset, key in dataset_names
}
self.__dict__.update(datasets)
if __name__ == "__main__":
# DEBUG
s = DataParser()
print(*next(iter(s.agency.values())).values())
try:
while True:
in_ = input("[dictname] [dictkey] >>> ").split()
print(getattr(s, in_[0])["".join(in_[1:])])
except (KeyboardInterrupt, EOFError):
print("Exiting...")