Example #1
0
	def add_city(self):
		'''
		Adds a city with the inputed parameters. If the parameters are successfully set, then the city is added to the map.
		True is returned if the city was successfully added, otherwise False.
		'''
		city_code = raw_input("\nPlease enter the city's code.\n")
		city_name = raw_input("Please enter the city's name.\n")
		city_country = raw_input("Please enter the country where the city is located.\n")
		city_continent = raw_input("Please enter the continent where the city is located.\n")
		city_timezone = raw_input("Please enter the city's timezone.\n")
		city_latitude = raw_input("Please enter city's latitudinal direction. Put \"N\" for north and \"S\" for south.\n")
		city_latitude_value = raw_input("Please enter angle of the latitude.\n")
		city_longitude = raw_input("Please enter the city's longitudinal direction. Put \"W\" for west and \"E\" for east.\n")
		city_longitude_value = raw_input("Please enter the angle of the longitude.\n")
		city_population = raw_input("Please enter the city's population.\n")
		city_region = raw_input("Please enter the city's region.\n")

		city_timezone = self.convert_to_int(city_timezone)
		city_latitude_value = self.convert_to_int(city_latitude_value)
		city_longitude_value = self.convert_to_int(city_longitude_value)
		city_population = self.convert_to_int(city_population)
		city_region = self.convert_to_int(city_region)

		city = Metro()
		if city.set_code(city_code) and city.set_name(city_name) and city.set_country(city_country) and city.set_continent(city_continent) and city.set_timezone(city_timezone) and city.set_coordinates(city_latitude, city_latitude_value, city_longitude, city_longitude_value) and city.set_population(city_population) and city.set_region(city_region):
			self.map.add_vertex(city)
			print city_name + " has been successfully added."
			return True

		print "You've given invalid inputs."
		return False
Example #2
0
	def setUp(self):
		'''
		Sets up the test suite by creating a new Metro object before every test is ran.
		'''
		self.metro = Metro('a', 'b')
		self.test_metro = Metro('TESTMETRO', 'TEST METRO')
		self.test_city = Metro('TESTCITY', 'TEST CITY')
