def test_shortest_route(self): routes = ['AB1', 'AC2', 'CA4', 'CD1', 'DA1'] rail = Railway(routes) self.assertRaises(ValueError, rail.shortest_route, None, 'A') self.assertEqual(rail.shortest_route('A', 'B'), 1) self.assertEqual(rail.shortest_route('B', 'C'), float('inf')) self.assertEqual(rail.shortest_route('C', 'A'), 2)
class RailwaySampleDataTest(unittest.TestCase): def setUp(self): routes = "AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7" self.rail = Railway(routes) def test_distances(self): self.assertEqual(self.rail.get_route_distance('A', 'B', 'C'), 9) self.assertEqual(self.rail.get_route_distance('A', 'D'), 5) self.assertEqual(self.rail.get_route_distance('A', 'D', 'C'), 13) self.assertEqual(self.rail.get_route_distance('A', 'E', 'B', 'C', 'D'), 22) self.assertEqual(self.rail.get_route_distance('A', 'E', 'D'), None) def test_find_routes_by_stops(self): self.assertEqual(len(self.rail.find_routes_by_stops('C', 'C', 3)), 2) self.assertEqual( len( self.rail.find_routes_by_stops('A', 'C', 4, Railway.FindModes.exact)), 3) def test_find_shortest_route(self): self.assertEqual(self.rail.shortest_route('A', 'C'), 9) self.assertEqual(self.rail.shortest_route('B', 'B'), 9) def test_find_routes_by_distance(self): self.assertEqual(len(self.rail.find_routes_by_distance('C', 'C', 30)), 7)
def test_init_add_routes(self): self.assertRaises(TypeError, Railway) routes = ['AB1', 'AC2', 'CA4'] rail = Railway(routes) self.assertEqual(rail.routes['A']['B'], 1) self.assertEqual(rail.routes['A']['C'], 2) self.assertEqual(rail.routes['C']['A'], 4) routes = 'DE3, FD7, AG2' rail = Railway(routes) self.assertEqual(rail.routes['D']['E'], 3) self.assertEqual(rail.routes['F']['D'], 7) self.assertEqual(rail.routes['A']['G'], 2)
def next_action(self): if self.slots['to_city'] is None: print(1) self.get_to_city() elif self.slots['from_city'] is None: self.get_from_city() elif self.slots['date'] is None or self.slots['month'] is None: self.get_date() else: train_info = Railway().train_between_station( self.slots['from_city'], self.slots['to_city'], str(self.slots['date']) + '-' + str(self.slots['month']) + '-' + str(self.slots['year'])) self.slots['to_city'] = None self.slots['from_city'] = None self.slots['date'] = None self.slots['month'] = None self.slots['year'] = None self.slots = {} print("!!!!!!!!!!!!!!!!!!!!!!!!!!!", self.slots) socket_client.send(train_info) self.message = '' # socket_client.send("Which train do you want to choose?") # self.choose_train = True self.count = 0
def test_validate_and_parse_route(self): route = None self.assertRaises(ValueError, Railway.validate_and_parse_route, route) route = '' self.assertRaises(ValueError, Railway.validate_and_parse_route, route) route = 'A' self.assertRaises(ValueError, Railway.validate_and_parse_route, route) route = 'A 5' self.assertRaises(ValueError, Railway.validate_and_parse_route, route) route = 'DED' self.assertRaises(ValueError, Railway.validate_and_parse_route, route) route = 'zA4' self.assertRaises(ValueError, Railway.validate_and_parse_route, route) route = 'A$4' self.assertRaises(ValueError, Railway.validate_and_parse_route, route) route = 'AE5' self.assertEqual(Railway.validate_and_parse_route(route), ('A', 'E', 5))
def get_from_city(self): if self.message != '': print("whoh") print(self.message) print('111') self.slots = self.en.get_from(self.message) print(self.slots) self.message = '' # self.next_action() if self.slots['to_city'] is None: socket_client.send("Where do you want to travel to?") elif self.slots['from_city'] is None: socket_client.send( "Where will you be boarding the train from?") elif self.slots['date'] is None or self.slots['month'] is None: socket_client.send("At what date to you want to travel?") else: train_info = Railway().train_between_station( self.slots['from_city'], self.slots['to_city'], str(self.slots['date']) + '-' + str(self.slots['month']) + '-' + str(self.slots['year'])) self.slots['to_city'] = None self.slots['from_city'] = None self.slots['date'] = None self.slots['month'] = None self.slots['year'] = None self.slots = {} print("!!!!!!!!!!!!!!!!!!!!!!!!!!!", self.slots) socket_client.send(train_info) self.message = '' self.count = 0
def test_get_route_distance(self): routes = ['AB1', 'AC2', 'CA4'] rail = Railway(routes) self.assertRaises(ValueError, rail.get_route_distance, None, 'A') self.assertRaises(KeyError, rail.get_route_distance, 'D', 'A') self.assertEqual(rail.get_route_distance('A', 'B'), 1) self.assertEqual(rail.get_route_distance('A', 'C'), 2) self.assertEqual(rail.get_route_distance('C', 'A'), 4) self.assertEqual(rail.get_route_distance('A', 'C', 'A'), 6) self.assertEqual(rail.get_route_distance('B', 'C'), None)
def test_find_routes_by_stops(self): routes = ['AB1', 'AC2', 'CA4'] rail = Railway(routes) self.assertRaises(ValueError, rail.find_routes_by_stops, None, 'A', 5) self.assertRaises(ValueError, rail.find_routes_by_stops, 'A', 'B', 'Z') self.assertRaises(KeyError, rail.find_routes_by_stops, 'D', 'A', 5) self.assertEqual(rail.find_routes_by_stops('A', 'A', 3), [(['A', 'C', 'A'], 6)]) self.assertEqual(rail.find_routes_by_stops('A', 'A', '3'), [(['A', 'C', 'A'], 6)]) self.assertEqual(rail.find_routes_by_stops('A', 'C', 3), [(['A', 'C'], 2)]) self.assertEqual( rail.find_routes_by_stops('A', 'C', 4, Railway.FindModes.exact), []) self.assertEqual( rail.find_routes_by_stops('A', 'C', 5, Railway.FindModes.exact), [(['A', 'C', 'A', 'C', 'A', 'C'], 14)])
def get_pnr(self): if self.pnr: if len(self.pnr) == 10: pnr = Railway().pnr_status(self.pnr) socket_client.send(pnr) self.count = 0 else: socket_client.send("wrong pnr number") self.count = 0 else: responses = [ 'What is your pnr number?', 'Can You tell me what your PNR number is?', 'Ok, sure tell me your pnr number', 'please tell me your pnr number' ] socket_client.send(random.choice(responses)) self.message = '' self.get_pnr_from_user()
def test_find_routes_by_distance(self): routes = ['AB1', 'AC2', 'CA4'] rail = Railway(routes) self.assertRaises(ValueError, rail.find_routes_by_distance, None, 'A', 5) self.assertRaises(ValueError, rail.find_routes_by_distance, 'A', 'B', 'Z') self.assertRaises(KeyError, rail.find_routes_by_distance, 'D', 'A', 5) self.assertEqual(rail.find_routes_by_distance('A', 'B', -1), []) self.assertEqual(rail.find_routes_by_distance('A', 'B', 0), []) self.assertEqual(rail.find_routes_by_distance('A', 'B', 2), [['A', 'B', 1]]) self.assertEqual(rail.find_routes_by_distance('A', 'C', 14), [['A', 'C', 2], ['A', 'C', 'A', 'C', 8]]) self.assertEqual(rail.find_routes_by_distance('A', 'C', 15), [['A', 'C', 2], ['A', 'C', 'A', 'C', 8], ['A', 'C', 'A', 'C', 'A', 'C', 14]]) self.assertEqual(len(rail.find_routes_by_distance('A', 'C', 1000)), 167)
def get_pnr_from_user(self): # self.message = '' if self.message != '': print("######") l = self.message.split(' ') print(l) x = None for e in l: try: int(e) x = int(e) print(x) except: pass if x: pnr = Railway().pnr_status(x) socket_client.send(pnr) self.count = 0 else: socket_client.send("wrong pnr number") self.count = 0
# Подгруженные файлы (Классы)(модули) from railway import Railway NEWLINE = "\n" * 2 # Создаёт экземпляр класса Railway и выводит приветственное сообщение. railway = Railway() print(NEWLINE) print("Добро пожаловать в программу 'Железная дорога'") print(NEWLINE) # Запуск меню (Цикл). С запросом ввода нужного пользователю пункта # и передаёт результат в виде параметра методу selected. while True: print(f"stations: {railway.stations}\n\n") print(f"trains: {railway.trains}\n\n") print(f"wagons: {railway.wagons}\n\n") print(f"routes: {railway.routes}\n\n") print(NEWLINE) railway.menu_items() menu_item = input() if menu_item == '0': break railway.selected(menu_item)
def detect_intent(self): self.slots = { 'to_city': None, 'from_city': None, 'date': None, 'month': None, 'year': 2019 } intent = self.i.classify(self.message, self.vocab, self.intent_categories) self.intent = intent print(intent) if intent == "train_reservation": self.get_entities() elif intent == "greeting": responses = ["hey, I'm RailBot", "Hi there", "hey"] socket_client.send(random.choice(responses)) self.message = '' self.count = 0 elif intent == "goodbye": responses = [ 'See you later, thanks for visiting', 'Have a nice day', 'Bye! Come back again soon.' ] socket_client.send(random.choice(responses)) self.message = '' self.count = 0 elif intent == "pnr_status": self.get_pnr() self.message = '' elif intent == "thanks": responses = ['Happy to help!', 'Any time!', 'My pleasure'] socket_client.send(random.choice(responses)) self.message = '' self.count = 0 elif intent == "train_schedule": if self.message != '': print("######") l = self.message.split(' ') print(l) x = None for e in l: try: int(e) x = int(e) print(x) except: pass if x: pnr = Railway().train_schedule(x) socket_client.send(pnr) self.count = 0 else: socket_client.send("wrong train number") self.count = 0 else: self.count = 0
# Подгруженные файлы (Классы)(модули) from railway import Railway NEWLINE = "\n" * 2 # Создаёт экземпляр класса Railway и выводит приветственное сообщение. railway = Railway() print(NEWLINE) print("Добро пожаловать в программу 'Железная дорога'") print(NEWLINE) # Запуск меню (Цикл). С запросом ввода нужного пользователю пункта # и передаёт результат в виде параметра методу selected. while True: railway.main_menu_items() number_menu = input() if number_menu == '0': break while True: print(NEWLINE) print(f"stations: {railway.stations}\n\n") print(f"trains: {railway.trains}\n\n") print(f"wagons: {railway.wagons}\n\n") print(f"routes: {railway.routes}\n\n") if number_menu == '1': railway.create_menu_items() elif number_menu == '2': railway.perform_actions_objects_menu() elif number_menu == '3':
def __init__(self): """ Parameters: None Returns: None """ timeslots.extend(timeslots_NL) rail_list.extend(rail_list_NL) self.pause = True # start off in pause mode to make adding delays easier self.stations = stations_list self._create_station_hash() self.schedules = [] self._create_schedules() for r in rail_list: self._get_station(r[0]).attach_rail( Railway(r[2], r[3], self._get_station(r[0]), self._get_station(r[1]))) self._get_station(r[1]).attach_rail( Railway(r[2], r[3], self._get_station(r[1]), self._get_station(r[0]))) # create background background_image = cv2.imread("backgroundNL.jpg") self.background_image = cv2.resize(background_image, (SCREEN_WIDTH_B, SCREEN_HEIGHT_B)) self.train_image = cv2.imread("logo.png") self.background2 = self.train_image # set sizes for stations and trains self.station_width = STATION_WIDTH self.station_height = STATION_HEIGHT self.train_width = TRAIN_WIDTH self.train_height = TRAIN_HEIGHT # set all black pixels to yellow black = np.all(self.train_image == [0, 0, 0], axis=-1) self.train_image[black] = [51, 204, 255] # NS yellow color self.background2 = cv2.resize(self.background2, (400, 300)) self.background_image2 = np.copy(self.background2) self.train_image = cv2.resize(self.train_image, (self.train_width, self.train_height)) self.background = np.copy(self.background_image) # holds the mouse hover coordinates self.ix = -1 self.iy = -1 # holds the mouse click coordinates self.click_x = -1 self.click_y = -1 # holds the mouse shift click coordinates self.shift_click_x = -1 self.shift_click_y = -1 self.tick = 0 self.selected_train = None
file_path = opts.get("s", "data.xml") def print_help(): print("1 - load date from file\n" + "2 - save data in file \n" + "3 - add new railroad \n" + "4 - add new train station \n" + "5 - edit railroad \n" + "6 - edit train station \n" + "7 - delete railroad \n" + "8 - delete train station \n" + "9 - search railroad \n" + "10 - search train station \n" + "11 - output list of railroads \n" + "12 - output list of train stations on a railroad \n" + "any other key - exit") if __name__ == '__main__': Railway = Railway(dtd_path) while True: print_help() print("Enter command number:") command = int(input()) if command == 1: Railway.load_data(file_path) elif command == 2: Railway.save_to_db(file_path) elif command == 3: print("Enter railroad name: ") railroadName = input() Railroad = Railroad(0, railroadName) Railway.add_railroad(Railroad)
def setUp(self): routes = "AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7" self.rail = Railway(routes)
'6: Find the number of different routes between specified stations with a specified maximum distance' ) print('M: Display this menu') print('X: Exit the Kiwiland railway system') while True: print('Welcome to the Kiwiland railway system!') print('Our latest railway map is: {}'.format(route)) change_graph = input('Is this correct? Y/N: ') if change_graph.upper() == 'Y': break else: route = input('Please enter the latest railway map: ') rail = Railway(route) print_menu() while True: print('') menu_selection = input('Your selection: ').upper() if menu_selection == '1': print('Output #1: {}'.format( rail.get_route_distance('A', 'B', 'C') or 'NO SUCH ROUTE')) print('Output #2: {}'.format( rail.get_route_distance('A', 'D') or 'NO SUCH ROUTE')) print('Output #3: {}'.format( rail.get_route_distance('A', 'D', 'C') or 'NO SUCH ROUTE')) print('Output #4: {}'.format( rail.get_route_distance('A', 'E', 'B', 'C', 'D') or 'NO SUCH ROUTE'))