当前位置: 首页>>代码示例>>Python>>正文


Python Graph.get_vertex_from_name方法代码示例

本文整理汇总了Python中Graph.Graph.get_vertex_from_name方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.get_vertex_from_name方法的具体用法?Python Graph.get_vertex_from_name怎么用?Python Graph.get_vertex_from_name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Graph.Graph的用法示例。


在下文中一共展示了Graph.get_vertex_from_name方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestGraph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_vertex_from_name [as 别名]

#.........这里部分代码省略.........
	def test_set_vertices(self):
		'''
		Tests the set_vertices() function by initializing a list of metros and setting the vertices list instance variable
		of the Graph object equal to the list we've initialized by calling set_vertices(). Then we iterate through the list and checking if 
		the list consists of the elements of the original vertices list we've initialized. 
		'''
		metros = [Metro('cityCode3', 'cityName3', 'c', 'd', 'e', 'f', 'g', 'h'), Metro('cityCode4', 'cityName4', 'c', 'd', 'e', 'f', 'g', 'h')]
		self.graph.set_vertices(metros)
		self.assertEqual(self.graph.vertices[0].code, 'cityCode3')
		self.assertEqual(self.graph.vertices[0].name, 'cityName3')
		self.assertEqual(self.graph.vertices[0].country, 'c')
		self.assertEqual(self.graph.vertices[0].continent, 'd')
		self.assertEqual(self.graph.vertices[0].timezone, 'e')
		self.assertEqual(self.graph.vertices[0].coordinates, 'f')
		self.assertEqual(self.graph.vertices[0].population, 'g')
		self.assertEqual(self.graph.vertices[0].region, 'h')
		self.assertEqual(self.graph.vertices[1].code, 'cityCode4')
		self.assertEqual(self.graph.vertices[1].name, 'cityName4')
		self.assertEqual(self.graph.vertices[1].country, 'c')
		self.assertEqual(self.graph.vertices[1].continent, 'd')
		self.assertEqual(self.graph.vertices[1].timezone, 'e')
		self.assertEqual(self.graph.vertices[1].coordinates, 'f')
		self.assertEqual(self.graph.vertices[1].population, 'g')
		self.assertEqual(self.graph.vertices[1].region, 'h')

	def test_add_edge(self):
		'''
		Tests the add_edge() function by adding routes to the edges list with add_edge() and checking if
		those routes have been added to the edges list instance variable.
		'''
		self.graph.add_edge('YOL', 'SWA', 420)
		self.assertEqual('YOL', self.graph.edges[4].ports[0])
		self.assertEqual('SWA', self.graph.edges[4].ports[1])
		self.assertEqual(420, self.graph.edges[4].distance)

	def test_set_edges(self):
		'''
		Tests the set_edges() function by initializing a list of edges and setting the edges list instance variable
		of the Route object equal to the list we've initialized by calling set_edges(). Then we iterate through the list and checking if 
		the list consists of the elements of the original edges list we've initialized. 
		'''
		flightPair = ['TES', 'TIN']
		pair = ['GRA', 'PHS']
		routes = [Route(flightPair, 12345), Route(pair, 98765)]
		self.graph.set_edges(routes)
		self.assertEqual('TES', self.graph.edges[0].ports[0])
		self.assertEqual('TIN', self.graph.edges[0].ports[1])
		self.assertEqual(12345, self.graph.edges[0].distance)
		self.assertEqual('GRA', self.graph.edges[1].ports[0])
		self.assertEqual('PHS', self.graph.edges[1].ports[1])
		self.assertEqual(98765, self.graph.edges[1].distance)

	def test_get_vertex_from_code(self):
		'''
		Tests the get_vertex_from_code() function by calling the function with known codes and checking if the retrieved metros are
		equal to their position in the vertices list in the graph.
		'''
		metro = self.graph.get_vertex_from_code('cityCode1')
		city = self.graph.get_vertex_from_code('cityCode2')
		self.assertEqual(metro, self.graph.vertices[0])
		self.assertEqual(city, self.graph.vertices[1])

	def test_get_vertex_from_name(self):
		'''
		Tests the get_vertex_from_name() function by calling the function with known names and checking if the retrieved metros are
		equal to their position in the vertices list in the graph.
		'''
		metro = self.graph.get_vertex_from_name('cityName1')
		city = self.graph.get_vertex_from_name('cityName2')
		self.assertEqual(metro, self.graph.vertices[0])
		self.assertEqual(city, self.graph.vertices[1])

	def test_get_edge(self):
		'''
		Tests the get_edge() function by adding test vertices and edges to the graph, then calling the function and checking if 
		the edge that is returned is the same as the one made in the beginning. Invalid inputs are tested by checking if None
		is returned when invalid codes are inputed.
		'''
		pyt = Metro('PYT', 'Pyt')
		hon = Metro('HON', 'Hon')
		self.graph.add_vertex(pyt)
		self.graph.add_vertex(hon)
		self.graph.add_edge('PYT', 'HON', 510)
		result = self.graph.get_edge(pyt.code, hon.code)
		self.assertEqual(pyt.code, result.ports[0])
		self.assertEqual(hon.code, result.ports[1])
		false_result = self.graph.get_edge('aaa', 'bbb')
		self.assertEqual(false_result, None)

	def test_shortest_path(self):
		'''
		Tests the shortest_path() function getting existing nodes, and checking if the shortest path outputed by the function calling
		is the same as the shortest path of the graph. It also tests for invalid inputs.
		'''
		chi = self.graph.get_vertex_from_name('Chicago')
		tor = self.graph.get_vertex_from_name('Toronto')
		tes = self.graph.get_vertex_from_name('Test City')
		shortest_path_sol = [chi, tes, tor]
		short_path = self.graph.shortest_path(chi, tor)
		self.assertEqual(shortest_path_sol, short_path)