def _gen_test(fm_trans_json, fm_line_json, fm_edges_fix_json, input_vertex,
              turn_count):

    with open(fm_trans_json, 'r', encoding='UTF-8') as json_file:
        trans_data = json.load(json_file)

    with open(fm_line_json, 'r', encoding='UTF-8') as line_file:
        line_data = json.load(line_file)

    # with open('trans_classification_experiment_data.json', 'r', encoding='UTF-8') as data_file:
    # with open('trans_classification_list.json', 'r', encoding='UTF-8') as data_file:
    #     input_data = json.load(data_file)

    data_set = []

    for i in line_data:
        for j in line_data[i]:
            data_set.append(j + i)

    alpha_for_alpha_pruning = 1.2
    alpha_for_straight_forward = 1.2
    alpha_pruning_result = []
    alpha_pruning_result_avg = 0
    straight_forward_result = []
    straight_forward_result_avg = 0
    #
    # wb = openpyxl.load_workbook('result for cal.xlsx')
    # sheet = wb['Sheet1']

    # for k in range(1, 11):
    # start_time = time.time()
    reresult = []
    path_start_end_pairs = {}
    compare_paths_transdata = []

    a = 0
    # for j in range(0, len(input_data)):
    #     print(a)
    # reresult.extend(dijkstra_distributer.cal_dists(input_data[str(j)]['from'], input_data[str(j)]['to']))
    # reresult.extend(alpha_pruning.get_result(metro, input_data[str(j)]['from'], input_data[str(j)]['to'], alpha_for_alpha_pruning))
    # reresult.append(alpha_pruning2.get_result(metro, input_data[str(j)]['from'], input_data[str(j)]['to'], alpha_for_alpha_pruning)[0])
    # reresult.append(alpha_pruning2.get_result(metro, input_data[str(j)]['from'], input_data[str(j)]['to'], alpha_for_alpha_pruning)[0])
    # reresult.append(alpha_pruning2.get_result(metro, input_data[str(j)]['from'], input_data[str(j)]['to']))
    # a = a + 1
    # sett = []
    # for i in range(50000):
    #     ran_start, ran_end = rand_start_end(data_set)
    #     if not [ran_start, ran_end] in sett and not [ran_end, ran_start] in sett:
    #         sett.append([ran_start, ran_end])
    #         # print([ran_start, ran_end])
    #         print(len(sett))
    #     if len(sett) >= 10000:
    #         break

    # print(sett)
    # print(len(sett))

    # file_len = len(sett)
    file_len = 1999

    # make_dataset.make_data(len(sett), sett, input_vertex)

    metro = Metro(fm_trans_json, fm_line_json, fm_edges_fix_json)

    # fm_line_small_json = 'line_' + ','.join(input_vertex) + '_smalldata.json'
    #
    # with open(fm_line_small_json, 'r', encoding='UTF-8') as line_file:
    #     line_smalldata = json.load(line_file)

    ################ shortest_path구하는 곳
    # wb = openpyxl.load_workbook('cls.xlsx')
    # sheet = wb['Sheet1']
    # for i in range(file_len):
    #     # cal_shortest_path.get_result(metro, line_smalldata[str(i)]['from'], line_smalldata[str(i)]['to'], trans_data, i, sheet)
    #     cal_shortest_path.get_result(metro, sheet.cell(i + 2, 2).value, sheet.cell(i + 2, 3).value, trans_data, i, sheet)
    #     print(i)
    # wb.save('cls.xlsx')
    # /////////////////////////////////////
    # ############### cal_scores
    # wb = openpyxl.load_workbook('cls.xlsx')
    # sheet = wb['Sheet1']
    # for i in range(file_len):
    #     cal_scores.get_result(metro, sheet.cell(i + 2, 2).value, sheet.cell(i + 2, 3).value, sheet.cell(i+2, 8).value, trans_data, i, sheet)
    #     print(i)
    # wb.save('cls.xlsx')
    # /////////////////////////////////////
    # ############### cal_weights
    wb = openpyxl.load_workbook('cls.xlsx')
    sheet = wb['Sheet1']
    for i in range(file_len):
        cal_paths_weights.get_result(metro,
                                     sheet.cell(i + 2, 2).value,
                                     sheet.cell(i + 2, 3).value,
                                     sheet.cell(i + 2, 15).value, trans_data,
                                     i, sheet)
        print(i)
    wb.save('cls.xlsx')
    # /////////////////////////////////////
    cal_time = 0
    cal_time2 = 0
    # 9734

    # wb = openpyxl.load_workbook('cls.xlsx')
    # sheet = wb['Sheet1']
    #
    # for i in range(file_len):
    #     # reresult.append(cal_all_paths.get_result(metro, line_smalldata[str(i)]['from'], line_smalldata[str(i)]['to'], alpha_for_straight_forward))
    #     # if sheet.cell(i+2, 5).value == 6 or sheet.cell(i+2, 5).value == 7 or sheet.cell(i+2, 5).value == 8 or sheet.cell(i+2, 5).value == 9 or sheet.cell(i+2, 5).value == 10:
    #     #     pass
    #     # else:
    #     if sheet.cell(i+2, 5).value == 9:
    #         # if sheet.cell(i+2, 9).value == 0:
    #         start_time = time.time()
    #         reresult.append(cal_all_paths.get_result(metro, sheet.cell(i + 2, 2).value, sheet.cell(i + 2, 3).value,
    #                                                  alpha_for_straight_forward))
    #         end_time = time.time()
    #         cal_time = end_time - start_time
    #         print(i, "th _all path finish", ":", sheet.cell(i + 2, 2).value, sheet.cell(i + 2, 3).value, "->",
    #               cal_time)
    #         sheet.cell(row=i + 2, column=9, value=cal_time)
    #         wb.save('cls.xlsx')
    # else:
    #     start_time = time.time()
    #     reresult.append(cal_all_paths.get_result(metro, sheet.cell(i+2, 2).value, sheet.cell(i+2, 3).value, alpha_for_straight_forward))
    #     end_time = time.time()
    #     cal_time = end_time - start_time
    #     print(i, "th _all path finish", ":", sheet.cell(i+2, 2).value, sheet.cell(i+2, 3).value, "->", cal_time)
    #     sheet.cell(row=i + 2, column=9, value=cal_time)
    #     wb.save('cls.xlsx')
    # if i % 2000 == 0:
    # print("하는중 -> ", i , len(sett))

    # ################ alpha
    # wb = openpyxl.load_workbook('cls.xlsx')
    # sheet = wb['Sheet1']
    # # for kkkk in range(100):
    # for j in range(file_len):
    #     # if sheet.cell(j+2, 5).value == 6 or sheet.cell(j+2, 5).value == 7 or sheet.cell(j+2, 5).value == 8 or sheet.cell(j+2, 5).value == 9 or sheet.cell(j+2, 5).value == 10:
    #     #     pass
    #     # else:
    #     if sheet.cell(j + 2, 5).value == 9:
    #     # if sheet.cell(j+2, 5).value == 0 or sheet.cell(j+2, 5).value == 1 or sheet.cell(j+2, 5).value == 2 or sheet.cell(j+2, 5).value == 3 or sheet.cell(j + 2, 5).value == 4 or sheet.cell(j+2, 5).value == 5 or sheet.cell(j+2, 5).value == 6 or sheet.cell(j+2, 5).value == 7 or sheet.cell(j+2, 5).value == 8:
    #         # if sheet.cell(j+2, 10).value == 0:
    #         start_time2 = time.time()
    #         reresult.append(
    #             cal_alpha_pruning.get_result(metro, sheet.cell(j + 2, 2).value, sheet.cell(j + 2, 3).value,
    #                                          alpha_for_alpha_pruning))
    #         end_time2 = time.time()
    #         cal_time2 = end_time2 - start_time2
    #         print(j, "th _alpha path finish", ":", sheet.cell(j + 2, 2).value, sheet.cell(j + 2, 3).value, "->",
    #               cal_time2)
    #         sheet.cell(row=j + 2, column=10, value=cal_time2)
    #         sheet.cell(row=j + 2, column=15, value=str(reresult[-1][0]))
    #
    # wb.save('cls.xlsx')
    # print("fin")
    # alpha /////////////////////////
    # reresult.append(cal_alpha_pruning.get_result(metro, line_smalldata[str(j)]['from'], line_smalldata[str(j)]['to'], alpha_for_alpha_pruning))
    #     else:
    #         start_time2 = time.time()
    #         reresult.append(cal_alpha_pruning.get_result(metro, sheet.cell(j + 2, 2).value, sheet.cell(j + 2, 3).value, alpha_for_alpha_pruning))
    #         end_time2 = time.time()
    #         cal_time2 = end_time2 - start_time2
    #         print(j, "th _alpha path finish", ":", sheet.cell(j+2, 2).value, sheet.cell(j+2, 3).value, "->", cal_time2)
    #         sheet.cell(row=j + 2, column=10, value=cal_time2)
    #         wb.save('cls.xlsx')
    #         # if i % 2000 == 0:
    #         # print("하는중 -> ", i , len(sett))

    # print(cal_time / file_len)
    # print(cal_time2 / file_len)
    # print(len(trans_data['trans']))
    # wb.save('cls.xlsx')

    # wb = openpyxl.load_workbook('smalldata_result.xlsx')
    # sheet = wb['Sheet4']
    # sheet.cell(row=turn_count, column=3, value=len(trans_data['trans']))
    # sheet.cell(row=turn_count, column=4, value=len(sett))
    # sheet.cell(row=turn_count, column=5, value=cal_time/file_len)
    # sheet.cell(row=turn_count, column=6, value=cal_time2/file_len)
    # wb.save('smalldata_result.xlsx')


