Question

I have a piece of C++ code that generates the data. I want to expose the data to Python. But it is not as trivial as it sounds...

Say this C++ compiles into binaries cppcode. Because of the complicated way of the framework setup, I can only run the code as

./cppcode {command line arguments} 

Inside cppcode it generates data that I want to access from Python. I know if I can somehow rewrites the program as

CppClass cpp; 
cpp.run( args );

Then I can wrap up CppClass with Boost Python, build an instance, run with arguments, and access the data in generates.

But now that I can only run the code as ./cppcode {command line arguments}

I can't just call ./cppcode from Python, because after ./cppcode finishes, its memory will be recollected by the system, and I can't see it from Python anymore. Is there a way to get around that?

I can think of: (1) let ./cppcode dump the data into the disk and let python read it. (2) dump the data to stdout and pipe into python script. But either way I have to do some string parsing in Python that could be slow. Is there a way to get around it?

Was it helpful?

Solution

You could dump the data in JSON format from ccpcode onto the disk then simply use simplejson from python to parse it. Then you won't have to write any parsers of your own.

Here are some examples of C++ JSON modules: https://stackoverflow.com/questions/245973/whats-the-best-c-json-parser

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top