def set_att(self, graph, *, inplace=False): """Set an attribute on all vertices in a graph, att -- attribute to set val -- value to set to inplace -- Set true to mutate graph (otherwise makes a copy). Default is False. """ g = graph.copy() if not inplace else graph nx.set_vertex_attributes(g, self.att, self.val) return g
def test_add_property(self): g1 = graph("linear")() u = nx.DiGraph() u.add_vertex("port1") nx.set_vertex_attributes(u, "firewall", True) g2 = _update(g1, u) self.assertTrue(g2.vertex["port1"]["firewall"]) self.assertEqual(set(g1.vertices()), set(g2.vertices()))
def test_new_prop(self): attribute = "att2" val = 34 g1 = graph("linear")() nx.set_vertex_attributes(g1, "att", 0) g2 = g1.copy() nx.set_vertex_attributes(g2, attribute, val) delta = diff(g1,g2) changed = list(delta.keys())[0] self.assertEqual(len(g1), len(delta)) self.assertTrue("props" in delta[changed].keys()) self.assertEqual("prop added", delta[changed]["props"][attribute])
def test_del_prop(self): changed = "port1" attribute = "att2" g1 = graph("linear")() nx.set_vertex_attributes(g1, "att", 0) nx.set_vertex_attributes(g1, attribute, 13) g2 = g1.copy() del g2.vertex[changed][attribute] delta = diff(g1,g2) self.assertEqual(1, len(delta)) self.assertEqual(changed, list(delta.keys())[0]) self.assertTrue("props" in delta[changed].keys()) self.assertEqual("prop removed", delta[changed]["props"][attribute])
def test_changed_prop(self): g1 = graph("linear")() nx.set_vertex_attributes(g1, "att", 0) g2 = g1.copy() changed = "port3" attribute = "att" new_val = 34 g2.vertex[changed][attribute] = new_val delta = diff(g1,g2) self.assertEqual(1, len(delta)) self.assertEqual(changed, list(delta.keys())[0]) self.assertTrue("props" in delta[changed].keys()) self.assertEqual(new_val, delta[changed]["props"][attribute])