开发者ID:KenYHT,项目名称:AirlineConsole,代码行数:104,代码来源:GraphTest.py

示例2: __init__

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_vertex_from_name [as 别名]
class CSAir:
	'''
	A CSAir class containing the logic behind the CSAir program.
	'''	
	
	def __init__(self, url):
		'''
		The constructor for the CSAir class. It loads the data from a JSON file from the given url, then creates a map containing all
		the cities, flights, and the information associated the the cities and flights.
		'''
		json_data = open(url)
		data = json.load(json_data)
		self.map = Graph() 
		self.data_sources = []
		self.load_file('../json/map_data.json')
		self.load_file('../json/map_addon.json')
		self.load_file('../json/saved_map_data.json')
		self.options = Options(self.map)
		self.init_options()
		json_data.close()

	
	def init_options(self):
		'''
		Initializes the list of options. It creates a list of pairs, where the first element in the pair is the string describing the 
		function call, which is the second element of the pair.
		'''
		self.options = [
			["List all cities", self.options.list_cities],
			["List all flights", self.options.list_routes],
			["Look up all flights from a city", self.options.find_routes_from],
			["Look up all flights to a city", self.options.find_routes_to],
			["Look up a city details", self.options.check_city],
			["Check distance between two cities", self.options.find_distance],
			["Check statistics", self.options.check_statistics],
			["See the route map", self.options.generate_flight_map],
			["Remove a city", self.remove_city],
			["Remove a route", self.remove_route],
			["Add a city", self.add_city],
			["Add a route", self.add_route],
			["Edit a city", self.edit_city],
			["Get information about a route", self.options.user_route_information],
			["Find the shortest route between two cities", self.options.find_shortest_route],
			["Save changes", self.save_network_to_disk],
			["Exit", self.exit]
		]

	def init_data_sources(self, data_sources):
		'''
		Stores the data sources by parsing the data from the JSON file.
		'''
		sources = [source for source in data_sources if source not in self.data_sources or not self.data_sources]
		for source in sources:
			self.data_sources.append(source)

	def init_metros(self, metros_data):
		'''
		Initializes the map with cities by parsing the data from the JSON file.
		'''
		metros = [Metro(metro['code'], metro['name'], metro['country'], metro['continent'], metro['timezone'], metro['coordinates'], metro['population'], metro['region']) for metro in metros_data if self.map.get_vertex_from_code(metro['code']) is None]
		for metro in metros:
			self.map.add_vertex(metro)

	def init_routes(self, routes_data):
		'''
		Initializes the map with flights by parsing the data from the JSON file.
		'''
		routes = []
		for route in routes_data:
			if self.map.get_edge(route['ports'][0], route['ports'][1]) is None:
				routes.append(Route(route['ports'], route['distance']))
				reverse_ports = [route['ports'][1], route['ports'][0]]
				routes.append(Route(reverse_ports, route['distance']))
				src = self.map.get_vertex_from_code(route['ports'][0])
				dest = self.map.get_vertex_from_code(route['ports'][1])
				src.add_outgoing(dest)
				src.add_incoming(dest)
				dest.add_outgoing(src)
				dest.add_incoming(src)

		for route in routes:
			self.map.add_edge(route.ports[0], route.ports[1], route.distance)

	def remove_city(self):
		'''
		Removes the city with the inputed city name from the map. All the other cities and routes are updated with the removal.
		True is returned if the removal was successful, otherwise False.
		'''
		city_name = raw_input("\nPlease enter the city you would like to remove.\n")
		metro = self.map.get_vertex_from_name(city_name)
		if metro is None:
			print "There is no city " + city_name + " being serviced by CSAir."
			return False
		else:
			for city in self.map.vertices: # remove the given city from the outgoing and incoming list of all of the other cities
				if metro in city.incoming:
					city.incoming.remove(metro)
				
				if metro in city.outgoing:	
					city.outgoing.remove(metro)
#.........这里部分代码省略.........
开发者ID:KenYHT,项目名称:AirlineConsole,代码行数:103,代码来源:CSAir.py


注:本文中的Graph.Graph.get_vertex_from_name方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。