# for i in reresult[0]:
#     print(i)
#
# print(len(reresult[0]))

# set_of_reresult = []
# for d in range(len(reresult)):
#     set_of_reresult.append(reresult[0])

#
# index = 1
# for v in reresult:
#     compare_paths_transdata = []
#     for v2 in trans_data["trans"]:
#         if v2 in v[0]:
#             compare_paths_transdata.append(v2)
#     print(compare_paths_transdata)
#     len_compare_paths_transdata = len(compare_paths_transdata)
#
#     path_start_end_pairs[len_compare_paths_transdata] = (v[0], v[-1])ㅔ
#     print(len_compare_paths_transdata, ",", v[0], ",", v[-1])
#     print(v[0][0], v[0][-1])
#     print(v)
#     # 지나가는 경유역 개수 (동일역 포함
#     sheet.cell(row=index, column=1, value=len_compare_paths_transdata)
#     sheet.cell(row=index, column=2, value=v[0][0])
#     sheet.cell(row=index, column=3, value=v[0][-1])
#     sheet.cell(row=index, column=4, value=v[-1])
#     sheet.cell(row=index, column=5, value=str(v[0]))
#
#
#     # sheet.cell(row=index, column=4, value=v)
#     # sheet.cell(row=index, column=4).value = v
#     # for r in range(0, len(v)):
#     #     sheet.cell(row=index, column=4).value=v[r]
#     index = index + 1
#
#     # alpha_pruning_result.append(alpha_pruning.get_result(metro, input_data[str(j)]['from'], input_data[str(j)]['to'], alpha_for_alpha_pruning))
#     # print(alpha_pruning.get_result(metro, input_data[str(j)]['from'], input_data[str(j)]['to'], alpha_for_alpha_pruning))
#     # alpha_pruning_result_avg = alpha_pruning_result_avg + alpha_pruning_result[j][2]
#
# wb.save('result for cal.xlsx')

