示例#1
0
def start_cli_loop(router: Router, origin: str = None, dest: str = None):
    interactive = not origin and not dest
    first_time = True

    while interactive or first_time:
        first_time = False

        # block until origin is valid
        while not router.has_station(origin):
            if origin:
                print(f'Station "{origin}" not found')
            origin = input('Enter Origin Station: ')

        # block until dest is valid
        while not router.has_station(dest):
            if dest:
                print(f'Station "{dest}" not found')
            dest = input('Enter Destination Station: ')

        print('Routing...')
        try:
            route = router.find_route(origin, dest)
            if route is None:
                print(f'Result: No routes from {origin} to {dest}')
            else:
                print(
                    f'Result: {route.stop_count} stop(s), {route.total_time} minute(s)'
                )
                print(
                    f'Details: {", ".join([str(seg) for seg in route.path])}')
        except Exception as ex:
            print(ex, file=sys.stderr)

        origin = dest = None

    return 0
示例#2
0
 def test_error_handling(self):
     router = Router('tests/data/test_case_1.csv')
     self.assertRaises(InvalidStationError,
                       lambda: router.find_route('A', 'Z'))
     self.assertRaises(InvalidStationError,
                       lambda: router.find_route(None, ''))