def stops_by_route( self, update: bool = False) -> Dict[int, List[ShuttleService.Stop]]: """ Get the stops for each route active for the current shuttle service :param update: Whether to return the last computed data or fetch the latest data from the web :return: A dictionary mapping route IDs to lists of stops """ self._route_data_lock.acquire() if update: ss = ShuttleService.ShuttleService(self._agency_id) stops = ss.get_stops() stop_dict = {} for stop in stops: stop_dict[stop.id] = stop stops_by_route = ss.get_stop_ids_for_routes() latest_route_data = {} for route in ss.get_routes(): latest_route_data[route.id] = [ stop_dict[s] for s in stops_by_route[route.id] ] self._last_route_data = latest_route_data self._route_data_lock.release() return self._last_route_data
def test_get_route_name(self): ss = ShuttleService.ShuttleService(307) TestScheduleManager.sm.get_route_name(ss.get_routes()[0].id) with pytest.raises(ScheduleManager.UnknownRoute): TestScheduleManager.sm.get_route_name(1)
def __init__(self): self.id = random.randrange(100000, 200000) self.route_id = random.choice(list(MockShuttle.STOPS_BY_ROUTE_ID)) self.timestamp = datetime.datetime.now().astimezone(datetime.timezone(datetime.timedelta(seconds=0))) self._stops = cycle(MockShuttle.STOPS_BY_ROUTE_ID[self.route_id]) self._ss = ShuttleService.ShuttleService(307) pass
def main(): sm = ShuttleService.ShuttleManager(307) with ScheduleManager.SharedScheduleManager() as manager: scheduler: ScheduleManager.ScheduleManager = manager.ScheduleManager( 307, os.path.join(os.getcwd(), 'schedules', 'generated'), 'America/New_York') workers: List[Process] = [] while True: for shuttle in sm.shuttles: p = Process(target=process_shuttle, args=(scheduler, shuttle)) p.start() workers.append(p) time.sleep(2 - (time.time() % 2))
def main(): logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(levelname)s:%(message)s') db: psycopg2 = psycopg2.connect(**parse_config()) db.set_session(autocommit=True) sm = ShuttleService.ShuttleManager(307) scheduler: ScheduleManager.ScheduleManager = ScheduleManager.ScheduleManager( 307, os.path.join(os.getcwd(), 'schedules', 'generated'), 'America/New_York') while True: for shuttle in sm.shuttles(): threading.Thread(target=process_shuttle, args=(scheduler, shuttle, db)).start() time.sleep(1 - (time.time() % 1))
def __init__(self, agency_id: int, schedules_path: str, local_timezone: str): """ Manages all routing and scheduling and determines the timeliness of a shuttle :param agency_id: The agency for which to compute scheduling :param schedules_path: The path where the CSVs of the schedules are stored :param local_timezone: A string representing the local timezone """ self._shuttle_data_lock = Lock() self._route_data_lock = Lock() self._paper_schedules_lock = Lock() self._tz = pytz.timezone(local_timezone) self._agency_id = agency_id self._schedules_path = schedules_path self._last_route_data = self.stops_by_route(update=True) self._last_paper_schedules = self.paper_schedules(update=True) self._shuttle_data = {} routes = ShuttleService.ShuttleService(self._agency_id).get_routes() self._known_routes = {route.id: route.long_name for route in routes}
def test_get_stops(self): ss = ShuttleService.ShuttleService(307) assert isinstance(ss.get_stops(), list)
def test_get_stop_ids_for_routes(self): ss = ShuttleService.ShuttleService(307) assert isinstance(ss.get_stop_ids_for_routes(), dict)