# end_time = time.time()
# print(alpha_pruning_result_avg / len(input_data))
# print("AP_WorkingTime: {} sec".format(end_time-start_time))
#
# sheet.cell(row=k, column=1, value="알파 프루닝 결과 평균치")
# sheet.cell(row=k, column=2, value=alpha_pruning_result_avg / len(input_data))
# sheet.cell(row=k, column=3, value="알파 프루닝 결과 시간")
# sheet.cell(row=k, column=4, value=format(end_time-start_time))
#
# wb.save('result.xlsx')

# start_time = time.time()
# for j in range(len(input_data)):
#     straight_forward_result.append(straight_forward.get_result(metro, input_data[str(j)]['from'], input_data[str(j)]['to'], alpha_for_straight_forward))
#     straight_forward_result_avg = straight_forward_result_avg + straight_forward_result[j][2]
#
# end_time = time.time()
# print(straight_forward_result_avg / len(input_data))
# print("SF_WorkingTime: {} sec".format(end_time - start_time))

# # 1.2 내 포함되는 데이터셋 만드는 코드
# time_out = 3
# deny = []
# for i in range(0, 10000):
#     inlist = []
#     ran_start, ran_end = rand_start_end(data_set)
#
#     done_counting = threading.Event()
#     # t = threading.Thread(target=alpha_pruning.get_result, args=(metro, ran_start, ran_end, alpha))
#     # t = threading.Thread(target=straight_forward.get_result, args=(metro, ran_start, ran_end, alpha_for_alpha_pruning))
#     t = threading.Thread(target=straight_forward_for_datasetting.get_result, args=(metro, ran_start, ran_end, alpha_for_alpha_pruning))
#     t.setDaemon(True)
#
#     t.start()
#
#     t.join(time_out)
#     done_counting.wait(time_out)
#     # if runtime out (difficult problem)
#     if t.is_alive():
#         # print(ran_start, ran_end)
#         pass
#     # if runtime out (easy problem)
#     else:
#         inlist = [ran_start, ran_end]
#         if not list in deny:
#             deny.append([ran_start, ran_end])
#             print(ran_start, ran_end)
#         else:
#             i = i - 1
#     # pass

# wb.save('result2.xlsx')
Example #4
0

def rand_start_end(_data_set):
    while True:
        _start = random.choice(_data_set)
        _end = random.choice(_data_set)

        if _start is not _end:
            break

    return _start, _end


alpha_for_alpha_pruning = 1.2
alpha_for_straight_forward = 1.0
metro = Metro()
alpha_pruning_result = []
alpha_pruning_result_avg = 0
straight_forward_result = []
straight_forward_result_avg = 0

