-
Notifications
You must be signed in to change notification settings - Fork 1
/
metro_rush.py
executable file
·63 lines (55 loc) · 1.89 KB
/
metro_rush.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
#!/usr/bin/env python3
from argparse import ArgumentParser
from sys import stderr
from time import time
from moving import MovingTrains
def get_arguments():
"""
Get and parse arguments from terminal.
@return: namespace of arguments include:
- data file name
- algorithm type
- visualization
"""
parser = ArgumentParser(prog='Metro Network',
usage='[filename] --algo [ALGO] --gui')
parser.add_argument('filename', help='A metro stations file')
parser.add_argument('--algo', nargs='?', metavar='ALGO', default=0,
choices=[0, 1], type=int,
help='specify which algorithm to use for finding '
'the smallest number of turns')
parser.add_argument('--gui', action='store_true',
help='visualize the Metro Network with Pyglet')
return parser.parse_args()
def read_data_file(filename):
"""
Read information from data file.
Save each line to a list.
@param filename: metro stations file
@return: list of each line in metro stations file
"""
try:
with open(filename, 'r') as file:
return file.readlines()
except (FileNotFoundError, PermissionError, IsADirectoryError):
stderr.write('Invalid file\n')
exit(1)
def main():
"""Run main program."""
# Setup start time of program.
start = time()
# Get arguments for user input.
args = get_arguments()
# Find paths and run trains.
delhi_metro = MovingTrains(read_data_file(args.filename), args.algo)
# Print runtime.
print('\nRuntime: {}s'.format(round(time() - start, 5)))
# Visualize metro network by Pyglet.
if args.gui:
from visualize import GUI
GUI(delhi_metro)
if __name__ == '__main__':
try:
main()
except Exception as error:
print(error)