title: How to transfer and parse snap graph from python to c++
body: <p>Stanford <a href="http://snap.stanford.edu/snappy/index.html" rel="nofollow noreferrer">SNAP</a> is a well-known package for graph mining, and has both Python implementation and C++ implementation.</p>
  
  <p>I have some code in python to do graph mining using SNAP. I also have a C++ function process the snap graph. Now I need to write a wrapper so that this C++ function can be invoked from Python.</p>
  
  <p>The problem is that I don't know <strong>how to parse/dereference the snap graph object from Python to C++</strong>.</p>
  
  <p>The python code looks like: (More explanations come after the code examples)</p>
  
  <pre><code>import my_module;
  import snap;
  
  G = snap.GenRndGnm(snap.PUNGraph, 100, 1000);
  
  print(type(G));
  A = my_module.CppFunction(G);  # customized function
  print(A);
  </code></pre>
  
  <p>The CPP wrapper <code>my_module_in_cpp.cpp</code> looks like:</p>
  
  <pre><code>// module name: my_module, defined in the setup file
  // function to be called from python: CppFunction 
  #include &lt;Python.h&gt;
  //#include "my_file.h" // can be ignored in this minimal working example.
  #include "Snap.h"
  
  #include &lt;iostream&gt;
  
  static PyObject *My_moduleError;
  
    // module_name_function, THE CORE FUNCTION OF THIS QUESTION
  static PyObject *my_module_CppFunction(PyObject *self, PyObject *args) {
    PUNGraph G_py;
    int parseOK = PyArg_ParseTuple(args, "O", &amp;G_py);
    if (!parseOK) return NULL;
  
    std::cout &lt;&lt; "N: " &lt;&lt; G_py-&gt;GetNodes() &lt;&lt; ", E: " &lt;&lt; G_py-&gt;GetEdges() &lt;&lt; std::endl;
    fflush(stdout);
    if ((G_py-&gt;GetNodes()!=100)||(G_py-&gt;GetEdges()!=1000)) {
      PyErr_SetString(My_moduleError, "Graph reference incorrect.");
      return NULL;
    }
  
    PyObject *PList = PyList_New(0);
  
    PyList_Append(PList,Py_BuildValue("i",G_py-&gt;GetNodes()));
    PyList_Append(PList,Py_BuildValue("i",G_py-&gt;GetEdges()));
    return PList;
  }
  
  // To register the core function to python
  static PyMethodDef CppFunctionMethod[] = {
    {"CppFunction", my_module_CppFunction, METH_VARARGS, "To call CppFunction in C++"},
    {NULL,NULL,0,NULL}
  };
  
  extern "C" PyMODINIT_FUNC initmy_module(void) {
    PyObject *m =  Py_InitModule("my_module",CppFunctionMethod);
    if (m==NULL) return;
    My_moduleError = PyErr_NewException("my_module.error", NULL, NULL);
    Py_INCREF(My_moduleError);
    PyModule_AddObject(m, "error", My_moduleError);
  }
  </code></pre>
  
  <p>I'm using Ubuntu, python-2.7. In case someone may want to re-produce the problem, the <code>setup.py</code> file is also provided.</p>
  
  <pre><code>from distutils.core import setup, Extension
  
  module1 = Extension('my_module',\
      include_dirs = ['/usr/include/python2.7/','/users/&lt;my_local&gt;/Snap-3.0/','/users/&lt;my_local&gt;/Snap-3.0/snap-core','/users/&lt;my_local&gt;/Snap-3.0/glib-core'],
      library_dirs = ['/users/&lt;my_local&gt;/Snap-3.0/snap-core/'],
      extra_objects = ['/users/&lt;my_local&gt;/Snap-3.0/snap-core/Snap.o'],
      extra_compile_args=['-fopenmp','-std=c++11'],
      extra_link_args=['-lgomp'],
      sources = ['my_module_in_cpp.cpp'])
  
  setup (name = 'NoPackageName', version = '0.1',\
      description = 'No description.', ext_modules = [module1])
  </code></pre>
  
  <p>Every time I run the python code above, the error message "Graph reference incorrect." is displayed. </p>
  
  <p>Apparently <code>G_py-&gt;GetNodes()</code> and <code>G_py-&gt;GetEdges()</code> cause the problem. This must result from <code>G_py</code> not pointing to the right address/in right format. I tried using TUNGraph in the cpp code as well, it still does not point to the correct address. <strong>Is there any way that the pointer in C++ can point to the correct address of the original C++ object</strong>?</p>
  
  <p>Although in general it is hard to dereference a PythonObject from C++, but in this case I think it is doable since <em>Snap-Python is also implemented in C++. We just need to unwrap its python wrapper.</em> And the snap authors also provided the SWIG <a href="https://github.com/snap-stanford/snap-python/blob/master/swig/pungraph.i" rel="nofollow noreferrer">files</a>.</p>
  
  <p>Of course we can write the graph file in the disk, and read from that, but this will result in I/O and incur extra time consumption. And the snap-user-group does not have as much user traffic as stackoverflow.</p>
  
  <p>BTW, there are <code>networkx</code> and <code>stanford-nlp</code> tags, but no <code>stanford-snap</code> or similar tag referring to that tool. Can someone create such a tag?</p>