igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] Python's Graph.write_x producing empty files


From: Tamás Nepusz
Subject: Re: [igraph] Python's Graph.write_x producing empty files
Date: Tue, 14 Aug 2012 16:21:29 +0200

> Thanks! In the meantime, since I can use igraph to write the correct format, 
> and pipe it to sys.stdout, what would be the best hack for me to pipe stdout 
> to a file? It feels like another line of code could get the functionality I 
> need for the moment.

Well, it's a bit complicated. Since igraph's core is written in C and the 
functions that export a graph into a file require a FILE* object in the 
low-level C layer, it is not possible to use the standard Python trick, which 
would involve swapping sys.stdout with a file object created with the open() 
function and then swapping them back when igraph has saved everything. The only 
reason why sys.stdout works is because there is a standard protocol which tells 
igraph how to 'extract' a file handle from sys.stdout that we can pass on to 
igraph_write_graph_pajek in the C layer. The whole magic is in the 
igraphmodule_filehandle_init function in fileobject.c of the Python interface 
source code, and to be honest, I have no idea yet why it does not work on RHEL. 
The function essentially has two code paths: one that handles plain file names 
passed in strings, and one that handles Python's file-like objects such as 
sys.stdout. You have shown already that plain file names do not work for you, 
and
 it was the same with passing in an object that was created with Python's 
open() function. Theoretically, sys.stdout is no different from any other file 
object created with open(), so that's why I think that this is pretty weird. 
The only difference I can think of right now is that sys.stdout auto-flushes 
the output to the screen after every newline character, while Python file 
objects do not necessarily do that.

Can you please try this code snippet:

f = open("test.net", "w")
g.write_pajek(f)
f.flush()
f.close()

-- 
T.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]