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


Python Graph.get_edge方法代码示例

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


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

示例1: test_get_edge

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_edge [as 别名]
  def test_get_edge(self):
    g = Graph()
    v = Vertex('v')
    w = Vertex('w')
    x = Vertex('x')
    e1 = Edge(v,w)
    e2 = Edge(w,x)
    e3 = Edge(v,x)

    # Query for edge from an empty graph
    self.assertEqual(g.get_edge(v, w), None)

    # Query for a non existent edge with one vertex present
    g.clear()
    g.add_vertex(v)
    self.assertEqual(g.get_edge(v, w), None)

    # Query for a non existent edge with both vertices present
    g.clear()
    g.add_vertex(v)
    g.add_vertex(w)
    self.assertEqual(g.get_edge(v, w), None)

    # Query for an existent edge
    g.clear()
    g.add_edge(e1)
    g.add_edge(e2)
    self.assertEqual(g.get_edge(v,w), e1)
开发者ID:shubhamgupta30,项目名称:think_bayes,代码行数:30,代码来源:Graph_test.py

示例2: GraphTest

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_edge [as 别名]
class GraphTest(unittest.TestCase):

    def setUp(self):
        self.v = Vertex('v')
        self.w = Vertex('w')
        self.x = Vertex('x')
        self.y = Vertex('y')
        self.z = Vertex('z')
        self.e = Edge(self.v, self.w)
        self.e2 = Edge(self.v, self.x)
        self.g = Graph([self.v, self.w ,self.x, self.y],[self.e, self.e2])

    def test_get_edge_not_exist(self):
        edge = self.g.get_edge(self.v, self.y)
        self.assertIsNone(edge)

    def test_get_edge_exist(self):
        edge = self.g.get_edge(self.v, self.w)
        self.assertEqual(edge, self.e)

    def test_remove_edge_is_removed( self ):
        '''
        La prueba checa si el Edge esta por ahi.

        Depende de la funcion de get_edge
        '''
        edge = self.g.remove_edge( self.e )
        self.assertIsNone( self.g.get_edge( self.v, self.w ) )
开发者ID:crisvolta,项目名称:Grafo-TDD,代码行数:30,代码来源:test_Graph.py

示例3: test_get_edge

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_edge [as 别名]
    def test_get_edge(self):

        v = Vertex(1)
        v2 = Vertex(2)
        e = Edge(v, v2)

        g = Graph(vs=[v,v2], es=[e])

        self.assertEqual(g.get_edge(v, v), None)
        self.assertEqual(g.get_edge(v, v2), e)
开发者ID:tsoporan,项目名称:think_complex,代码行数:12,代码来源:tests.py

示例4: GraphTest

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_edge [as 别名]
class GraphTest(unittest.TestCase):

    def setUp(self):
        self.v = Vertex('v')
        self.w = Vertex('w')
        self.x = Vertex('x')
        self.y = Vertex('y')
        self.z = Vertex('z')
        self.e = Edge(self.v, self.w)
        self.e2 = Edge(self.v, self.x)
        self.g = Graph([self.v, self.w ,self.x, self.y],[self.e, self.e2])

    def test_get_edge_not_exist(self):
        edge = self.g.get_edge(self.v, self.y)
        self.assertIsNone(edge)

    def test_vertices_is_list(self):
        ''' Verifica que el retorno de la funcion vertices sea una lista '''
        vs = self.g.vertices()
        self.assertEqual( list, type( vs ) )

    def test_vertices_exist(self):
        ''' Todos los vertices incluidos en la lista existen en el grafo '''
        vs = ['v','w','x','y' ]
        self.assertEqual( sorted(vs), self.g.vertices() )

    def test_vertices_not_exist(self):
        ''' El vertice z no existe en el grafo generado en setup() '''
        vs =  ['v','w','x','y','z' ]
        self.assertNotEqual( sorted(vs), self.g.vertices() )

    def test_get_edge_exist(self):
        edge = self.g.get_edge(self.v, self.w)
        self.assertEqual(edge, self.e)

    def test_remove_edge_is_removed( self ):
        '''
        La prueba checa si el Edge esta por ahi.

        Depende de la funcion de get_edge
        '''
        edge = self.g.remove_edge( self.e )
        self.assertIsNone( self.g.get_edge( self.v, self.w ) )

    def test_vertex_repr(self):
        self.assertEqual( repr(self.v), "Vertex('v')" )

    def test_edge_one_vertex(self):
        self.assertRaises( ValueError, Edge, self.v )

    def test_edge_repr(self):
        self.assertEqual( repr(self.e),
                    "Edge(Vertex('v'), Vertex('w'))" )
开发者ID:mimex,项目名称:Grafo-TDD,代码行数:55,代码来源:test_Graph.py

示例5: test_graph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_edge [as 别名]
 def test_graph(self):
     v = Vertex('v')
     w = Vertex('w')
     self.assertEqual(repr(v), "Vertex('v')")
     
     e = Edge(v, w)
     self.assertEqual(repr(e), "Edge(Vertex('v'), Vertex('w'))")
     
     g = Graph([v, w], [e])
     self.assertEqual(repr(g), "{Vertex('w'): {Vertex('v'): Edge(Vertex('v'), Vertex('w'))}, Vertex('v'): {Vertex('w'): Edge(Vertex('v'), Vertex('w'))}}")
     
     e2 = g.get_edge(v, w)
     self.assertEqual(e, e2)
     
     e3 = g.get_edge(v, v)
     self.assertEqual(e3, None)
     
     vs = [Vertex(c) for c in 'abcd']
     g = Graph(vs)
     g.add_regular_edges(3)
     
     for v in g.vertices():
         es = g.out_edges(v)
         self.assertEqual(len(es), 3)
         
         vs = g.out_vertices(v)
         self.assertEqual(len(vs), 3)
         
     g.remove_edge(Edge(Vertex('a'), Vertex('c')))
     
     vs = g.vertices()
     self.assertEqual(len(vs), 4)
     
     es = g.edges()
     self.assertEqual(len(es), 5)
     
     g.add_all_edges()
     es = g.edges()
     self.assertEqual(len(es), 6)
     
     g2 = eval(repr(g))
     self.assertEqual(g, g2)
开发者ID:paulmouzas,项目名称:think-complexity,代码行数:44,代码来源:GraphTest.py

示例6: TestGraph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_edge [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

示例7: __init__

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_edge [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

示例8: Vertex

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import get_edge [as 别名]
 w = Vertex(2)
 e = Edge(v,w)
 print e
 g = Graph([v, w], [e])
 pprint(g, width=1)
 pass
 
 v = Vertex('v')
 w = Vertex('w')
 e = Edge(v,w)
 print e
 
 g = Graph([v, w], [e])
 pprint(g, width=1)
 
 e1 = g.get_edge(v, v)
 e2 = g.get_edge(w, v)
 print e1, e2
 
 e1 = Edge(v, v)
 e2 = Edge(w, w)
 g = Graph([v, w], [e, e1, e2])
 g.remove_edge(Edge(v, v))
 pprint(g, width=1)
 
 print g.vertices()
 
 print g.edges()
 
 u = Vertex('u')
 g = Graph([u, v, w])
开发者ID:nikos-daniilidis,项目名称:python-complexity,代码行数:33,代码来源:GraphTest.py


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