Skip to content

Commit

Permalink
feat: add prefixattr=... argument to Graph.write_graphml(), fixes #759
Browse files Browse the repository at this point in the history
  • Loading branch information
ntamas committed Feb 14, 2024
1 parent e594422 commit ded8333
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/_igraph/graphobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -9728,18 +9728,19 @@ PyObject *igraphmodule_Graph_write_pajek(igraphmodule_GraphObject * self,
PyObject *igraphmodule_Graph_write_graphml(igraphmodule_GraphObject * self,
PyObject * args, PyObject * kwds)
{
PyObject *fname = NULL;
static char *kwlist[] = { "f", NULL };
PyObject *fname = NULL, *prefixattr_o = Py_True;
static char *kwlist[] = { "f", "prefixattr", NULL };
igraphmodule_filehandle_t fobj;

if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &fname))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, &fname, &prefixattr_o))
return NULL;

if (igraphmodule_filehandle_init(&fobj, fname, "w"))
return NULL;

if (igraph_write_graph_graphml(&self->g, igraphmodule_filehandle_get(&fobj),
/*prefixattr=*/ 1)) {
if (igraph_write_graph_graphml(
&self->g, igraphmodule_filehandle_get(&fobj), PyObject_IsTrue(prefixattr_o)
)) {
igraphmodule_handle_igraph_error();
igraphmodule_filehandle_destroy(&fobj);
return NULL;
Expand Down Expand Up @@ -16930,9 +16931,13 @@ struct PyMethodDef igraphmodule_Graph_methods[] = {
/* interface to igraph_write_graph_edgelist */
{"write_graphml", (PyCFunction) igraphmodule_Graph_write_graphml,
METH_VARARGS | METH_KEYWORDS,
"write_graphml(f)\n--\n\n"
"write_graphml(f, prefixattr=True)\n--\n\n"
"Writes the graph to a GraphML file.\n\n"
"@param f: the name of the file to be written or a Python file handle\n"
"@param prefixattr: whether attribute names in the written file should be\n"
" prefixed with C{g_}, C{v_} and C{e_} for graph, vertex and edge\n"
" attributes, respectively. This might be needed to ensure the uniqueness\n"
" of attribute identifiers in the written GraphML file.\n"
},
/* interface to igraph_write_graph_leda */
{"write_leda", (PyCFunction) igraphmodule_Graph_write_leda,
Expand Down
1 change: 1 addition & 0 deletions tests/test_foreign.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ def testGraphML(self):
self.assertTrue("name" in g.vertex_attributes())

g.write_graphml(tmpfname)
g.write_graphml(tmpfname, prefixattr=False)

def testGraphMLz(self):
with temporary_file(
Expand Down

0 comments on commit ded8333

Please sign in to comment.