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


Python Graph.get_vertex_from_code方法代码示例

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


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

示例1: TestGraph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_vertex_from_code [as 别名]
class TestGraph(unittest.TestCase):
	'''
	A unit test suite for the Graph class.
	'''

	def setUp(self):
		'''
		Sets up the test suite by creating a new graph before every test is ran
		'''
		city_pair = ['CHI', 'TOR']
		metro_pair = ['SHA', 'NYC']
		shorter_route_a = ['CHI', 'TES']
		shorter_route_b = ['TES', 'TOR']
		routes = [Route(city_pair, 1337), Route(metro_pair, 9001), Route(shorter_route_a, 50), Route(shorter_route_b, 100)]
		metros = [Metro('cityCode1', 'cityName1', 'c', 'd', 'e', 'f', 'g', 'h'), Metro('cityCode2', 'cityName2', 'c', 'd', 'e', 'f', 'g', 'h')]
		chi = Metro('CHI', 'Chicago')
		tor = Metro('TOR', 'Toronto')
		tes = Metro('TES', 'Test City')
		chi.outgoing.append(tor)
		chi.outgoing.append(tes)
		tor.incoming.append(chi)
		tor.incoming.append(tes)
		tes.outgoing.append(tor)
		tes.incoming.append(chi)
		metros.append(chi)
		metros.append(tor)
		metros.append(tes)
		self.graph = Graph(metros, routes)

	def test_constructor(self):
		'''
		Tests the constructor of the Graph class by checking if all the values passed into the constructor
		match all of the object's instance variables
		'''
		self.assertEqual('CHI', self.graph.edges[0].ports[0])
		self.assertEqual('TOR', self.graph.edges[0].ports[1])
		self.assertEqual(1337, self.graph.edges[0].distance)
		self.assertEqual('SHA', self.graph.edges[1].ports[0])
		self.assertEqual('NYC', self.graph.edges[1].ports[1])
		self.assertEqual(9001, self.graph.edges[1].distance)
		self.assertEqual(self.graph.vertices[0].code, 'cityCode1')
		self.assertEqual(self.graph.vertices[0].name, 'cityName1')
		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, 'cityCode2')
		self.assertEqual(self.graph.vertices[1].name, 'cityName2')
		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_vertex(self):
		'''
		Tests the add_vertex() function by adding metros to the vertices list with add_vertex() and checking if
		those metros have been added to the vertices list instance variable.
		'''
		self.graph.add_vertex(Metro('cityCode3', 'cityName3', 'c', 'd', 'e', 'f', 'g', 'h'))
		new_city = self.graph.get_vertex_from_code('cityCode3')
		self.assertEqual(new_city.code, 'cityCode3')
		self.assertEqual(new_city.name, 'cityName3')
		self.assertEqual(new_city.country, 'c')
		self.assertEqual(new_city.continent, 'd')
		self.assertEqual(new_city.timezone, 'e')
		self.assertEqual(new_city.coordinates, 'f')
		self.assertEqual(new_city.population, 'g')
		self.assertEqual(new_city.region, 'h')

	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):
		'''
#.........这里部分代码省略.........
开发者ID:KenYHT,项目名称:AirlineConsole,代码行数:103,代码来源:GraphTest.py

示例2: __init__

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_vertex_from_code [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_code方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。