Skip to content

Commit ded8333

Browse files
committed
feat: add prefixattr=... argument to Graph.write_graphml(), fixes #759
1 parent e594422 commit ded8333

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/_igraph/graphobject.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9728,18 +9728,19 @@ PyObject *igraphmodule_Graph_write_pajek(igraphmodule_GraphObject * self,
97289728
PyObject *igraphmodule_Graph_write_graphml(igraphmodule_GraphObject * self,
97299729
PyObject * args, PyObject * kwds)
97309730
{
9731-
PyObject *fname = NULL;
9732-
static char *kwlist[] = { "f", NULL };
9731+
PyObject *fname = NULL, *prefixattr_o = Py_True;
9732+
static char *kwlist[] = { "f", "prefixattr", NULL };
97339733
igraphmodule_filehandle_t fobj;
97349734

9735-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &fname))
9735+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, &fname, &prefixattr_o))
97369736
return NULL;
97379737

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

9741-
if (igraph_write_graph_graphml(&self->g, igraphmodule_filehandle_get(&fobj),
9742-
/*prefixattr=*/ 1)) {
9741+
if (igraph_write_graph_graphml(
9742+
&self->g, igraphmodule_filehandle_get(&fobj), PyObject_IsTrue(prefixattr_o)
9743+
)) {
97439744
igraphmodule_handle_igraph_error();
97449745
igraphmodule_filehandle_destroy(&fobj);
97459746
return NULL;
@@ -16930,9 +16931,13 @@ struct PyMethodDef igraphmodule_Graph_methods[] = {
1693016931
/* interface to igraph_write_graph_edgelist */
1693116932
{"write_graphml", (PyCFunction) igraphmodule_Graph_write_graphml,
1693216933
METH_VARARGS | METH_KEYWORDS,
16933-
"write_graphml(f)\n--\n\n"
16934+
"write_graphml(f, prefixattr=True)\n--\n\n"
1693416935
"Writes the graph to a GraphML file.\n\n"
1693516936
"@param f: the name of the file to be written or a Python file handle\n"
16937+
"@param prefixattr: whether attribute names in the written file should be\n"
16938+
" prefixed with C{g_}, C{v_} and C{e_} for graph, vertex and edge\n"
16939+
" attributes, respectively. This might be needed to ensure the uniqueness\n"
16940+
" of attribute identifiers in the written GraphML file.\n"
1693616941
},
1693716942
/* interface to igraph_write_graph_leda */
1693816943
{"write_leda", (PyCFunction) igraphmodule_Graph_write_leda,

tests/test_foreign.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ def testGraphML(self):
360360
self.assertTrue("name" in g.vertex_attributes())
361361

362362
g.write_graphml(tmpfname)
363+
g.write_graphml(tmpfname, prefixattr=False)
363364

364365
def testGraphMLz(self):
365366
with temporary_file(

0 commit comments

Comments
 (0)