| 332 | } |
| 333 | |
| 334 | py::object DiGraph_add_edges_from(py::args args, py::kwargs attr) { |
| 335 | DiGraph& self = args[0].cast<DiGraph&>(); |
| 336 | self.dirty_nodes = true; |
| 337 | self.dirty_adj = true; |
| 338 | py::list ebunch_to_add = py::list(args[1]); |
| 339 | for (int i = 0; i < len(ebunch_to_add); i++) { |
| 340 | py::list e = py::list(ebunch_to_add[i]); |
| 341 | py::object u, v; |
| 342 | py::dict dd; |
| 343 | switch (len(e)) { |
| 344 | case 2: { |
| 345 | u = e[0]; |
| 346 | v = e[1]; |
| 347 | break; |
| 348 | } |
| 349 | case 3: { |
| 350 | u = e[0]; |
| 351 | v = e[1]; |
| 352 | dd = e[2].cast<py::dict>(); |
| 353 | break; |
| 354 | } |
| 355 | default: { |
| 356 | PyErr_Format(PyExc_ValueError, "Edge tuple %R must be a 2 - tuple or 3 - tuple.", e.ptr()); |
| 357 | return py::none(); |
| 358 | } |
| 359 | } |
| 360 | node_t u_id, v_id; |
| 361 | if (!self.node_to_id.contains(u)) { |
| 362 | if (u.is_none()) { |
| 363 | PyErr_Format(PyExc_ValueError, "None cannot be a node"); |
| 364 | return py::none(); |
| 365 | } |
| 366 | u_id = DiGraph_add_one_node(self, u); |
| 367 | |
| 368 | } else { |
| 369 | u_id = self.node_to_id[u].cast<node_t>(); |
| 370 | } |
| 371 | if (!self.node_to_id.contains(v)) { |
| 372 | if (v.is_none()) { |
| 373 | PyErr_Format(PyExc_ValueError, "None cannot be a node"); |
| 374 | return py::none(); |
| 375 | } |
| 376 | v_id = DiGraph_add_one_node(self, v); |
| 377 | |
| 378 | } else { |
| 379 | v_id = self.node_to_id[v].cast<node_t>(); |
| 380 | } |
| 381 | auto datadict = self.adj[u_id].count(v_id) ? self.adj[u_id][v_id] : node_attr_dict_factory(); |
| 382 | py::list items = py::list(attr.attr("items")()); |
| 383 | items.attr("extend")(py::list(dd.attr("items")())); |
| 384 | for (int j = 0; j < py::len(items); j++) { |
| 385 | py::tuple kv = items[j].cast<py::tuple>(); |
| 386 | py::object pkey = kv[0]; |
| 387 | std::string weight_key = weight_to_string(pkey); |
| 388 | weight_t value = kv[1].cast<weight_t>(); |
| 389 | datadict.insert(std::make_pair(weight_key, value)); |
| 390 | } |
| 391 | // Warning: in Graph.py the edge attr is directed assigned by the dict extended from the original attr |
nothing calls this directly
no test coverage detected