MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / DiGraph_add_nodes_from

Function DiGraph_add_nodes_from

cpp_easygraph/classes/directed_graph.cpp:186–232  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

184}
185
186py::object DiGraph_add_nodes_from(py::args args, py::kwargs kwargs) {
187 DiGraph& self = args[0].cast<DiGraph&>();
188 self.dirty_nodes = true;
189 self.dirty_adj = true;
190 py::list nodes_for_adding = py::list(args[1]);
191 for (int i = 0; i < py::len(nodes_for_adding); i++) {
192 bool newnode;
193 py::dict attr = kwargs;
194 py::dict newdict, ndict;
195 py::object n = nodes_for_adding[i];
196 try {
197 newnode = !self.node_to_id.contains(n);
198 newdict = attr;
199 } catch (const py::error_already_set&) {
200 PyObject *type, *value, *traceback;
201 PyErr_Fetch(&type, &value, &traceback);
202 if (PyErr_GivenExceptionMatches(PyExc_TypeError, type)) {
203 py::tuple n_pair = n.cast<py::tuple>();
204 n = n_pair[0];
205 ndict = n_pair[1].cast<py::dict>();
206 newnode = !self.node_to_id.contains(n);
207 newdict = attr.attr("copy")();
208 newdict.attr("update")(ndict);
209 } else {
210 PyErr_Restore(type, value, traceback);
211 return py::none();
212 }
213 }
214 if (newnode) {
215 if (n.is_none()) {
216 PyErr_Format(PyExc_ValueError, "None cannot be a node");
217 return py::none();
218 }
219 DiGraph_add_one_node(self, n);
220 }
221 node_t id = self.node_to_id[n].cast<node_t>();
222 py::list items = py::list(newdict.attr("items")());
223 for (int i = 0; i < len(items); i++) {
224 py::tuple kv = items[i].cast<py::tuple>();
225 py::object pkey = kv[0];
226 std::string weight_key = weight_to_string(pkey);
227 weight_t value = kv[1].cast<weight_t>();
228 self.node[id].insert(std::make_pair(weight_key, value));
229 }
230 }
231 return py::none();
232}
233
234py::object DiGraph_remove_node(DiGraph& self, py::object node_to_remove) {
235 self.dirty_nodes = true;

Callers

nothing calls this directly

Calls 2

DiGraph_add_one_nodeFunction · 0.85
weight_to_stringFunction · 0.85

Tested by

no test coverage detected