1

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?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
CuriousMind
  • 15,168
  • 20
  • 82
  • 120
  • I'd go with `(2)` first, this should be quite trivial, as you can control how your data is formatted. Just output it in a simple format like comma-separated values, which can be parsed blazingly fast using `string.split()`. If that turns out to be too slow, I'd think about using something like Boost Python, which would be a lot more work to implement. – Niklas B. Mar 06 '12 at 14:56
  • Please, thank us instead by contributing back to the community. – Lightness Races in Orbit Mar 06 '12 at 14:57
  • @NiklasB.: comma-separated values are a bit more complicated than that, as they may contain strings that contain commas. Python does have a `csv` module though, which handles this just fine. – Matthieu M. Mar 06 '12 at 15:40
  • I concur with NiklasB. Unix is designed for piping, and Python is designed for string parsing. Both are pretty darn fast. It shouldn't be that difficult to write the code to do it. I'd do that, and if it isn't fast enough, then worry about faster data transfer methods. – rob05c Mar 06 '12 at 16:37
  • @mathieau: Sorry, I didn't mean the full CSV file format... For simple data you don't need quote escaping and stuff, so you can just split at the commas. Depends on the type of data, of course. Maybe if the output involves complicated strings (or just strings containing commas), one could use another separator like `\n` to avoid problems (given that the strings don't contain newlines, of course). – Niklas B. Mar 06 '12 at 17:16

1 Answers1

1

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

Community
  • 1
  • 1
Sid
  • 7,511
  • 2
  • 28
  • 41