wb = openpyxl.load_workbook('result for cal.xlsx')
sheet = wb['Sheet1']

# for k in range(1, 11):
# start_time = time.time()
reresult = []
path_start_end_pairs = {}
compare_paths_transdata = []

Example #5
0
    trafficfile.write(newdata)
    trafficfile.close()

except request.exception.RequestException as err:
    logging.error('Cannot request data from CET: ' + str(err))

except twitter.error.TwitterError:
    logging.error('Cannot post CET data to Twitter: ' + str(err))

# ### DATA FROM METRO AND CPTM ### #
logging.info('Retrieving data from Metro')
try:
    metropage = requests.get(METROURL)
    metrocontent = metropage.content.decode('utf-8')
    logging.info('Parsing metro status')
    metro = Metro()
    metro.feed(metrocontent)

    lines = metro.getLines()
    update = ''
    for idx in sorted(lines.keys()):
        if lines[idx]['status'] == 'Operação Normal' or lines[idx][
                'status'] == '':
            continue
        if update != '':
            update += ', '
        update += lines[idx]['color'] + ': ' + lines[idx]['status']

    if update != '':
        update = 'Situação #MetroSP. ' + update
        logging.info('Posting situacao MetroSP: ' + update)
Example #6
0
 def add_metro(self, metro_info):
     self.metros[metro_info['code']] = Metro(metro_info)
Example #7
0
 def create_metros(self, metro_list):
     for m in metro_list:
         self.metros[m['code']] = Metro(m)
Example #8
0
class TestMetro(unittest.TestCase):
	'''
	A unit test suite for the Metro class.
	'''
	
	def setUp(self):
		'''
		Sets up the test suite by creating a new Metro object before every test is ran.
		'''
		self.metro = Metro('a', 'b')
		self.test_metro = Metro('TESTMETRO', 'TEST METRO')
		self.test_city = Metro('TESTCITY', 'TEST CITY')

	def test_constructor(self):
		'''
		Tests the constructor of the Metro class by checking if all the values passed into the constructor
		match all of the object's instance variables
		'''
		self.assertEqual(self.metro.code, 'a')
		self.assertEqual(self.metro.name, 'b')
		self.assertEqual(self.metro.country, 'N/A')
		self.assertEqual(self.metro.continent, 'N/A')
		self.assertEqual(self.metro.timezone, 9001)
		self.assertEqual(self.metro.coordinates, {})
		self.assertEqual(self.metro.population, 0)
		self.assertEqual(self.metro.region, 9001)

	def test_set_code(self):
		'''
		Tests the set_code() function by changing the code of the metro to a valid code first, then attempting
		to change the code to an invalid input. 
		'''
		self.assertEqual(self.metro.set_code('METRO'), True)
		self.assertEqual(self.metro.set_code(1), False)
		self.assertEqual(self.metro.code, 'METRO')

	def test_set_name(self):
		'''
		Tests the set_name() function by changing the name of the metro to a valid name first, then attempting
		to change the name to an invalid input. 
		'''
		self.assertEqual(self.metro.set_name('METRO CITY'), True)
		self.assertEqual(self.metro.set_name(51), False)
		self.assertEqual(self.metro.name, 'METRO CITY')

	def test_set_country(self):
		'''
		Tests the set_country() function by changing the country of the metro to a valid country first, then attempting
		to change the country to an invalid input. 
		'''
		self.assertEqual(self.metro.set_country('METRO COUTNRY'), True)
		self.assertEqual(self.metro.set_country(72), False)
		self.assertEqual(self.metro.country, 'METRO COUTNRY')

	def test_set_continent(self):
		'''
		Tests the set_continent() function by changing the continent of the metro to a valid continent first, then attempting
		to change the continent to an invalid input. 
		'''
		self.assertEqual(self.metro.set_continent('METRO CONTINENT'), True)
		self.assertEqual(self.metro.set_continent([1]), False)
		self.assertEqual(self.metro.continent, 'METRO CONTINENT')

	def test_set_timezone(self):
		'''
		Tests the set_timezone() function by changing the timezone of the metro to a valid timezone first, then attempting
		to change the timezone to an invalid input. 
		'''
		self.assertEqual(self.metro.set_timezone(1024), True)
		self.assertEqual(self.metro.set_timezone("abcde"), False)
		self.assertEqual(self.metro.timezone, 1024)

	def test_set_coordinates(self):
		'''
		Tests the set_coordinates() function by changing the coordinates of the metro to a valid coordinates first, then attempting
		to change the coordinates to an invalid input. 
		'''
		self.assertEqual(self.metro.set_coordinates('S', 24, 'E', 52), True)
		self.assertEqual(self.metro.set_coordinates('E', 105, 2, -5), False)
		self.assertEqual(self.metro.coordinates, {'S': 24, 'E': 52})

	def test_set_population(self):
		'''
		Tests the set_population() function by changing the population of the metro to a valid population first, then attempting
		to change the population to an invalid input. 
		'''
		self.assertEqual(self.metro.set_population(12345678), True)
		self.assertEqual(self.metro.set_population('abcdefg'), False)
		self.assertEqual(self.metro.population, 12345678)

	def test_set_region(self):
		'''
		Tests the set_region() function by changing the region of the metro to a valid region first, then attempting
		to change the region to an invalid input. 
		'''
		self.assertEqual(self.metro.set_region(52), True)
		self.assertEqual(self.metro.set_region('abbbieieeir'), False)
		self.assertEqual(self.metro.region, 52)

	def test_add_outgoing(self):
		'''
		Tests the add_outgoing() function by adding metros to the outgoing list with add_outgoing() and checking if
		those metros have been added to the outgoing list instance variable. Tests for invalid input by checking if
		the invalid input isn't in the outgoing list.
		'''
		self.assertEqual(self.metro.add_outgoing(self.test_city), True)
		self.assertEqual(self.metro.add_outgoing(self.test_metro), True)
		self.assertEqual(self.metro.add_outgoing('CITY'), False)
		self.assertEqual(self.metro.outgoing[0], self.test_city)
		self.assertEqual(self.metro.outgoing[1], self.test_metro)
		self.assertEqual('CITY' not in self.metro.outgoing, True)

	def test_remove_outgoing(self):
		'''
		Tests the remove_outgoing() function by adding metros to the outgoing list, then removing the element that
		was just added. Afterwards, remove_outgoing() is called with invalid inputs, which shouldn't do anything.
		'''
		self.assertEqual(self.metro.add_outgoing(self.test_city), True)
		self.assertEqual(self.metro.remove_outgoing(self.test_city), True)
		self.assertEqual(self.metro.remove_outgoing(self.test_metro), False)
		self.assertEqual(self.metro.remove_outgoing(5), False)
		self.assertEqual(self.test_city not in self.metro.outgoing, True)

	def test_add_incoming(self):
		'''
		Tests the add_incoming() function by adding metros to the outgoing list with add_incoming() and checking if
		those metros have been added to the incoming list instance variable. Tests for invalid input by checking if
		the invalid input isn't in the incoming list.
		'''
		self.assertEqual(self.metro.add_incoming(self.test_city), True)
		self.assertEqual(self.metro.add_incoming(self.test_metro), True)
		self.assertEqual(self.metro.add_incoming('CITY'), False)
		self.assertEqual(self.metro.incoming[0], self.test_city)
		self.assertEqual(self.metro.incoming[1], self.test_metro)
		self.assertEqual('CITY' not in self.metro.incoming, True)

	def test_remove_incoming(self):
		'''
		Tests the remove_incoming() function by adding metros to the incoming list, then removing the element that
		was just added. Afterwards, remove_outgoing() is called with invalid inputs, which shouldn't do anything.
		'''
		self.assertEqual(self.metro.add_incoming(self.test_city), True)
		self.assertEqual(self.metro.remove_incoming(self.test_city), True)
		self.assertEqual(self.metro.remove_incoming(self.test_metro), False)
		self.assertEqual(self.metro.remove_incoming('aaassdeeee'), False)
		self.assertEqual(self.test_city not in self.metro.incoming, True)